• Home
  • AWS and Terraform for beginners Lesson 1 Run AWS EC2 Instance Using Terraform

AWS and Terraform for beginners Lesson 1 Run AWS EC2 Instance Using Terraform

AWS and Terraform for beginners Lesson 1 Installation Terraform and Setup Environment

In this tutorial series i will teach you very basic concepts of the Terraform and how to use Terraform to build aws infrastructure.i will start with very basic level.

What is Terraform

Terraform is the tool that created by company name hashicorp. Terraform help you to cloud Infrastructure automation easily.It support various  cloud platforms like aws Azure google cloud and more.In this tutorial im using AWS.

you can find more details from Terraform official website https://www.terraform.io/intro/index.html

Before we get started we need few things

  1. Amazon web service account (aws) Free Tire is enough for learning
  2. Python 2.7
  3. Ubuntu 16.04 or grater version because i’m conducting this lesson series using Ubuntu 16.04.You can also use windows or mac.If you can use Cygwin https://www.cygwin.com/  you can execute same command in windows or mac
  4. Python package installer (pip).You can install pip using
    sudo apt-get install python-pip or easy_install pip
  5. AWS-CLI tool
  6. Terraform you can dowload suitable version from https://www.terraform.io/downloads.html .I’m using 64bit version
  7. Any text Editor

Lets get started.

How to Create Amazon Web Service Free Tire Account

I hope any one have problem with creating aws account. go to https://aws.amazon.com/ and sign in to console and create new account it will ask your credit card or debit card details first time they will deduct $1 from your debit card and after that they will return it back for user verification.if you face any problem i will explain how to create aws free tier account for one year in another post.

Install PIP on Ubuntu

sudo apt-get install python-pip 

easy_install pip

Install AWS-CLI Tool

pip install awscli


After you installed pip it will display  never version is pip there.i upgrade pip using command

pip install --upgrade pip

now i have pip 10.0.1 but when you try to install aws-cli tool this error will occur

pip install awscli
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main

let’s solve this

python – ImportError: cannot import name main Solved

Edit /usr/bin/pip

nano /usr/bin/pip

change this code to following

from pip import main
if __name__ == '__main__':

Changed code

from pip import __main__
if __name__ == '__main__':

Now everything should working fine

lets try again

pip install awscli

now its successfully installed awscli tool

Lets install Terraform on Ubuntu 16.04

Terraform Installation on Ubuntu 16.04

Download terraform from official website i use 64 bit version.


Then extract it

and move to /usr/local/bin

mv terraform /usr/local/bin/

Add executable permission for terraform

chmod +x /usr/local/bin/terraform

Check terraform version by

terraform -version
Terraform v0.11.7

Now we are good to go

Now we setup Terraform with AWS

Terraform setup with AWS

we can do this in four easy steps

  1. Create Specific user for terraform
  2. Save Credential file to your computer.Because this is one time process .Second time you won’t credentials on aws console
  3. Attach policies for specific user
  4. Pass credentials to the terraform script

Log in to your aws account and open aws console and search for ami Identity and Access Management

click users

Then give a name and tic Programmatic access permission and then click permission button

Then next window select Attach existing policies directly and in a search bar search ec2.Then select  AmazonEC2FullAccess

Then review

Then create user and download .csv file contain access key id and secret key id this is one time process.

Now you can see new user does not belong to any group lets create admin group and add user to admin group.

give name as admin

click next step and Attach Policy.tic on AdministratorAccess and click next step

Then review and create group.This creates new group called admin but  no users attached to group.we are adding sam user to this group.click on group name admin and this will show like this

select sam user and add user

now sam user belong to admin group we are good with aws configuration.we have created sam user and add to admin group.lets create our first terraform script and check.

Basic Terraform Script

Start Amazon EC2 Instance Using Terraform

Create new folder with any name im creating folder in my home name terraform_lessons and change directory to newly created folder .we are create our first terraform script there.

mkdir /home/sameera/terraform_lessons

now i’m inside new folder

go to aws console and click on ec2 for there is no currently running instances.

using nano we create our first script.Before we write our script we need to chose Amazon Machine Image (AMI).for select image go to Create Instance launch instance window and copy AMI id

copy AMI id and save in text file later we need that AMi id for the script i copied free tire eligible amazon linux machine image

ok lets start our script

root@sameera-pc:/home/sameera/terraform_lessons# nano main.tf
provider "aws" {
  region      = "us-east-2"
  access_key  = "your access key "
  secret_key  = "your secret key"


resource "aws_instance" "Terraforminstance" { 
   ami  = "ami-922914f7"
   instance_type = "t2.micro"
      tags { 
        Name = "MyApp"

we are using t2.micro instance here because its free tier.you can see your region by aws console url

and get access key and secret key from the .csv file we have download it before when we created sam user.i gave a name for this instance as MyApp. after save the main.tf file and we ready to go.

execute command terraform plan if everything is ok it will show like this.

gave me some error.ask to run

terraform init

let’s run and check again

we are good to go then execute

terraform apply

This will ask question to build the infrastructure answer is yes

press enter

now terraform is building infrastructure for you inside your aws.

Now Ec2 instance is created.

lets check on aws console.previously there is no instance running now one instance is running.

Lets take a look at that instance

its working now.

Let’s try to terminate this instance using terraform

Stop EC2 instance using terraform

execute command

terraform destroy

this will destroy the infrastructure.Again it will ask for question do you want to destroy answer is yes and press enter.

Lets check it on aws console

Previously we created instance is terminated.I hope you will enjoy this tutorial.in next tutorial i will discuss more about terraform and aws.If you really enjoy this tutorial please share on facebook twitter google plus

Have a Good Day

Sameera Dissanayaka






One comment

Leave A Comment