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
}
}
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"
}
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"
}
}
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"
}
}
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"
}
}
Create Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "my-igw"
}
}
Create Route Table for Public Subnet
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "public-route-table"
}
}
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
}
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
}
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"
}
}
4. Outputs
Output VPC ID
output "vpc_id" {
value = aws_vpc.my_vpc.id
}
Output Public Subnet ID
output "public_subnet_id" {
value = aws_subnet.public_subnet.id
}
Output EC2 Instance Public IP
output "instance_public_ip" {
value = aws_instance.app_server_irlan.public_ip
}
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
Step 2: Review the Terraform Plan
Verify the resources that will be created:
terraform plan
Step 3: Deploy the Resources
Apply the Terraform script to provision resources on AWS:
terraform apply
Type yes
when prompted to confirm the deployment.
5. Verify the Deployment
After applying the script:
- Check the AWS Management Console for the created VPC, subnets, and EC2 instance.
- Use the outputs printed to access the public subnet ID, EC2 instance public IP, and VPC ID.
- To get outputs again at any time, run:
terraform output
6. Clean Up
To destroy all resources created by Terraform:
terraform destroy
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)