TerraformPilot

AWS

How to Use Terraform with AWS - Complete Setup Guide for Beginners

Set up Terraform with AWS from scratch. Covers AWS CLI configuration, provider setup, IAM permissions, and your first EC2 instance deployment step by step.

LLuca Berton1 min read

Introduction

#

Getting started with Terraform on AWS is one of the most common entry points for infrastructure as code. This guide walks you through the complete setup process, from AWS CLI configuration to deploying your first EC2 instance.

Prerequisites

#

Step 1 - Configure AWS Credentials

#

Create an IAM User

#
  1. Go to AWS Console > IAM > Users
  2. Click "Create User"
  3. Name: terraform-admin
  4. Attach policy: AdministratorAccess (for learning; restrict in production)
  5. Create access key (CLI type)

Configure AWS CLI

#
aws configure
# AWS Access Key ID: AKIA...
# AWS Secret Access Key: xxxx...
# Default region: us-east-1
# Default output format: json

Verify Access

#
aws sts get-caller-identity

Step 2 - Create Your First Terraform Config

#

Project Setup

#
mkdir my-first-terraform && cd my-first-terraform

providers.tf

#
terraform {
  required_version = ">= 1.5"
  
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
 
provider "aws" {
  region = "us-east-1"
}

main.tf

#
# Get the latest Amazon Linux 2 AMI
data "aws_ami" "amazon_linux" {
  most_recent = true
  owners      = ["amazon"]
 
  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }
}
 
# Create a security group
resource "aws_security_group" "web" {
  name        = "web-server-sg"
  description = "Allow HTTP and SSH"
 
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
 
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Restrict in production!
  }
 
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
 
# Launch an EC2 instance
resource "aws_instance" "web" {
  ami                    = data.aws_ami.amazon_linux.id
  instance_type          = "t2.micro"  # Free tier eligible
  vpc_security_group_ids = [aws_security_group.web.id]
 
  user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
    echo "Hello from Terraform!" > /var/www/html/index.html
  EOF
 
  tags = {
    Name = "terraform-web-server"
  }
}

outputs.tf

#
output "instance_id" {
  value = aws_instance.web.id
}
 
output "public_ip" {
  value = aws_instance.web.public_ip
}
 
output "public_dns" {
  value = aws_instance.web.public_dns
}

Step 3 - Deploy

#
# Initialize Terraform
terraform init
 
# Preview changes
terraform plan
 
# Apply changes
terraform apply
# Type "yes" when prompted
 
# View outputs
terraform output

Step 4 - Verify

#
# Check the instance
curl http://$(terraform output -raw public_ip)
# Output: Hello from Terraform!

Step 5 - Clean Up

#
# Destroy all resources (stop billing)
terraform destroy
# Type "yes" when prompted

Next Steps

#

After your first deployment:

  1. Add remote state with S3 backend
  2. Use variables for reusability
  3. Create modules for common patterns
  4. Set up CI/CD with GitLab or GitHub Actions
  5. Explore more AWS services (VPC, RDS, ECS)

Hands-On Courses

#

Conclusion

#

You've just deployed your first AWS infrastructure with Terraform. This is the foundation for managing everything from simple web servers to complex multi-service architectures. Keep exploring and automating!

#Terraform#AWS#Infrastructure as Code#DevOps

Share this article