DEV Community

Cover image for How to Build a Simple AWS Test Environment with Terraform
Jon Batista
Jon Batista

Posted on

How to Build a Simple AWS Test Environment with Terraform

As a developer, sometimes you need to create a real environment to test your stuff. It can take a quite amount of your precious time. So if you want to create a cost-effective, manageable test environment, Terraform is the best choice. This simple article walks you through creating this kind of test environment on AWS that you can quickly launch or destroy, ensuring you avoid unexpected costs.

Think of it as a starting point and add the things you need to customize your environment. Use an LLM to help you create your own Terraform template. Iā€™m sure it will save a lot of time.

Prerequisites

This is a straight-to-the-point tutorial, but before diving in, ensure you have:

  • An AWS account.
  • Terraform installed on your machine.
  • Basic understanding of Terraform and AWS concepts like VPC, subnets, and security groups.

If you don't know about these things, no worries. It's a simple configuration that can help you in your studies.

1. Provider Configuration

The first step is to create a Terraform file called main.tf. Then, configure the AWS provider to specify the region where your resources will be created:

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

Here, I'm using the us-east-1 region, but feel free to choose your preferred AWS region.

2. Variables for Flexibility

To make the setup flexible and reusable, we define variables for key configurations:

variable "vpc_cidr" {
  default = "10.0.0.0/16"
}

variable "public_subnet_cidr" {
  default = "10.0.1.0/24"
}

variable "private_subnet_cidr" {
  default = "10.0.2.0/24"
}

variable "instance_type" {
  default = "t2.micro"
}

variable "ami" {
  description = "Amazon Linux 2 AMI"
  default     = "ami-0c02fb55956c7d316"
}
Enter fullscreen mode Exit fullscreen mode

Using variables allows you to tweak the CIDR blocks, instance types, and AMI IDs without modifying the main configuration.

3. VPC Setup

The VPC (Virtual Private Cloud) is the backbone of your infrastructure:

resource "aws_vpc" "test_env_vpc" {
  cidr_block           = var.vpc_cidr
  enable_dns_support   = true
  enable_dns_hostnames = true
  tags = {
    Name = "test_env_vpc"
  }
}
Enter fullscreen mode Exit fullscreen mode

This creates a VPC with DNS support and hostnames enabled, which are essential for networking.

4. Subnets: Public and Private

We create both public and private subnets within the VPC:

resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.test_env_vpc.id
  cidr_block              = var.public_subnet_cidr
  map_public_ip_on_launch = true
  availability_zone       = "us-east-1a"
  tags = {
    Name = "test_env_public_subnet"
  }
}

resource "aws_subnet" "private" {
  vpc_id            = aws_vpc.test_env_vpc.id
  cidr_block        = var.private_subnet_cidr
  availability_zone = "us-east-1a"
  tags = {
    Name = "test_env_private_subnet"
  }
}
Enter fullscreen mode Exit fullscreen mode

The public subnet assigns public IPs for internet-facing instances, while the private subnet is reserved for internal resources.

5. Internet Gateway and Routing

To enable internet connectivity for resources in the public subnet:

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.test_env_vpc.id
  tags = {
    Name = "test_env_igw"
  }
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.test_env_vpc.id
  tags = {
    Name = "test_env_public_rt"
  }
}

resource "aws_route" "public_route" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.igw.id
}

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

This setup routes public traffic through the internet gateway.

7. Instance Deployment

Launch a public EC2 instance with Terraform:

resource "aws_instance" "public_instance" {
  ami           = var.ami
  instance_type = var.instance_type
  subnet_id     = aws_subnet.public.id
  associate_public_ip_address = true
  vpc_security_group_ids = [aws_security_group.public_sg.id]

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

This creates a t2.micro instance running Amazon Linux 2 with public internet access.

8. Outputs

To view key information, we define outputs:

output "vpc_id" {
  value = aws_vpc.test_env_vpc.id
}

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

These outputs make it easy to reference your resources post-deployment.

Wrapping Up

With this configuration, you have a simple, flexible AWS test environment that can be launched or destroyed in minutes. To launch it, run:

terraform init
terraform apply
Enter fullscreen mode Exit fullscreen mode

And to tear it down:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

By managing your infrastructure with Terraform, you can experiment confidently without worrying about unexpected costs.

I hope this guide can help you!

Top comments (0)