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

Published: (December 2, 2025 at 01:20 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Cover image for 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:

  1. Install Terraform
  2. Configure AWS access
  3. Create a Terraform project
  4. Deploy AWS infrastructure

Step 1 – Install Terraform on Ubuntu

sudo apt update
sudo apt install -y wget unzip
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   # verify installation

Step 2 – Install AWS CLI

sudo apt install -y awscli
aws --version   # verify installation

Step 3 – Configure AWS Credentials

aws configure

You will be prompted for:

Enter AWS Access Key ID     : 
Enter AWS Secret Access Key : 
Default region name         : ap-south-1
Default output format       : json

Note: Create or retrieve access keys in the AWS Console → IAM → Users → Your User → Security credentials → Access keys → Create access key. Choose a region that matches the nearest data center.

Step 4 – Create Terraform Project Structure

mkdir -p terraform_demo/terraform
cd terraform_demo/terraform

Folder layout:

terraform_demo/
└── terraform/
    ├── provider.tf
    ├── main.tf
    ├── variables.tf
    ├── terraform.tfvars
    └── outputs.tf

Step 5 – Write Terraform Files

provider.tf

provider "aws" {
  region = var.region
}

variables.tf

variable "region" {}
variable "instance_type" {}
variable "key_name" {}

terraform.tfvars

region        = "ap-south-1"
instance_type = "t2.micro"
key_name      = "nagios"

main.tf

resource "aws_instance" "web" {
  ami           = "ami-0a0f1259dd1c90938"
  instance_type = var.instance_type
  key_name      = var.key_name

  tags = {
    Name = "Terraform-Web"
  }
}

Finding the correct AMI
For the 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 the 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

Select the appropriate AMI ID and replace the value in main.tf.

outputs.tf

output "public_ip" {
  value = aws_instance.web.public_ip
}

Step 6 – Initialize Terraform

terraform init

Step 7 – Generate an Execution Plan

terraform plan

Terraform will display the resources it intends to create.

Step 8 – Apply & Create Infrastructure

terraform apply

Type yes when prompted. After the apply completes, the output will display the public IP of the EC2 instance.


🎉 Terraform Setup Completed!
Your AWS infrastructure is now deployed automatically using Terraform.

Back to Blog

Related posts

Read more »

AWS Terraform Lifecycle Rules

Introduction Infrastructure as Code IaC is most powerful when you have full control over how resources behave during updates, replacements, and deletions. Terr...

Terraform Project: Simple EC2 + Security Group

Project Structure terraform-project/ │── main.tf │── variables.tf │── outputs.tf │── providers.tf │── terraform.tfvars │── modules/ │ └── ec2/ │ ├── main.tf │...

Saving Terraform State in S3

Configuring S3 as a Terraform Backend Terraform can store its state in an S3 bucket. Below is a minimal configuration that sets up the S3 backend: hcl terrafor...