How To Create EC2 Instance Using Terraform

What Is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as On-premise datacenters.

Hence It is called as Infrastructure as a Code.

Installing Terraform:

If you havn’t installed terraform yet, You can go ahead and install using the below article.

Install Terraform In Linux

Setting Up Project Directory:

This is the place where you will store all the terraform files.

So What we are going to do is, we will create a folder and inside that we will create terraform files.

We need to create following files:

creds.tf , providers.tf , .gitignore , main.tf

Terraform will automatically pick all the .tf files within the directory.

Explanation Of .Tf Files:

variables.tf:

This is the place where we will store all the AWS secrets such as Access Key ID , Secret Key, Region.

aws_access_key – It makes an API call to AWS resources from your machine.

aws_secret_key – Secret Access Key that’s associated with Access Key.

aws_region – The AWS region where you want to create all your resources.

Providers.tf:

Providers are interfaces to the services that will maintain our resources.There are many cloud providers supported by terraform such as AWS, Azure and Google Cloud, IBM, Oracle Cloud, Digital Ocean.

Hence Amazon Web Services is One Provider.

The AWS Provider requires Access_Key (Which IAM user the terraform should use ) and Secret_key (Allows Authentication) and aws_region represents where the terraform should initiate creating the infrastructure.

Main.tf

You can change the name of this file as per the requirement and based on the Directory structure.

This section can be segregated into 2 parts:

  • Launch an EC2 instance from AWS Managed AMI
  • Launch an EC2 instance from Custom AMI

Launching An EC2 Instance From AWS Managed AMI:

AWS Managed AMI’s is the default AMI’s which AWS will provide to us based on the Operating system and architecture.

For this, Login to EC2 Management Console:

https://ap-southeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1#Home:

Choose Instances , Select Launch Instance , Here you will find the lists of Operating systems.

For example, You want to launch Ubuntu 18.04, You can find the AMI ID of Operating system,

You should use this AMI ID in your terraform file to launch an EC2 Instance with Ubuntu 18.04

Launching An EC2 Instance Using Custom AMI:

Let’s say you have an EC2 instance UP and running and you want to create another EC2 instance with same configurations.

Now, What you have do is, Create an image from that EC2 Instance and you will have a custom AMI with AMI ID, You should use that ami id in your terraform file.

Let’s see how to create custom AMI from existing EC2 Instance.

I already have an EC2 instance, Select the instance

Choose Actions , Select Image , Create image.

Fill up the details and Most importantly , You should check no reboot , To avoid instance from rebooting.

choose Create Image

Click on the AMI ID, It will take you to AMI Page.

You can find the AMI ID for the AMI you have created from your existing EC2 Instance.

Now, Lets back to Terraform file.

Here is the terraform file, I have created to launch an EC2 Instance.

This script is applicable for both, Using Custom AMI and AWS Managed AMI, All you have to do is just replace with the AMI ID.

We need to the describe the server we want in the .tf file.

resource is aws_instance – we are launching an EC2 instance (resource)

testinstance – a unique identifier for the resource we are creating.

ami – AMI ID of the operating system, It can be a custom AMI or AWS managed AMI.

instance_type – Each instance have an instance type specified based on the vCPU and Memory.

subnet_id – On which subnet you want to launch the instance.

associate_public_ip_address – If you want to have an ec2 instance with public ip , then value should be true

vpc_security_group_id – If you have an security group in place, You can mention it’s id here.

tags – how you want the instance to be named.

Now that we have all the informations in place.

Creating Infrastructure:

Open the terminal, Go the directory where you have kept all the terraform files and run

#terraform init

It will download and install the proper version of aws provider with required plugins to run the .tf files.

You can do dry run , Before applying the terraform files.

#terraform plan

It will explain us what will happen to my Infrastructure If I run terraform apply.

So based on the .tf I have in my directory, It says 2 instance will be added to my Infra.

Now you can run,

#terraform apply

It will ask for the confirmation, Type yes

Awesome!, you have successfully created your first EC2 instance in AWS using Terraform.

Hope you liked my post, Please do check out my other posts.