Introduction to Terraform — EC2 Instance Creation using Terraform

Gaurav Gupta
5 min readNov 24, 2019

--

This is my first article on Terraform, In this article, I’ll talk about overview on Terraform & Creation of EC2 Instance using Terraform.

So first question comes in mind! What is Terraform? What is use of it?

Terraform(IAC- Infrastructure as Code) is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular cloud service providers(AWS, AZURE, GCP, Alibaba) as well as custom in-house solutions. It is one of the famous DevOps tools in market.

Terraform is an open-source infrastructure as a code software tool created by HashiCorp. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language (HCL).

For Example, Suppose you need 2 EC2 Instance, 2 EIP attached with that EC2 Instances, 1 Security Group, 1 Load Balancer etc, So you’ll manually create it in AWS Console/CLI/SDK. Now in case, you need same thing, many times in your requirements, so this will be hectic work for you. So here comes Terraform, Write code once, use it(You can modify) according to you many times.

As we said, Terraform is IAC tool, what is this IAC?

Infrastructure as a Code(IAC) is a process of managing and provisioning mechanism for authenticating, planning and implementing servers and data centers in the cloud and private network through machine readable and understandable configuration files rather than physically configuring the hardware. Many infrastructures including bare metal servers and virtual servers can be configured through this method.

We’ll use AWS Cloud Provider for hands-on purpose. I’ll install the Terraform on EC2 Instance. I hope you have some basic knowledge on AWS EC2 Instance & one AWS account.

I have already created one EC2 Instance to install Terraform on it. I have used Amazon Linux 2 AMI (HVM) to create EC2 Instance.

EC2 Instance for Terraform Installation

I have connected this EC2 instance using putty(as using Windows OS Platform).

Connected EC2 Instance with Putty

Terraform Installation:

Let’ install Terraform on it, use below commands to install Terraform on Amazon Linux 2.

Step 1- Download Binary:

curl -O https://releases.hashicorp.com/terraform/0.12.16/terraform_0.12.16_linux_amd64.zip

Step 2- Unzip the binary to /usr/bin

unzip terraform_0.12.16_linux_amd64.zip -d /usr/bin/

Step 3- Check Terraform Version

terraform -v

Terraform Installation

We have successfully installed the Terraform on EC2 Instance, Now we’ll create one EC2 Instance using Terraform. I’ll use Pycharm to write code.

Once Terraform installed, we’ll configure aws IAM credentials in-order to use with AWS or you can create variables.tf inside gitignore directory.

AWS IAM Configuration

Creation of EC2 Instance Using Terraform:

The first step to using Terraform is typically to configure the providers (We’re using AWS here) you want to use. Create a file called providers.tf & provide the provider information.

Provider Info- providers.tf

Basically it tells Terraform that you are going to be using AWS as your provider and that you want to deploy your infrastructure into the us-east-1 region.

For each type of provider, there are many different kinds of resources that you can create, such as instrances, databases, security group, VPC and load balancers etc. Syntax for creating a resource in Terraform is:

Resources Syntax

Provider can be any as per you: aws, azure, gcp.

TYPE is the type of resource to create in that provider (ex: instance)

Configuration basically consists of one or more arguments that are specific to that resource. We’ll create one more file called main.tf

So for creating the EC2 Instance, we need basically ami, instance type & tags.

main.tf

Now we will go to terminal, go to that path where you created main.tf and run the terraform init command.

terraform init

The terraform binary contains the basic functionality for Terraform, but it does not come with the code for any of the cloud providers, so when you’re first starting to use Terraform, you need to run terraform init to tell Terraform to scan the code, figure out which providers you’re using, and download the code for them. By default, the provider code will be downloaded into a .terraform folder, which is Terraform’s scratch directory. You need to run init any time you start with new Terraform code, and that it’s safe to run init multiple times (the command is idempotent).

Now that you have the provider code downloaded, run the terraform plan command.

terraform plan

The plan command lets you see what Terraform will do before actually making any changes. This is a great way to sanity check your code before unleashing it onto the world. Anything with a plus sign (+) will be created, anything with a minus sign (–) will be deleted, and anything with a tilde sign (~) will be modified in place. In the preceding output, you can see that Terraform is planning on creating a single EC2 Instance and nothing else, which is exactly what you want.

To actually create the Instance, run the terraform apply command.

terraform apply

You’ll notice that the apply command shows you the same plan output and asks you to confirm whether you actually want to proceed with this plan.
Type yes and hit Enter to deploy the EC2 Instance.

EC2 Instance Created

Now you deployed an EC2 Instance in your AWS account using Terraform. Let’s check whether it’s created or not. Open your AWS Console & go to EC2.

This is for basic introduction of Terraform & We have deployed our first EC2 Instance using Terraform.

Hit clap if you find this article useful.

--

--

Gaurav Gupta
Gaurav Gupta

Responses (9)