vpc.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # =============
  2. # VPC Resources
  3. # =============
  4. # Define SGW Network
  5. data "yandex_vpc_network" "yc_net" {
  6. folder_id = data.yandex_resourcemanager_folder.sgw_folder.id
  7. name = var.yc_subnets.net_name
  8. }
  9. # Create SGW Subnet
  10. resource "yandex_vpc_subnet" "sgw_subnet" {
  11. folder_id = data.yandex_resourcemanager_folder.sgw_folder.id
  12. name = "${var.yc_sgw.name}-subnet"
  13. description = "YC IPsec Security gateway subnet"
  14. v4_cidr_blocks = [var.yc_sgw.subnet]
  15. zone = var.yc_sgw.zone
  16. network_id = data.yandex_vpc_network.yc_net.id
  17. labels = var.labels
  18. }
  19. # Reserve a static IP for the SGW instance
  20. resource "yandex_vpc_address" "sgw_public_ip" {
  21. folder_id = data.yandex_resourcemanager_folder.sgw_folder.id
  22. name = var.yc_sgw.name
  23. external_ipv4_address {
  24. zone_id = var.yc_sgw.zone
  25. }
  26. labels = var.labels
  27. }
  28. # Create Security Group for SGW
  29. resource "yandex_vpc_security_group" "sgw_sg" {
  30. folder_id = data.yandex_resourcemanager_folder.sgw_folder.id
  31. name = "${lower(var.yc_sgw.name)}-sg"
  32. description = "IPsec SGW VM"
  33. network_id = data.yandex_vpc_network.yc_net.id
  34. labels = var.labels
  35. ingress {
  36. description = "icmp"
  37. protocol = "ICMP"
  38. v4_cidr_blocks = ["0.0.0.0/0"]
  39. }
  40. ingress {
  41. description = "ssh"
  42. protocol = "TCP"
  43. port = 22
  44. v4_cidr_blocks = ["0.0.0.0/0"]
  45. }
  46. ingress {
  47. description = "http"
  48. protocol = "TCP"
  49. port = "8000"
  50. v4_cidr_blocks = ["0.0.0.0/0"]
  51. }
  52. ingress {
  53. description = "ipsec"
  54. protocol = "UDP"
  55. port = "500"
  56. v4_cidr_blocks = ["${var.remote_sgw.outside_ip}/32"]
  57. }
  58. ingress {
  59. description = "ipsec"
  60. protocol = "UDP"
  61. port = "4500"
  62. v4_cidr_blocks = ["${var.remote_sgw.outside_ip}/32"]
  63. }
  64. egress {
  65. description = "Permit ANY"
  66. protocol = "ANY"
  67. v4_cidr_blocks = ["0.0.0.0/0"]
  68. }
  69. }
  70. # Get All Subnets inside of specified Network/VPC
  71. data "yandex_vpc_subnet" "yc_sub_all" {
  72. folder_id = var.folder_id
  73. for_each = toset(data.yandex_vpc_network.yc_net.subnet_ids)
  74. subnet_id = each.value
  75. }
  76. locals {
  77. single_list = ["one-value"]
  78. # Filter Subnets by var.remote_subnets list
  79. sub_list = tolist(var.yc_subnets.prefix_list)
  80. subnet_list = flatten([
  81. for sub_id in data.yandex_vpc_network.yc_net.subnet_ids : {
  82. id = sub_id
  83. prefix = data.yandex_vpc_subnet.yc_sub_all[sub_id].v4_cidr_blocks[0]
  84. } if contains(local.sub_list, data.yandex_vpc_subnet.yc_sub_all[sub_id].v4_cidr_blocks[0])
  85. ])
  86. # generate yc CLI strings for apply RT to subnets
  87. yc_rt_cmd = "ids=\"${join(" ", flatten([
  88. for sub in local.subnet_list : ["${sub.id}"]
  89. ]))}\"; for id in $ids ; do yc vpc subnet update $id --route-table-name=${lower(var.yc_sgw.name)}-rt ; done"
  90. }
  91. # Create Route table for route traffic to the remote subnets via SGW
  92. resource "yandex_vpc_route_table" "sgw_rt" {
  93. folder_id = data.yandex_resourcemanager_folder.sgw_folder.id
  94. name = "${lower(var.yc_sgw.name)}-rt"
  95. network_id = data.yandex_vpc_network.yc_net.id
  96. dynamic "static_route" {
  97. for_each = var.remote_subnets == null ? [] : var.remote_subnets
  98. content {
  99. destination_prefix = static_route.value
  100. next_hop_address = var.yc_sgw.inside_ip
  101. }
  102. }
  103. dynamic "static_route" {
  104. for_each = [for el in local.single_list : el
  105. if var.yc_subnets.rt_internet_access == true]
  106. content {
  107. destination_prefix = "0.0.0.0/0"
  108. gateway_id = yandex_vpc_gateway.egress_gw[0].id
  109. }
  110. }
  111. }
  112. # If yc_subnets.rt_internet_access = true, Gateway should be created
  113. resource "yandex_vpc_gateway" "egress_gw" {
  114. count = var.yc_subnets.rt_internet_access ? 1 : 0
  115. folder_id = var.folder_id
  116. name = "${data.yandex_vpc_network.yc_net.name}-egw"
  117. shared_egress_gateway {}
  118. }
  119. # If yc_subnets.force_subnets_update = true, perform subnets update with yc
  120. resource "null_resource" "yc_subnets_update" {
  121. count = var.yc_subnets.force_subnets_update ? 1 : 0
  122. provisioner "local-exec" {
  123. command = local.yc_rt_cmd
  124. }
  125. depends_on = [
  126. yandex_vpc_route_table.sgw_rt
  127. ]
  128. }