DEV Community

Cover image for How to create AWS Instance with Terraform
Jameer Khan
Jameer Khan

Posted on • Originally published at stackblogger.com

How to create AWS Instance with Terraform

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp that enables you to define and provision data centre infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL). This article provides simple step-by-step guide on how to create and update AWS instance with Terraform script.

Unlike traditional methods that involve multiple manual steps to configure a cloud, Terraform provides a unified approach to create and manage infrastructure across various cloud providers.

Benefits of Using Terraform

Terraform has several benefits. Some of the key points are mentioned here.

Provider Flexibility

One of the most significant advantages of Terraform is its ability to work with multiple cloud providers. You can manage resources on AWS, Google Cloud, Azure, and others from a single tool, facilitating a true multi-cloud strategy.

Infrastructure as Code

Terraform enables you to manage infrastructure through code, which can be versioned and stored in a source control system like Git.

State Management

Terraform keeps track of the current state of your infrastructure in a state file. This state file acts as a source of truth that allows Terraform to understand what has already been deployed and make necessary adjustments when changes are made.

Plan and Apply

Terraform provides a clear two-step process when applying changes to infrastructure. First, the terraform plan command generates an execution plan that shows what changes will be made. This allows users to review and confirm changes before they are applied. After reviewing, you can execute terraform apply to create or modify the resources. This provides an extra layer of safety and assurance.

Create an AWS EC2 Instance with Terraform

Now, let’s walk through the steps to create an AWS EC2 instance using Terraform.

Install Terraform

If you haven’t already, download and install Terraform from the official Terraform website and ensure you have an active AWS account.

Verify Terraform is installed by running  raw `terraform -help` endraw  on your machine

Create Terraform Configuration Files

Create a directory for your project and navigate into it:

mkdir terraform-aws-instance

cd terraform-aws-instance/
Enter fullscreen mode Exit fullscreen mode

Then, create a configuration file named main.tf with following code:

provider "aws" {
  region = "us-east-1"
}

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"]
}

resource "aws_instance" "aws_instance_example" {
  ami               = data.aws_ami.ubuntu.id
  instance_type         = "t2.micro"

  tags = {
    Name = "AWSAppInstance"
  }
}

output "public_ip" {
  value = aws_instance.aws_instance_example.public_ip
}
Enter fullscreen mode Exit fullscreen mode

Initialize Terraform

Run the initialization command to set up your working directory:

terraform init
Enter fullscreen mode Exit fullscreen mode

Terraform Init Output

Plan and Apply

Generate an execution plan to see what Terraform will do:

terraform plan -out="example-instance"
Enter fullscreen mode Exit fullscreen mode

The -out flag in Terraform is used to specify the filename for the plan file when you generate an execution plan. This allows you to save the planned changes to a file instead of executing them immediately.

Terraform Plan

Apply your configuration to create the instance:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Type yes when asked and hit ENTER.

Once the action is confirmed, Terraform will provision the EC2 instance.

Terraform Apply

Check the AWS Account instances page, you will see a new instance is created and running.

AWS Instance created by Terraform

Updating the Instance Type

Suppose you want to change the instance type from t2.micro to t2.small in your existing EC2 instance configuration. Here’s how to do it:

Modify the Configuration

Open the main.tf file and update the instance_type parameter:

resource "aws_instance" "aws_instance_example" {
  ami               = data.aws_ami.ubuntu.id
  instance_type     = "t2.small"

  tags = {
    Name = "AWSAppInstance"
  }
}
Enter fullscreen mode Exit fullscreen mode

Plan the Changes

Run the following command to see what changes will be made:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Terraform Plan Updated Changes

Apply the Changes

After reviewing the plan, you can apply the changes:

terraform apply
Enter fullscreen mode Exit fullscreen mode

You’ll need to confirm the action, and Terraform will update the EC2 instance according to your new configuration.

Terraform Applied Changes

Destroying the EC2 Instance

If you want to remove the EC2 instance you created, you can do so using the terraform destroy command. This command will delete all resources defined in your Terraform configuration.

To destroy the instance, run:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Confirm the action with yes and hit ENTER.

Terraform Destroy

Conclusion

Terraform provides a powerful and flexible way to manage cloud infrastructure, allowing you to automate the deployment and management of AWS resources seamlessly.

With Terraform, you can build a cloud environment that meets your needs efficiently and reliably. Plus, updating and changing your infrastructure is simple, allowing you to adapt to the needs of your projects as they change.

Top comments (0)