private_subnets.tf 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. resource "aws_subnet" "private" {
  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 + 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 = "${length(var.vpc-azs)}"
  10. vpc = true
  11. depends_on = ["aws_internet_gateway.vpc"]
  12. }
  13. resource "aws_nat_gateway" "private" {
  14. count = "${length(var.vpc-azs)}"
  15. allocation_id = "${element(aws_eip.private-nat-eip.*.id, count.index)}"
  16. subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
  17. depends_on = ["aws_internet_gateway.vpc"]
  18. tags = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  19. }
  20. resource "aws_route" "nat_gateway" {
  21. count = "${length(var.vpc-azs)}"
  22. route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
  23. destination_cidr_block = "0.0.0.0/0"
  24. nat_gateway_id = "${element(aws_nat_gateway.private.*.id, count.index)}"
  25. }
  26. resource "aws_route_table" "private" {
  27. count = "${length(var.vpc-azs)}"
  28. vpc_id = "${aws_vpc.vpc.id}"
  29. tags = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
  30. }