AWS VPC Setup with Terraform!
Bhupendra Maurya
Software Developer | Web Developer | React | Angular | JavaScript | Typescript | Node | AWS | HTML/CSS | Passionate about solving problems and constantly learning to enhance my skills in full-stack development
?? I'm excited to share a recent project where I leveraged Terraform to automate the creation of a secure and scalable AWS infrastructure. Here’s what we accomplished:
?? Created a VPC: Set up a Virtual Private Cloud (VPC) to isolate our AWS resources.
?? Subnets: Defined both public and private subnets within the VPC for better resource segmentation.
?? Internet Gateway: Attached an Internet Gateway to the VPC, allowing internet access to instances in the public subnet.
?? Routing: Configured route tables to manage traffic flow between subnets and the internet.
?? Security Groups: Established security groups to control inbound and outbound traffic, ensuring our instances are secure.
?? EC2 Instances: Launched EC2 instances in both public and private subnets, each with appropriate security settings.
Why Terraform?
Using Terraform allowed us to:
- Automate Infrastructure Provisioning: No more manual setups! Infrastructure as Code (IaC) ensures consistency and repeatability.
领英推荐
- Version Control: Keep track of changes with version control, making rollbacks and audits straightforward.
- Scalability: Easily scale the infrastructure as needed by adjusting the configuration files.
Key Takeaways:
- Efficiency: Automating the setup reduced the deployment time significantly.
- Security: Proper configuration of security groups and subnets ensures a secure environment.
- Flexibility: The infrastructure is now flexible and can be easily modified to meet future needs.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.50.0"
}
}
}
provider "aws" {
region = "us-east-2"
}
locals {
dev_env = "dev"
}
# creating VPC
resource "aws_vpc" "dev_vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "${local.dev_env}-vpc"
}
}
# create subnets
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.dev_vpc.id
cidr_block = "10.0.10.0/24"
tags = {
Name = "${local.dev_env}-public-subnet"
}
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.dev_vpc.id
cidr_block = "10.0.20.0/24"
tags = {
Name = "${local.dev_env}-private-subnet"
}
}
# attach the VPC to IGW
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.dev_vpc.id
tags = {
Name = "${local.dev_env}-igw"
}
}
# public route table
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.dev_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "${local.dev_env}-public-rt"
}
}
# associate public subnet with public route table
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_rt.id
}
# private route table (no need to add a route as it's private)
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.dev_vpc.id
tags = {
Name = "${local.dev_env}-private-rt"
}
}
# associate private subnet with private route table
resource "aws_route_table_association" "private_association" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.private_rt.id
}
# create security group for public instance
resource "aws_security_group" "public_sg" {
vpc_id = aws_vpc.dev_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${local.dev_env}-public-sg"
}
}
# create security group for private instance
resource "aws_security_group" "private_sg" {
vpc_id = aws_vpc.dev_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${local.dev_env}-private-sg"
}
}
# create EC2 instances
resource "aws_instance" "public_instance" {
ami = "ami-02bf8ce06a8ed6092" # Replace with your desired AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnet.id
security_groups = [aws_security_group.public_sg.id]
tags = {
Name = "${local.dev_env}-public-instance"
}
}
resource "aws_instance" "private_instance" {
ami = "ami-02bf8ce06a8ed6092" # Replace with your desired AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.private_subnet.id
security_groups = [aws_security_group.private_sg.id]
tags = {
Name = "${local.dev_env}-private-instance"
}
}
#AWS #Terraform #CloudInfrastructure #DevOps #Automation #InfrastructureAsCode #IaaS #CloudComputing