DEV Community

Irlan Dos Santos
Irlan Dos Santos

Posted on

How to Deploy Resources on AWS Using Terraform

How to Deploy AWS Resources Using Terraform

1. Prerequisites

Before starting, ensure the following tools are installed and configured:

  • Terraform - Install Terraform
  • AWS CLI with configured Access KeyID and secret AccessKey.

Create a folder, exemple:my_test_deploy, open VS Code and open the folder you just created.
Create a new file and name it as main.tf, and paste the terraform code bellow.


2. Backend State Storage (Optional)

In a production environment, save your state file remotely. To enable S3 remote state storage, update the following block:

terraform {
  backend "s3" {
    bucket  = "your-s3-bucket-name"
    key     = "terraform/state.tfstate"
    region  = "us-east-1"
    encrypt = true
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Terraform Configuration

Required Providers and Versions

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

Create VPC

resource "aws_vpc" "my_vpc" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name = "my_vpc"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create Public Subnet

resource "aws_subnet" "public_subnet" {
  vpc_id                  = aws_vpc.my_vpc.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "us-east-1a"

  tags = {
    Name = "public-subnet"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create Private Subnet

resource "aws_subnet" "private_subnet" {
  vpc_id                  = aws_vpc.my_vpc.id
  cidr_block              = "10.0.2.0/24"
  availability_zone       = "us-east-1b"

  tags = {
    Name = "private-subnet"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create Internet Gateway

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.my_vpc.id

  tags = {
    Name = "my-igw"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create Route Table for Public Subnet

resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.my_vpc.id

  tags = {
    Name = "public-route-table"
  }
}
Enter fullscreen mode Exit fullscreen mode

Add Route to Internet Gateway

resource "aws_route" "public_internet_route" {
  route_table_id         = aws_route_table.public_rt.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.igw.id
}
Enter fullscreen mode Exit fullscreen mode

Associate Public Subnet with Route Table

resource "aws_route_table_association" "public_assoc" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.public_rt.id
}
Enter fullscreen mode Exit fullscreen mode

Launch an EC2 Instance

resource "aws_instance" "app_server_irlan" {
  ami           = "ami-01816d07b1128cd2d"   # Replace with the desired AMI ID
  instance_type = "t2.micro"

  tags = {
    Name = "app_server_irlan"
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Outputs

Output VPC ID

output "vpc_id" {
  value = aws_vpc.my_vpc.id
}
Enter fullscreen mode Exit fullscreen mode

Output Public Subnet ID

output "public_subnet_id" {
  value = aws_subnet.public_subnet.id
}
Enter fullscreen mode Exit fullscreen mode

Output EC2 Instance Public IP

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

Terraform Workflow

Open a terminal, and go to the folder you created earlier.

Step 1: Initialize Terraform

Run the following command to initialize the working directory and download the AWS provider plugin:

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 2: Review the Terraform Plan

Verify the resources that will be created:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Step 3: Deploy the Resources

Apply the Terraform script to provision resources on AWS:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Type yes when prompted to confirm the deployment.


5. Verify the Deployment

After applying the script:

  1. Check the AWS Management Console for the created VPC, subnets, and EC2 instance.
  2. Use the outputs printed to access the public subnet ID, EC2 instance public IP, and VPC ID.
  3. To get outputs again at any time, run:
terraform output
Enter fullscreen mode Exit fullscreen mode

6. Clean Up

To destroy all resources created by Terraform:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Confirm by typing yes when prompted.


Conclusion

Using Terraform to deploy infrastructure on AWS provides a scalable and consistent approach. This script automates the creation of a VPC, public and private subnets, an Internet Gateway, and an EC2 instance. You can modify or extend it further to include security groups, load balancers, or RDS instances as needed.

Top comments (0)