Creating AWS Instance using Terraform

I appreciate that you are using AWS dashboard to create resources on amazon web services. There is no problem in using AWS dashboard to manage your infrastructure. You can also use AWS-CLI to manage your infrastructure on AWS if you are a fan of command line. (which is actually a good thing).

The problem arises when your servers / resources on AWS grows and keep on growing and you are working with a team to manage aws resources. As your infrastructure on AWS grows, you will find it difficult to manage and keep it in consistent state.

You need a infrastructure provisoning tool. Though major Cloud Service Providers (CSP) have their own tools. Terraform is gaining popularity as terraform can be used to manage around 40 major CSP’s including AWS, Google Cloud Platform, Microsoft Azure, Rackspace and the list goes on.

We can it IAC (Infrastructure As Code). This is your first actual step towards DevOps.

I firmly believe the best way of learning anything new is, by doing it.

So lets start creatinga instance on AWS using Terraform.

Creating AWS Account

You should have a AWS account with you. Recommend creating AWS Free Tier account for the purpose.

Creating AWS Access Keys

Once you are inside your AWS dashboard. Go and create security keys for your administrator access account. Its highly recommended not to use the same login, which you have used for creating the account. Better, create a new IAM user , assign him administrative privileges and align keys with that IAM user account.

creating access keys for aws iam user account
Attaching Access Keys with IAM User

Never, ever share this key with anyone. Download the key, when prompted.

Installing Terraform

Download and Install Terraform in your Linux machine. I am using Linux (Ubuntu) machine for this purpose. Once downloaded, go to “Downloads” directory (the default location in Linux machines) and run:

unzip -d /usr/local/bin

Verify the installation by giving command:

terraform -v
terraform installation on linux machine
Verify Terraform Installation

Installing Amazon Web Service Command Line

Download and install AWS-CLI package also. Switch to “root” user.

 ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

Confirm the installation using

aws --version
installing aws cli on Linux machine
AWS CLI Installation and Confirmation

Configuring AWS Command Line

Use “aws configure” command and supply your “aws key” and “aws secret” when prompted.

connecting aws cli to your aws account using aws configure
aws configure command

Also to connect terraform with your AWS account. You need to export two environment variables and supply your “aws key” and “aws secret” , which you have supplied while using “aws configure” command.

export AWS_ACCESS_KEY_ID=<your aws access key>
export AWS_SECRET_ACCESS_KEY=<your aws secret access key>

Checking AWS CLI Connectivity

Once you are all set and have done the things right. You are ready to go and create aws instance using terraform.

For confirmation, check your AWS account connectivity using AWS-CLI. This simple command will do:

aws ec2 describe-regions
aws cli instalation and configuration

Creating AWS Instance using Terraform

If you had came this, you are just one step away from creating your first aws instance using terraform.

Create a directory name – morning (you can use any name of your choice)

Go inside the morning directory and create a file with extension of .tf

I am creating a file name – , with the following contents to create aws instance using terraform.

I am using minimum options here, just to make things easy at our first attempt.

creating aws instance using terraform
Creating aws instance using terraform

You can copy and paste the contents, if you wish and you are feeling lazy today.

provider "aws" {
   region = "ap-south-1"
 resource "aws_instance" "good-morning" {
   ami               = "ami-5b673c34"
   instance_type     = "t2.micro"
   availability_zone = "ap-south-1a"
   tags = {
         Name = "first-server"

So here, we are using cloud provider “aws” and choosen region “ap-south-1” (which is for Mumbai region) to create our aws instance using terraform.

Further, we are using resource type as “aws_instance” and name of the resource of “good-morning”. We intent to use ami ID of “ami-5b673c34” and instance type of “t2.micro” (as its available under free tier). We have also choosen specific AZ “ap-south-1a” for this instance. Finally we have given this aws instance a name of “first-server”

Applying Terraform Code

Before running our terraform code. Its always good to check the file for any syntax errors:

 terraform validate

If its all ok, then run the command “terraform apply” to execute our terraform code on AWS. At the first attempt it will prompt you for plugin error.

No need to panic, just execute “terraform init” command to download the aws plugin.

terraform init

Once its done, you can now give “terraform apply” to run the terraform code. Supply “yes” when prompted.

using terraform apply
terraform apply

You will soon get the same update on screen.

output of terraform apply command
output of terraform apply command

Confirm Instance Creation in AWS Dashboard

Confirm the same by going on AWS dashboard and you will see your aws instance created using terraform.

confirm aws instance creation using terraform code
Confirm aws instance creation using terraform

Congrats , you have taken your first step towards DevOps, successfully.

Destroying Terraform Code

Don’t forget to delete your aws instance to avoid un-necessary billing.

terraform destroy

Confirm with “yes” when prompted.

terraform destroy command
using terraform destroy command

It will delete every single resource which is created by .tf files under “morning” directory. As you are giving the command from inside “morning” directory.

If you would like to know more about AWS, check out: AWS Training Program
If you are completely new to Linux, I would suggest that you go through the basic Linux program also known as RHCSA: Linux Training
If you are an expert with Linux, grow your career with RHCE, Devops, Openstack or Openshift.