public_subnets.tf 1.2 KB

12345678910111213141516171819202122232425262728
  1. resource "aws_subnet" "public" {
  2. count = length(var.vpc-azs)
  3. vpc_id = aws_vpc.vpc.id
  4. cidr_block = cidrsubnet(var.vpc-cidr, length(var.vpc-azs) * 2, count.index)
  5. availability_zone = var.vpc-azs[count.index]
  6. map_public_ip_on_launch = var.map-public-ip-on-launch
  7. tags = merge({ Name = "${var.vpc-name}-public-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  8. }
  9. resource "aws_route" "public_internet_gateway" {
  10. route_table_id = element(aws_route_table.public_route_table.*.id, count.index)
  11. destination_cidr_block = "0.0.0.0/0"
  12. gateway_id = aws_internet_gateway.vpc.id
  13. count = length(var.vpc-azs)
  14. }
  15. resource "aws_route_table_association" "public" {
  16. count = length(var.vpc-azs)
  17. subnet_id = element(aws_subnet.public.*.id, count.index)
  18. route_table_id = element(aws_route_table.public_route_table.*.id, count.index)
  19. }
  20. resource "aws_route_table" "public_route_table" {
  21. count = length(var.vpc-azs)
  22. vpc_id = aws_vpc.vpc.id
  23. tags = merge({ Name = "${var.vpc-name}-public-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  24. }