Logo

dev-resources.site

for different kinds of informations.

How to Build a Simple AWS Test Environment with Terraform

Published at
1/8/2025
Categories
tutorial
terraform
aws
ec2
Author
jonbatista
Categories
4 categories in total
tutorial
open
terraform
open
aws
open
ec2
open
Author
10 person written this
jonbatista
open
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!

ec2 Article's
30 articles in total
Favicon
Power Up Your AWS Game: Create EC2 Instances, Install Apache, and Connect with PowerShell
Favicon
Introducing vulne-soldier: A Modern AWS EC2 Vulnerability Remediation Tool
Favicon
Tensorflow on AWS
Favicon
Forward logs to Cloudwatch for an EC2 instance running a custom Linux AMI
Favicon
How to Provision an Ubuntu Server & Using Apache for Hosting a Website
Favicon
EC2 instances with pre-configured EFS (elastic file system ) using Terraform modules
Favicon
Understanding EC2 in AWS - Day 1
Favicon
Identifying EBS Volumes and Mount Points with lsblk
Favicon
Creating an EC2 Instance on AWS and Installing IIS server on it
Favicon
Short: User Data file for Ubuntu based AWS ec2 instance with docker and docker compose.
Favicon
AWS VPC with Public and Private Subnets & NAT Gateway
Favicon
Terraform in AWS | Provision EC2 with AWS Systems Manager SSM access
Favicon
How to Build a Simple AWS Test Environment with Terraform
Favicon
How to Simulate High CPU Usage on AWS Ubuntu Instances for Testing and Performance Optimization
Favicon
šŸš€ EBS Volumes for EC2: Should You Use Multiple Small Volumes or One Large Volume? šŸ’”
Favicon
Run vs code on a private AWS ec2 instance without ssh (with AWS CDK examples)
Favicon
Understand Amazon Elastic Compute Cloud (EC2) for launching virtual machines
Favicon
Deploy Vite-React Project in AWS EC2 using custom domain and free SSL Certificate.
Favicon
AWS Network Fundamentals for EC2 instance!
Favicon
Connecting to an EC2 Instance with Ubuntu and Installing NGINX on AWS
Favicon
What Are the Key Differences Between AWS EC2 and AWS Lambda?
Favicon
Deploy your Discord Bot using Amazon EC2
Favicon
AWS Elastic Compute Cloud (EC2)
Favicon
Securing Your AWS EC2 and S3 Communication: Best Practices for Enhanced Security
Favicon
How to install an iis web server on Amazon Ec2 instance
Favicon
Creating a Windows EC2 Instance on AWS and Installing IIS Server on it
Favicon
Detached EBS volume from your local Linux system.
Favicon
AI Model Optimization on AWS Inferentia and Trainium
Favicon
How to Attach, Modify, and Increase an AWS EC2 EBS Volume from Your Local Linux Machine.
Favicon
How to Deploy a Flask App on an AWS EC2 Instance: A Step-by-Step Guide

Featured ones: