Create EC2 Instance using Terraform

In this blog we will see how to create EC2 instance using terraform. The blog contains the main.tf file used for creating the EC2 instance.

1st thing to perform is to pass on the aws key and secret using “aws configure” command.

aws configure command

The next step is to create a main.tf file which will cerate an EC2 instance. In this example, I am creating a t2.micro instance with amazon ami. You can add more parameters like “user data”, network interface properties etc if needed.

# Configure the AWS Provider
provider "aws" {
  region = "ap-south-1"
}
#resource creation
resource "aws_instance" "vm1" {
  ami           = "ami-041db4a969fe3eb68" 
  instance_type = "t2.micro"
  key_name = "myawskeypair"
tags = {
    Owner = "Amal"
    Createdby = "terraform"
  } 

root_block_device {
  encrypted = "true"
  volume_size = "20"
  volume_type ="standard"
}
}
 

The updated main.tf file is saved in the github location.

https://github.com/amalkabraham001/MyWordpressRepo/blob/1945f7df95f111175128946ecbd73691bf4e22bb/Terraform/main.tf

Terraform init command will initiate the terraform by installing the required modules.

Terraform Init command

Terraform plan command will create an execution plan and let you know the proposed set of actions. I ran Terraform plan command to validate if my main.tf file is valid.

Finally terraform apply command to deploy the EC2 instance.

Terraform Apply command

Verify in the AWS EC2 console to verify if the VM got created using Terraform.

Final VM creation screen

More DevOps blogs are on the way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s