Connect with us

DevOps

Introduction to Terraform — Create an EC2 instance using Terraform

Developed by HashiCorp, Terraform is an open-source tool that is used for defining and provisioning infrastructure of cloud platforms using code. In fact, Terraform is colloquially referred to as an Infrastructure As Code (IaC) . Terraform is Cloud agnostic and you can use it to manage a wide range of infrastructure services on different platforms such as AWS, Google Cloud, and GCP. In this guide, you will learn how to install Terraform, and we shall later demonstrate how to create an EC2 instance using Terraform.

Step 1: Download and install Terraform

First, we need to download and install Terraform. At the time of writing this guide, the latest version of Terraform is 0.15.3. But first, update the package lists as shown:

Next, install the prerequisite packages that will be required during the installation of Terraform:

Next, download the Terraform zip file as shown.

Download Terraform Ubuntu 20.04
Download Terraform Ubuntu 20.04

Next, unzip the Terraform file to the /usr/local/bin directory

unzip and install Terraform Ubuntu 20.04
unzip and install Terraform Ubuntu 20.04

To confirm the version of Terraform installed, run the command:

Check Terraform version
Check Terraform version

Wonderful. Terraform is now installed on our system. Next, we are going to see how to deploy a virtual server, commonly referred to as an ec2 instance on AWS.

Step 2: Create an EC2 instance using Terraform on AWS

To get started, first create a directory from which you are going to use to deploy the AWS ec2 instance and navigate into it.

Next, create the Terraform configuration file.

Inside the configuration file, we are going to define the following crucial parameters:

  1. The name of the cloud provider – Here, we go this AWS.
  2. Access and Secret keys – These are confidential keys specific to your AWS that allow you to make API calls.
  3. Region – the location where the cloud infrastructure will be provisioned. In our case, this will be in us-east-2.
  4. The ami – This is a unique ID required to deploy an EC2 instance.

The configuration file takes the following format.

#Specify the access & secret keys and region provider “aws” { access_key = “Your-own-access-key” secret_key = “Your-own-secret-key” region = “us-east-2” }

#Specify details of the ec2 instance  resource “aws_instance” “instance1” { ami = “ami-id” instance_type = “t2.micro” tags = { Name = “Amazon-Linux” } }

Terraform config file
Terraform config file

To begin using Terraform commands, you need to first initialize Terraform as shown.

terraform init

To execute a simulated run of the steps Terraform will perform to deploy the ec2 instance, run the command:

This only a simulation or a test run that gives you a glimpse of how the ec2 instance will be deployed.

To deploy the ec2 instance, execute:

The command actually implements the actions in the terraform plan command and starts provisioning the ec2 instance based on the provided details in the configuration file.

Create an EC2 instance using Terraform

You will be prompted to type ‘Yes‘ in order to continue.

Create an EC2 instance using Terraform

In just a matter of seconds, you instance will be ready!

Create an EC2 instance using Terraform

To confirm this, head over to your AWS account under the ec2 instances section.

And this draws the curtain on our guide today. We hope it has provided insights on how you can deploy an ec2 instance on AWS using Terraform.

Continue Reading
Advertisement

Trending