Working with Terraform – Example 1 : Create an EC2 Instance

EC2 instance created using Terraform
Figure 6 : EC2 instance created using Terraform

  • In previous article , we have learned what is Infrastructure as Code (IaC) and a brief about Terraform and its benefits over other IaC tools . We have also configured/setup the terraform server.

  • Now, since we have enough knowledge about terraform and its installation process, its time to get into the actual implementation and learn how to build an infrastructure using terraform .

  • To start with terraform , in this article we will go with the simplest example of creating an EC2 instance by setting a bare-minimum properties for it and learn the steps involved in the deployment of infrastructure using terraform.


  • Have aws-cli installed and aws configured with secret keys.
  • Have a Terraform installed on the server . ( Follow this article to configure a Terraform server )

Create EC2 instance with Terraform

Step 1 – Create a Configuration file for Terraform AWS

Connect to the terraform server using SSH and create a file with .tf extention . We will specify all the configuration details in this file .


A blank file will open , click i button to Insert the code in this file .

Step 2 – Setting up AWS Provider

Provider tells Terraform which service it’s going to use to set up resources. Since we are working with AWS here , specify provider as aws and region where you wish to deploy your infrastructure. For ex. us-east-1 .

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

If you have configured aws by running “aws configure” as I did you do not need to provide the secret keys in the terraform file.

Step 3 – Setting up EC2 Instance

For creating an EC2 instance , we have to use resource type as “aws_instance” .  Write below code in file after the provider details. 

resource aws_instance "terrInstance" {
        ami = "ami-0c2b8ca1dad447f8a"
        instance_type = "t2.micro"

        tags = {
	Name = "Terraform Instance"


  • ami – The Amazon Machine Instance to run on EC2 instance. AMI ID can be found next to AMI name.  
              Look for the ami id in the region where you are creating the instance.
  • instance_type – The type of EC2 instance to run.
  • tags – Provide tags to the instance in key-value pair format.

You can also set various other properties available for aws instances.

Step 4 – Deploy the EC2 instance

Now let us see how to deploy/create the EC2 instance using terraform commands.

  • In the terminal, go into the folder where you have created and run terraform init command.

    Figure 1 : terraform init command


    When you are first starting with Terraform , you need to run terraform init to tell Terraform to scan the code , figure out what provider you’re using, and download the code for them. By default, the provider code will be downloaded in .terraform folder. 

  • Second step is to run terraform validate command to check whether the configuration is valid.
    Figure 2 : terraform validate command

    Figure 2 : terraform validate command

  • Run terraform plan command. This will let you see what terraform will do before actually doing it.
    Figure 3 : terraform plan command

    Figure 3 : terraform plan command

  • Final step is to run terraform apply command. This will actually create an EC2 instance in the specified region.
    Figure 4 : terraform apply command

    Figure 4 : terraform apply command

    Figure 5 : terraform apply completed

    Figure 5 : terraform apply completed

Output – EC2 instance created

Go to the region you have specified in the terraform file , you will see that an EC2 instance is successfully created .

EC2 instance created using Terraform

Figure 6 : EC2 instance created using Terraform

Now, if you don’t need this instance , you can easily destroy it by single command –

terraform destroy

Hope you like the article . If this is worth reading atleast once , please do like and share…!!!

In next article , we will deploy a single web server using terraform .

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: