|
| 1 | +resource "aws_vpc" "vpc" { |
| 2 | + cidr_block = var.vpc_cidr |
| 3 | + enable_dns_support = var.vpc_enable_dns_support |
| 4 | + enable_dns_hostnames = var.vpc_enable_dns_hostnames |
| 5 | + tags = merge(var.vpc_tags, tomap({"Name" = var.short_env})) |
| 6 | +} |
| 7 | + |
| 8 | +# Internet-gateway |
| 9 | +resource "aws_internet_gateway" "igw" { |
| 10 | + vpc_id = aws_vpc.vpc.id |
| 11 | + tags = merge(var.vpc_tags, tomap({"Name" = var.short_env})) |
| 12 | +} |
| 13 | + |
| 14 | +#EIP for Nat attaching |
| 15 | +resource "aws_eip" "nat" { |
| 16 | + domain = "vpc" |
| 17 | + tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-nat"})) |
| 18 | +} |
| 19 | + |
| 20 | +#Nat gateway setup |
| 21 | +resource "aws_nat_gateway" "nat-gw" { |
| 22 | + allocation_id = aws_eip.nat.id |
| 23 | + subnet_id = aws_subnet.public[0].id |
| 24 | + tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}"})) |
| 25 | +} |
| 26 | + |
| 27 | +resource "aws_subnet" "public" { |
| 28 | + count = length(var.public_subnets) |
| 29 | + cidr_block = var.public_subnets[count.index].CIDR_BLOCK |
| 30 | + vpc_id = aws_vpc.vpc.id |
| 31 | + availability_zone = var.public_subnets[count.index].AVAILABILITY_ZONE |
| 32 | + map_public_ip_on_launch = true |
| 33 | + tags = merge(var.vpc_tags, tomap({"Name" = var.public_subnets[count.index].NAME})) |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_subnet" "private" { |
| 37 | + count = length(var.private_subnets) |
| 38 | + cidr_block = var.private_subnets[count.index].CIDR_BLOCK |
| 39 | + vpc_id = aws_vpc.vpc.id |
| 40 | + availability_zone = var.private_subnets[count.index].AVAILABILITY_ZONE |
| 41 | + tags = merge(var.vpc_tags, tomap({"Name" = var.private_subnets[count.index].NAME})) |
| 42 | +} |
| 43 | + |
| 44 | +resource "aws_security_group" "default" { |
| 45 | + name = "${var.short_env}-default" |
| 46 | + description = "Default SG to alllow traffic from the VPC" |
| 47 | + vpc_id = aws_vpc.vpc.id |
| 48 | + depends_on = [ |
| 49 | + aws_vpc.vpc |
| 50 | + ] |
| 51 | + |
| 52 | + ingress { |
| 53 | + from_port = "0" |
| 54 | + to_port = "0" |
| 55 | + protocol = "-1" |
| 56 | + self = true |
| 57 | + } |
| 58 | + |
| 59 | + egress { |
| 60 | + from_port = "0" |
| 61 | + to_port = "0" |
| 62 | + protocol = "-1" |
| 63 | + cidr_blocks = ["0.0.0.0/0"] |
| 64 | + } |
| 65 | + |
| 66 | + tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-default"})) |
| 67 | +} |
0 commit comments