CREATING EC2 INSTANCES USING TERRAFORM

6 mins Jun 21, 2021 Sandip Singh CLOUD Views : 4229

In the below blog, We will learn to write terraform templates to launch EC2 instances.

Table Of Contents

  1. What is Terraform?
  2. What is EC2?
  3. Installation of Terraform
  4. Installation of AWS CLI
  5. Configuring AWS CLI
  6. Create Working directory for Terraform
  7. Understanding Terraform files
  8. Launching EC2 instances from AWS Managed AMI
  9. Launching EC2 instances from Custom AMI

Prerequisites

  1. Installation of Terraform
  2. Installation of AWS CLI
  3. IAM user with Programmatic access

What is Terraform?

  1. Terraform is used to Create, Modify and delete the Infrastructure.
  2. It works across cloud providers like AWS, microsoft azure cloud computing platform and GCP etc.
  3. A template will be written which explains about the resources using which we can launch the resources.

What is EC2?

EC2 stands for Elastic Compute Cloud.

EC2 provides elastic servers in the google cloud services which means it is easy to set up and scale as per the requirement.


INSTALLING TERRAFORM

Install terraform using One of the below options

  1. Using binary package (.zip)
  2. Compiling from source

https://learn.hashicorp.com/tutorials/terraform/install-cli

From the above link , Depending on the OS , download the terraform and install it.

By executing the below command,Terraform version can be identified

terraform -v


INSTALLING AWS CLI

AWS CLI is a tool to create and manage the AWS resources programmatically

Install AWS CLI using the below command

sudo apt-get install AWS cli

Run the below command to check the installed version

AWS — version

Installing AWS CLI

CONFIGURING AWS CLI

Profiles should be configured with access and secret keys so that the terraform files can be used for authentication while provisioning the resources.

The provided access and secret keys should the permissions to launch AWS resources using terraform

Programmatic access for an IAM user gives us access and secret keys

Provide the access key and secret key and AWS region when prompted while running the below command.

 configure


UNDERSTANDING TERRAFORM FILES

variables.tf:

This file consists of Access Key ID , Secret Key, and Region which will be used for authentication

What not to do with Access Keys?

Never use hard coded secret credentials in a file


WHAT SHOULD WE DO?

Instead, We will setup AWS CLI, an open source tool that enables you to interact with AWS Cloud services using commands in your command-line shell.

Then we will add AWS keys to /home/zenesys/.aws/credentials file.

We can have ’n’ of profiles in the credentials file , which can be referred to in the terraform template.

Providers.tf:

Resources are implemented with the help of providers plugin

Terraform supports providers such as AWS, Azure and GCP , IBM, Oracle Cloud, and Digital Ocean.

Terraform supports providers

Hence Amazon Web Services is One Provider.

Main.tf

A file which consists of a template to provision the resources in the cloud.

You can provide a custom name for this file.


CREATE EC2 INSTANCE FROM AWS MANAGED AMI

It is the base images for any operating system , provided and managed by the AWS

For this, Login to EC2 Management Console

Choose Instances → Launch Instance

Lists of OS can be found here.

And each OS has their own AMI ID referred

 

Create EC2 Instance from AWS Managed AMI

 

The following AMI ID can be used , If you want the Ubuntu 18.04 LTS OS to be provisioned using the terraform


LAUNCHING AN EC2 INSTANCE USING CUSTOM AMI

If you want to create a copy of an EC2 instance with all the configurations,

Then Create an AMI of that Instance which provides an AMI ID, and it can be used in the terraform file.

The above images we created is referred to as Custom AMI

To create AMI from an EC2 instance.

Select the instance → Choose Actions → Image , click Create image.

Also read: Tracking S3 Bucket Changes using Lambda Function

Launching an EC2 instance using Custom AMI

Provide the name for AMI and don’t forget to enable No reboot

choose Create Image

AMI enable No reboot

template for AMI Page

The AMI ID for the created AMI can be found on the AMI page.

 

template for AWS Managed AMI

The template works for Custom AMI and AWS Managed AMI.

resource “aws_instance” “testinstance” {

ami = “ami — 028598a84ca601344”

instance_type = “m5.4xlarge”

subnet_id = “subnet-06a4dd555ee331cac”

associate_public_ip_address “false”

vpc_security_group_ids = [ “sg-0b7a71dca0a95842f” ]

key_name “testinstance”

tags {

Name = “testinstance”

}

}

aws instance

The above file is explained as follows

resource is aws_instance — the type of resources that terraform will create

testinstance — a name for the resources that it creates.

ami — AMI ID of OS, It can either be Custom AMI or AWS managed AMI.

instance_type — Type of instance depending on the requirement such as CPU cores and Memory

subnet_id — refers to the subnet where the instance should be launched.

associate_public_ip_address — For instance to have a public IP address , set the value to true else false.

vpc_security_group_id — Existing security group can be attached to the instance by referring it here

tags — Name the resources that we create.

You may also Like: Provisioning RDS Instances using Terraform


CREATING INFRASTRUCTURE

GO TO THE LOCATION IN YOUR SYSTEM WHERE YOU HAVE CREATED ALL THE TERRAFORM FILES

#terraform init

The above command installs the required plugin. In our case , the AWS provider plugin will be installed.

Creating Infrastructure of terraform

#terraform plan

It provides details such as what action will be taken If we run terraform apply.

terraform plan

terraform apply

#terraform apply

The above command asks for the confirmation, Type yes

Awesome!, With the help of terraform We are able to provision the EC2 instances in the AWS.

Must Read: What are the types of Cloud Computing Service Models