networking.tf 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. resource "aws_internet_gateway" "this" {
  2. count = var.create_vpc ? 1 : 0
  3. vpc_id = aws_vpc.this[0].id
  4. tags = merge(
  5. local.common_tags,
  6. {
  7. Name = "${var.name}-igw"
  8. }
  9. )
  10. }
  11. resource "aws_eip" "nat" {
  12. for_each = var.create_vpc ? (var.single_nat_gateway ? { shared = local.azs[0] } : { for az in local.azs : az => az }) : {}
  13. domain = "vpc"
  14. tags = merge(
  15. local.common_tags,
  16. {
  17. Name = "${var.name}-nat-eip-${each.key}"
  18. }
  19. )
  20. }
  21. resource "aws_nat_gateway" "this" {
  22. for_each = var.create_vpc ? (var.single_nat_gateway ? { shared = local.azs[0] } : { for az in local.azs : az => az }) : {}
  23. allocation_id = aws_eip.nat[each.key].id
  24. subnet_id = aws_subnet.public[each.value].id
  25. tags = merge(
  26. local.common_tags,
  27. {
  28. Name = "${var.name}-nat-${each.key}"
  29. }
  30. )
  31. depends_on = [aws_internet_gateway.this]
  32. }
  33. resource "aws_route_table" "public" {
  34. count = var.create_vpc ? 1 : 0
  35. vpc_id = aws_vpc.this[0].id
  36. route {
  37. cidr_block = "0.0.0.0/0"
  38. gateway_id = aws_internet_gateway.this[0].id
  39. }
  40. tags = merge(
  41. local.common_tags,
  42. {
  43. Name = "${var.name}-public-rt"
  44. }
  45. )
  46. }
  47. resource "aws_route_table_association" "public" {
  48. for_each = var.create_vpc ? aws_subnet.public : {}
  49. subnet_id = each.value.id
  50. route_table_id = aws_route_table.public[0].id
  51. }
  52. resource "aws_route_table" "private" {
  53. for_each = var.create_vpc ? aws_subnet.private : {}
  54. vpc_id = aws_vpc.this[0].id
  55. route {
  56. cidr_block = "0.0.0.0/0"
  57. nat_gateway_id = aws_nat_gateway.this[var.single_nat_gateway ? "shared" : each.key].id
  58. }
  59. tags = merge(
  60. local.common_tags,
  61. {
  62. Name = "${var.name}-private-rt-${each.key}"
  63. }
  64. )
  65. }
  66. resource "aws_route_table_association" "private" {
  67. for_each = var.create_vpc ? aws_subnet.private : {}
  68. subnet_id = each.value.id
  69. route_table_id = aws_route_table.private[each.key].id
  70. }