Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ec2-user@DESKTOP-Q259SHJ ~/count/vpc-module (default)$ cat main.tf
- # 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_enviornment}"
- }
- }
- #Creating IGW
- resource "aws_internet_gateway" "igw" {
- vpc_id = aws_vpc.main.id
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-vpc"
- }
- }
- #Creating public subnet
- resource "aws_subnet" "public" {
- count = 3
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index)
- vpc_id = aws_vpc.main.id
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = true
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-public-${count.index}"
- }
- }
- #Creating private subnets
- 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}"]
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-private-${count.index}"
- }
- }
- # Creating EIP allocation
- resource "aws_eip" "ip" {
- count = var.natgw == true ? 1 : 0
- vpc = true
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-natip"
- }
- }
- # Creating NAT G/W
- resource "aws_nat_gateway" "example" {
- count = var.natgw == true ? 1 : 0
- allocation_id = aws_eip.ip[0].id
- subnet_id = aws_subnet.public[1].id
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-natgw"
- }
- depends_on = [aws_internet_gateway.igw]
- }
- # Creating Public 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_enviornment}-public"
- }
- }
- # Creating private route table
- resource "aws_route_table" "private_true" {
- count = var.natgw == true ? 1 : 0
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- nat_gateway_id = aws_nat_gateway.example[0].id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-private"
- }
- }
- resource "aws_route_table" "private_false" {
- count = var.natgw == false ? 1 : 0
- vpc_id = aws_vpc.main.id
- tags = {
- "Name" = "${var.project_name}-${var.project_enviornment}-private"
- }
- }
- # public route table associations
- resource "aws_route_table_association" "public" {
- count = 3
- subnet_id = aws_subnet.public[count.index].id
- route_table_id = aws_route_table.public.id
- }
- # Private route table associations
- resource "aws_route_table_association" "private_true" {
- count = var.natgw == true ? 3 : 0
- subnet_id = aws_subnet.private[count.index].id
- route_table_id = aws_route_table.private_true[0].id
- }
- resource "aws_route_table_association" "private_false" {
- count = var.natgw == false ? 3: 0
- subnet_id = aws_subnet.private[count.index].id
- route_table_id = aws_route_table.private_false[0].id
- }
Advertisement
Add Comment
Please, Sign In to add comment