How to Create an AWS Instance with Terraform

AWS EC2 instances are virtual machines that run an operating system. Administrators can use Terraform’s command files to automate deployment of EC2 instances.

aws

3 minutes

AWS EC2 instances are virtual machines that run an operating system. It’s not uncommon for administrators in an enterprise environment to deploy and de-provision numerous EC2 instances every year. 

Terraform automates this process for administrators. Instead of manually creating an EC2 instance and configuring it, admins create Terraform files containing commands to perform repeatable deployments of every instance, saving time in the process.

How to Write an EC2 Instance with Terraform: An Example

Terraform files contain commands to perform deployments and automation against the AWS interface. In this example, the EC2 instance is created in one file named ec2_create.tf and a separate variable file named variables.tf stores the name of the Amazon image, the size of the instance, and the name of the virtual private cloud (VPC).

Step 1: Create the EC2 File

The first step is to define the provider, which is AWS in this example. Then, you define the EC2 configurations. Use the following code for the ec2_create.tf file:

terraform {

  required_providers {

    aws = {

      source = "hashicorp/aws"

    }

  }

}

resource "aws_instance" "" {

  ami           = var.ami

  instance_type = var.instance_type

  network_interface {

    network_interface_id = var.network_interface_id

    device_index         = 0

  }

  credit_specification {

    cpu_credits = "unlimited"

  }

}

In the commands above, an AWS instance is created with unlimited CPU usage. The resource name and VPC are set to variables created in the next step.

Step 2: Create the Variables File

The ec2_create.tf file is a base template for creating an EC2 instance, but having a separate variable file makes it dynamic. Using a separate variable file lets you use the same base creation template for numerous other EC2 instances. Use the following code for the variables.tf file:

variable "network_interface_id" {

  type = string

  default = "network_id_from_aws"

}

variable "ami" {

    type = string

    default = "ami-005e54dee72cc1d00"

}

variable "instance_type" {

    type = string

    default = "t2.micro"

}

The above code sets the instance type to micro and assigns the EC2 instance to an image and network interface. The default operating system set to the instance is Ubuntu, so this virtual machine will run Linux.

Step 3: Execute Terraform Commands

With the files created, you can now send them to Terraform and execute the commands. First, you must initialize Terraform by using:

terraform init

You can validate the commands and ensure you have no errors in syntax using:

terraform plan

Next, apply the changes to your AWS environment:

terraform apply --auto-approve

AWS 101: How to Launch Multiple EC2 Instances in Terraform

In an enterprise environment, you might need several instances created at the same time to host multiple applications, test multiple versions of an application, or use as a group of hosts to perform a specific action. You only need to make one change to your ec2_create.tf file to create multiple instances at the same time.

Step 1: Create the EC2 File

Notice that the difference between the single-instance deployment and the multi-instance deployment is the “count” command. In this example, five EC2 instances are created. Use the following commands for the EC2 instances:

terraform {

  required_providers {

    aws = {

      source = "hashicorp/aws"

    }

  }

}

resource "aws_instance" "" {

  ami           = var.ami

  instance_type = var.instance_type

  count = 5

  network_interface {

    network_interface_id = var.network_interface_id

    device_index         = 0

  }

  credit_specification {

    cpu_credits = "unlimited"

  }

}

Try Out FlashBlade

Step 2: Create the Variables File

The variables file can remain the same. Ubuntu is used to create the virtual machine, and the type of instance is set to a micro setting. The following content can be used as the variables.tf file:

variable "network_interface_id" {

  type = string

  default = "network_id_from_aws"

}

variable "ami" {

    type = string

    default = "ami-005e54dee72cc1d00"

}

variable "instance_type" {

    type = string

    default = "t2.micro"

}

Step 3: Execute the Terraform Commands

The same commands can be used to execute the commands in Terraform. The following commands will send the command files to Terraform and create the EC2 instances:

terraform init

You can validate the commands and ensure you have no errors in syntax using:

terraform plan

Next, apply the changes to your AWS environment:

 terraform apply --auto-approve

AWS Conclusion

Terraform makes it easy for administrators to provision cloud resources in AWS. Using Terraform’s command files, you can automate provisioning to reduce the overhead of manually creating instances in the AWS dashboard. To learn more, check out How to Use AWS IAM with Terraform.