DEV Community

Cover image for Your First Mini Terraform Project: Install, Configure, and Deploy on AWS
Krisha Arya
Krisha Arya

Posted on

Your First Mini Terraform Project: Install, Configure, and Deploy on AWS

🌟 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

You should now see Terraform version.

Step 2: Install AWS CLI

sudo apt install -y awscli
aws --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure AWS Credentials

aws configure
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

In this file, we define which provider we are going to use.

variables.tf

variable "region" {}
variable "instance_type" {}
variable "key_name" {}
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then after, select latest one!

outputs.tf

output "public_ip" {
  value = aws_instance.web.public_ip
}
Enter fullscreen mode Exit fullscreen mode

This file saves the output we get after running "terraform apply" command.

Step 6: Initialize Terraform

cd ~/terraform_demo/terraform
terraform init
Enter fullscreen mode Exit fullscreen mode

Step 7: Generate an Execution Plan

terraform plan
Enter fullscreen mode Exit fullscreen mode

It will show what Terraform is going to create.

Step 8: Apply & Create Infrastructure

terraform apply
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
6085_prince_tripathi_54c8 profile image
6085_Prince Tripathi

Nicely explained. It works. Thank you 👍🏻