π What is Terraform?
Terraform by HashiCorp is an Infrastructure as Code (IaC) tool used to:
β Provision AWS / Azure / GCP resources
β Automate infrastructure
β Maintain repeatable, version-controlled deployments
β Destroy and recreate resources easily
It uses its own DSL called HCL (HashiCorp Configuration Language).
In this guide, we will:
- Install Terraform
- Configure AWS access
- Create a Terraform project
- Deploy AWS infrastructure
Step 1: Install Terraform on Ubuntu
sudo apt update
sudo apt install -y wget unzip
Download Terraform binary:
wget https://releases.hashicorp.com/terraform/1.6.3/terraform_1.6.3_linux_amd64.zip
unzip terraform_1.6.3_linux_amd64.zip
sudo mv terraform /usr/local/bin/
terraform -v
You should now see Terraform version.
Step 2: Install AWS CLI
sudo apt install -y awscli
aws --version
Step 3: Configure AWS Credentials
aws configure
You will be prompted:
Enter AWS Access Key ID : AKIAS4UYMELDA4JHKVP4
Enter AWS Secret Access Key : 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
Default region name : ap-south-1
Default output format : json
Note: You can get "AWS Access Key ID" and "AWS Secret Access Key" by following this path (either create or get if already created):
AWS Console β IAM β Users β Your User β Security Credentials β Access Keys β Create Access Key
Also, decide default region name according to nearest data center of your region.
Step 4: Create Terraform Project Structure
mkdir terraform_demo
cd terraform_demo
Your folder structure will be:
terraform_ansible_demo/
βββ terraform/
βββ provider.tf
βββ main.tf
βββ variables.tf
βββ terraform.tfvars
βββ outputs.tf
Step 5: Write Terraform Files
provider.tf
provider "aws" {
region = var.region
}
In this file, we define which provider we are going to use.
variables.tf
variable "region" {}
variable "instance_type" {}
variable "key_name" {}
In this file, we are going to define all variables which have been used in provider.tf and main.tf files.
terraform.tfvars
region = "ap-south-1"
instance_type = "t2.micro"
key_name = "nagios"
In this file, we are going to define actual value of variables which have been defined in variables.tf file.
main.tf
resource "aws_instance" "web" {
ami = "ami-0a0f1259dd1c90938"
instance_type = var.instance_type
key_name = var.key_name
tags = {
Name = "Terraform-Web"
}
}
This is the main code file where we define all setups or configurations we want for EC2 instances.
Note: For getting ami, you need to run below command to get correct ami of your selected region.
For latest Ubuntu AMI (preferred):
aws ec2 describe-images --owners canonical --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-*" --query 'Images[*].[ImageId,Name]' --output table
For latest Amazon Linux 2 AMI:
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-*-x86_64-gp2" --query 'Images[0].ImageId' --output text
Then after, select latest one!
outputs.tf
output "public_ip" {
value = aws_instance.web.public_ip
}
This file saves the output we get after running "terraform apply" command.
Step 6: Initialize Terraform
cd ~/terraform_demo/terraform
terraform init
Step 7: Generate an Execution Plan
terraform plan
It will show what Terraform is going to create.
Step 8: Apply & Create Infrastructure
terraform apply
Type yes.
You will get the public EC2 IP at the end.
π Terraform Setup Completed!
You now have infrastructure deployed fully automatically.
Top comments (1)
Nicely explained. It works. Thank you ππ»