Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 50 additions & 41 deletions scripts/aws/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,84 @@ provider "aws" {
region = var.aws_region
}

# VPC
data "aws_availability_zones" "available" {
state = "available"
}

resource "aws_vpc" "custom_vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = var.vpc_name
Owner = var.vpc_owner
}
tags = merge(
var.common_tags,
{
Name = var.vpc_name
Owner = var.vpc_owner
}
)
}

# Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.custom_vpc.id

tags = {
Name = "${var.vpc_name}-igw"
Owner = var.vpc_owner
}
tags = merge(
var.common_tags,
{
Name = "${var.vpc_name}-igw"
Owner = var.vpc_owner
}
)
}

# Public Subnet 1
resource "aws_subnet" "public_subnet_1" {
vpc_id = aws_vpc.custom_vpc.id
cidr_block = var.public_subnet_cidr_1
availability_zone = var.public_subnet_az_1
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = true

tags = {
Name = "${var.vpc_name}-public-subnet-1"
Owner = var.vpc_owner
}
tags = merge(
var.common_tags,
{
Name = "${var.vpc_name}-public-subnet-1"
Owner = var.vpc_owner
}
)
}

# Public Subnet 2
resource "aws_subnet" "public_subnet_2" {
vpc_id = aws_vpc.custom_vpc.id
cidr_block = var.public_subnet_cidr_2
availability_zone = var.public_subnet_az_2
availability_zone = data.aws_availability_zones.available.names[1]
map_public_ip_on_launch = true

tags = {
Name = "${var.vpc_name}-public-subnet-2"
Owner = var.vpc_owner
}
tags = merge(
var.common_tags,
{
Name = "${var.vpc_name}-public-subnet-2"
Owner = var.vpc_owner
}
)
}

# Public Route Table for Routing to Internet Gateway
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.custom_vpc.id

tags = {
Name = "${var.vpc_name}-public-rt"
Owner = var.vpc_owner
}
tags = merge(
var.common_tags,
{
Name = "${var.vpc_name}-public-rt"
Owner = var.vpc_owner
}
)
}

# Route for Internet Access
resource "aws_route" "public_internet_access" {
route_table_id = aws_route_table.public_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
depends_on = [aws_internet_gateway.igw]
}

# Associate Route Table with Public Subnets
resource "aws_route_table_association" "public_assoc_1" {
subnet_id = aws_subnet.public_subnet_1.id
route_table_id = aws_route_table.public_rt.id
Expand All @@ -79,7 +90,6 @@ resource "aws_route_table_association" "public_assoc_2" {
route_table_id = aws_route_table.public_rt.id
}

# Security Group for SSH Access
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH access from anywhere"
Expand All @@ -89,29 +99,30 @@ resource "aws_security_group" "allow_ssh" {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allow SSH from anywhere
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1" # Allow all outbound traffic
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "Allow SSH"
Owner = var.vpc_owner
}
tags = merge(
var.common_tags,
{
Name = "Allow SSH"
Owner = var.vpc_owner
}
)
}

# Output the VPC ID
output "vpc_id" {
description = "The ID of the custom VPC"
value = aws_vpc.custom_vpc.id
}

# Output the Public Subnets IDs
output "public_subnet_id_1" {
description = "The ID of the first public subnet"
value = aws_subnet.public_subnet_1.id
Expand All @@ -122,9 +133,7 @@ output "public_subnet_id_2" {
value = aws_subnet.public_subnet_2.id
}

# Output the Security Group ID
output "security_group_id" {
description = "The ID of the security group"
value = aws_security_group.allow_ssh.id
}

19 changes: 8 additions & 11 deletions scripts/aws/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ variable "public_subnet_cidr_2" {
default = "10.0.2.0/24"
}

variable "public_subnet_az_1" {
description = "Availability Zone for the first public subnet"
type = string
default = "ap-south-1a"
variable "common_tags" {
description = "Common tags for all resources"
type = map(string)
default = {
"app-code" = "OSUS-002"
"service-phase" = "dev"
"cost-center" = "136"
}
}

variable "public_subnet_az_2" {
description = "Availability Zone for the second public subnet"
type = string
default = "ap-south-1b"
}