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:
1 |
$ sudo apt update |
Next, install the prerequisite packages that will be required during the installation of Terraform:
1 |
$ sudo apt install unzip wget |
Next, download the Terraform zip file as shown.
1 |
$ sudo wget https://releases.hashicorp.com/terraform/0.15.3/terraform_0.15.3_linux_amd64.zip |
Next, unzip the Terraform file to the /usr/local/bin
directory
1 |
$ sudo unzip terraform_0.15.3_linux_amd64.zip -d /usr/local/bin |
To confirm the version of Terraform installed, run the command:
1 |
$ 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.
1 |
$ mkdir aws_dir && cd aws_dir |
Next, create the Terraform configuration file.
1 |
$ sudo vim config.tf |
Inside the configuration file, we are going to define the following crucial parameters:
- The name of the cloud provider – Here, we go this AWS.
- Access and Secret keys – These are confidential keys specific to your AWS that allow you to make API calls.
- Region – the location where the cloud infrastructure will be provisioned. In our case, this will be in us-east-2.
- 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” } }
To begin using Terraform commands, you need to first initialize Terraform as shown.
1 |
$ terraform init |
To execute a simulated run of the steps Terraform will perform to deploy the ec2 instance, run the command:
1 |
$ terraform plan |
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:
1 |
$ terraform apply |
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.
You will be prompted to type ‘Yes‘ in order to continue.
In just a matter of seconds, you instance will be ready!
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.
-
DevOps55 years ago
Saltstack Tutorial for beginners [2023]
-
DevOps55 years ago
How to build a Docker cron job Container easily [2023]
-
Linux55 years ago
mail Command in Linux/Unix with 10+ Examples [2023]
-
DevOps55 years ago
Docker ADD vs COPY vs VOLUME – [2023]
-
DevOps55 years ago
How to setup Pritunl VPN on AWS to Access Servers
-
Linux55 years ago
Grep Command In Unix/Linux with 25+ Examples [2023]
-
Linux55 years ago
How To setup Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04
-
Linux55 years ago
Find command in Unix/Linux with 30+ Examples [2023]