private_subnets.tf 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. resource "aws_subnet" "private" {
  2. count = local.count_private
  3. vpc_id = aws_vpc.vpc.id
  4. cidr_block = cidrsubnet(var.vpc-cidr, length(var.vpc-azs) * 2, count.index + length(var.vpc-azs))
  5. availability_zone = var.vpc-azs[count.index]
  6. tags = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  7. }
  8. resource "aws_eip" "private-nat-eip" {
  9. count = local.count_private
  10. vpc = true
  11. tags = merge({ Name = "${var.vpc-name}-nat-eip-${count.index}" }, var.common-tags)
  12. depends_on = [aws_internet_gateway.vpc]
  13. }
  14. resource "aws_nat_gateway" "private" {
  15. count = local.count_private
  16. allocation_id = element(aws_eip.private-nat-eip.*.id, count.index)
  17. subnet_id = element(aws_subnet.public.*.id, count.index)
  18. depends_on = [aws_internet_gateway.vpc]
  19. tags = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  20. }
  21. resource "aws_route" "nat_gateway" {
  22. count = local.count_private
  23. route_table_id = element(aws_route_table.private.*.id, count.index)
  24. destination_cidr_block = "0.0.0.0/0"
  25. nat_gateway_id = element(aws_nat_gateway.private.*.id, count.index)
  26. }
  27. resource "aws_route_table" "private" {
  28. count = local.count_private
  29. vpc_id = aws_vpc.vpc.id
  30. tags = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  31. }