Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Fetaching the details of the availablity zones
- data "aws_availability_zones" "az" {
- state = "available"
- }
- # creating VPC
- resource "aws_vpc" "main" {
- cidr_block = var.vpc_cidr
- instance_tenancy = "default"
- enable_dns_support = true
- enable_dns_hostnames = true
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- #Creating IGW
- resource "aws_internet_gateway" "igw" {
- vpc_id = aws_vpc.main.id
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- # Creating subnets
- resource "aws_subnet" "public" {
- count = 3
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
- }
- }
- resource "aws_subnet" "private" {
- count = 3
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = false
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
- }
- }
- # creating elastic IP for NAT G/W
- resource "aws_eip" "ip" {
- vpc = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-"
- }
- }
- # creating NAT gateway
- resource "aws_nat_gateway" "nat" {
- allocation_id = aws_eip.ip.id
- subnet_id = aws_subnet.public[0].id
- tags = {
- "Name" = "${var.project_name}-${var.project_env}"
- }
- depends_on = [aws_internet_gateway.igw]
- }
- # Creating Route table
- resource "aws_route_table" "public" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- gateway_id = aws_internet_gateway.igw.id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public"
- }
- }
- resource "aws_route_table" "private" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- nat_gateway_id = aws_nat_gateway.nat.id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private"
- }
- }
- #Association with the route table
- resource "aws_route_table_association" "public" {
- count = 3
- subnet_id = aws_subnet.public["${count.index}"].id
- route_table_id = aws_route_table.public.id
- }
- resource "aws_route_table_association" "private" {
- count = 3
- subnet_id = aws_subnet.private["${count.index}"].id
- route_table_id = aws_route_table.private.id
- }
Advertisement
Add Comment
Please, Sign In to add comment