Last active
April 20, 2024 09:35
-
-
Save sheikhnavezz/92c4f827360e9ff401946e17e9d09319 to your computer and use it in GitHub Desktop.
With the help of this Terraform code gist you can create vpc with subnet, route table. rtb association, igw in us-east-2 (Ohio) region.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform_project/ | |
│ | |
├── vpc/ | |
│ ├── main.tf | |
│ ├── variables.tf | |
│ └── outputs.tf | |
│ | |
└── main.tf | |
└── provider.tf | |
vpc/main.tf : | |
# Configure the AWS provider | |
provider "aws" { | |
region = var.region | |
} | |
# Create a VPC | |
resource "aws_vpc" "my_vpc" { | |
cidr_block = var.vpc_cidr_block | |
enable_dns_support = true | |
enable_dns_hostnames = true | |
tags = { | |
Name = var.vpc_name | |
} | |
} | |
# Create a subnet within the VPC | |
resource "aws_subnet" "my_subnet" { | |
vpc_id = aws_vpc.my_vpc.id | |
cidr_block = var.subnet_cidr_block | |
availability_zone = var.availability_zone | |
map_public_ip_on_launch = var.map_public_ip_on_launch | |
tags = { | |
Name = var.subnet_name | |
} | |
} | |
# Create an internet gateway | |
resource "aws_internet_gateway" "my_igw" { | |
vpc_id = aws_vpc.my_vpc.id | |
tags = { | |
Name = var.igw_name | |
} | |
} | |
# Create a route table | |
resource "aws_route_table" "my_route_table" { | |
vpc_id = aws_vpc.my_vpc.id | |
route { | |
cidr_block = "0.0.0.0/0" | |
gateway_id = aws_internet_gateway.my_igw.id | |
} | |
tags = { | |
Name = var.route_table_name | |
} | |
} | |
# Associate the route table with the subnet | |
resource "aws_route_table_association" "my_subnet_association" { | |
subnet_id = aws_subnet.my_subnet.id | |
route_table_id = aws_route_table.my_route_table.id | |
} | |
vpc/outputs.tf : | |
output "vpc_id" { | |
value = aws_vpc.my_vpc.id | |
} | |
output "subnet_id" { | |
value = aws_subnet.my_subnet.id | |
} | |
vpc/variables.tf : | |
variable "region" { | |
description = "The AWS region." | |
type = string | |
} | |
variable "vpc_cidr_block" { | |
description = "The CIDR block for the VPC." | |
type = string | |
} | |
variable "vpc_name" { | |
description = "The name tag for the VPC." | |
type = string | |
} | |
variable "subnet_cidr_block" { | |
description = "The CIDR block for the subnet." | |
type = string | |
} | |
variable "availability_zone" { | |
description = "The availability zone for the subnet." | |
type = string | |
} | |
variable "map_public_ip_on_launch" { | |
description = "Boolean flag to map public IP on launch." | |
type = bool | |
} | |
variable "subnet_name" { | |
description = "The name tag for the subnet." | |
type = string | |
} | |
variable "igw_name" { | |
description = "The name tag for the internet gateway." | |
type = string | |
} | |
variable "route_table_name" { | |
description = "The name tag for the route table." | |
type = string | |
} | |
in the root folder: | |
main.tf : | |
# Use the VPC module | |
module "my_vpc" { | |
source = "./vpc" | |
region = "us-east-2" | |
vpc_cidr_block = "10.0.0.0/16" | |
vpc_name = "MyVPC" | |
subnet_cidr_block = "10.0.1.0/24" | |
availability_zone = "us-east-2a" | |
map_public_ip_on_launch = true | |
subnet_name = "MySubnet" | |
igw_name = "MyIGW" | |
route_table_name = "MyRouteTable" | |
} | |
provider.tf : | |
provider "aws" { | |
region = "us-east-2" # Specify your desired AWS region | |
profile = "tf-navez" | |
} | |
terraform init | |
terraform fmt | |
terraform plan | |
terraform apply | |
terraform destroy | |
Hence, you have successfully created AWS VPC with IGW, Route table, Route table association, Subnets. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment