Create EC2 Instance using Terraform

In this blog we will see how to create EC2 instance using terraform. The blog contains the 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 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 file is saved in the github location.

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 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: Logo

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

Facebook photo

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

Connecting to %s