Creating EC2 Instances using Terraform

We will learn to write terraform templates to launch EC2 instances.

Table of Contents

PreRequisites

What is Terraform?

What is EC2?

EC2 stands for Elastic Compute Cloud.

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

Installing Terraform

Install terraform using One of the below options

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

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 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.

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

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.

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

choose Create Image

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

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”

}

}

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.

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.

#terraform plan

It provides details such as what action will be taken If we run 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.

Zenesys is an 11-year-old IT Company based in the USA. Our key services: Mobile & Web Development, RPA, CMS, UI/UX & Cloud Services, etc. with the best solution