Provisioning EC2 instance on AWS using Terraform

Feel Bored to Launch AWS EC2 instance using Console?

Want to know how Enterprises Launch instances?

Want to know about IaC (Infrastructure as Code)?

Then you are at right place.

Before diving into how to provision an EC2 instance on AWS using Terraform, you deserve a bit of introduction to what is IaC (Infrastructure as Code), Terraform and why it’s needed.

What is IaC?

Infrastructure as code, or programmable infrastructure, means writing code (which can be done using a high level language or any descriptive language) to manage configurations and automate provisioning of infrastructure in addition to deployments. This is not simply writing scripts, but involves using tested and proven software development practices that are already being used in application development. For example: version control, testing, small deployments, use of design patterns etc. In short, this means you write code to provision and manage your server, in addition to automating processes.

What is Terraform?

Terraform is an Infrastructure as a code software by HASHICORP. It is a tool for building, changing and versioning infrastructure safely and efficiently. I can manage existing and popular service providers such as AWS, Azure, Google Cloud and other cloud vendors. You can compare Terraform with Cloud Formation only Cloud Formation is limited to AWS as it is a service of AWS while Terraform can do the same for all of the other services.

So now let’s see how we can use Terraform to trigger an EC2

Instance and the steps needed to do the same.

Steps to follow:

  • We will be doing the solution in Amazon Linux instance as covered in our aws tutorial, so I will download in the same environment. For that you need to enter in your EC2 instance .

Copy the download link for the Linux and in your instance use the following command:

wget > paste the link address

  • So now if you will do “ls”you must be getting the zip file of Terraform, you can use the following command to unzip:


Now we have to make sure that Terraform binary is available  on the Path.

So for Mac/Linux users follow this command:

echo $”export Path=\$Path:$(pwd)” >> ~/.bash_profile

Source  ~/.bash_profile

For windows users please make use of this link to work on this

  • Now you can go ahead and create one folder naming it anything of your choice :

mkdir terraform-test

cd terraform-test

Now create a file inside terraform-test  folder


  • You can now go ahead to IAM service and for the user created give appropriate policy “AmazonEC2FullAccess”

Now you can go to the security credentials in the of the user in the IAM service(learn from our amazon cloud computing certification) and click on Create access key > copy the Access key Id and the Secret access key

  • As it is done you can go ahead to your EC2 instance in my case for below script I have placed an access key and secret key you can place the appropriate user keys as required and update the region.

provider “aws” {

access_key = “AKIAIRP23RU7O7U44QIA”

secret_key = “VDgIMlzsgGeTMFgMnpv9EG3eZ3mkTKXoCrtrTaEm”

region     = “ap-south-1”


resource “aws_instance” “example” {

ami           = “ami-06bcd1131b2f55803”

instance_type = “t2.micro”


  • Now as we have been through this you can save it and come out of the file and initialize the working directory for terraform with the following command:

terraform init

The terraform init command is to initialize a working directory containing Terraform configuration files.

  • Finally you can provision the EC2 instance with this command:

terraform apply

  • Now you can enter into your management console can see your new instance is in the initializing state.

  • So we saw an simple example how to trigger an EC2 instance using Terraform, if you want you can destroy the instance from your terminal/command prompt/shell, you can type the simple command:

terraform destroy

To Master Terraform – Enroll with now.

For any queries you can contact us below:

Leave a Reply

Your email address will not be published. Required fields are marked *