remote.tf 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. resource "yandex_vpc_network" "remote-admin" {
  2. name = "remote-admin"
  3. }
  4. resource "yandex_vpc_route_table" "route-to-cloud" {
  5. name = "route-to-cloud"
  6. network_id = yandex_vpc_network.remote-admin.id
  7. static_route {
  8. destination_prefix = "10.0.0.0/8"
  9. next_hop_address = "192.168.0.5"
  10. }
  11. }
  12. resource "yandex_vpc_subnet" "remote-a" {
  13. name = "remote-a"
  14. zone = "ru-central1-a"
  15. network_id = yandex_vpc_network.remote-admin.id
  16. v4_cidr_blocks = ["192.168.0.0/24"]
  17. }
  18. data "yandex_compute_image" "my_vpn" {
  19. family = "ipsec-instance-ubuntu"
  20. }
  21. resource "yandex_vpc_security_group" "sg-remote" {
  22. name = "sg-remote"
  23. description = "allows traffic in and out of tunnel and tunnel itself"
  24. network_id = yandex_vpc_network.remote-admin.id
  25. ingress {
  26. protocol = "TCP"
  27. description = "internal_net_ssh"
  28. v4_cidr_blocks = ["10.0.0.0/8", "192.168.0.0/24"]
  29. port = 22
  30. }
  31. ingress {
  32. protocol = "ICMP"
  33. description = "internal_icmp"
  34. v4_cidr_blocks = ["10.0.0.0/8", "192.168.0.0/24"]
  35. }
  36. ingress {
  37. protocol = "ANY"
  38. description = "p2p"
  39. predefined_target = "self_security_group"
  40. }
  41. ingress {
  42. protocol = "UDP"
  43. description = "ipsec_peer_allow_4500"
  44. v4_cidr_blocks = formatlist("%s/32", [yandex_vpc_address.vpnaddr.external_ipv4_address.0.address])
  45. port = 4500
  46. }
  47. ingress {
  48. protocol = "UDP"
  49. description = "ipsec_peer_allow_500"
  50. v4_cidr_blocks = formatlist("%s/32", [yandex_vpc_address.vpnaddr.external_ipv4_address.0.address])
  51. port = 500
  52. }
  53. ingress {
  54. protocol = "TCP"
  55. description = "p2p"
  56. v4_cidr_blocks = var.remote_whitelist_ip
  57. port = "22"
  58. }
  59. egress {
  60. protocol = "ANY"
  61. description = "egress_internet"
  62. v4_cidr_blocks = ["0.0.0.0/0"]
  63. from_port = 0
  64. to_port = 65535
  65. }
  66. }
  67. data "template_file" "remote_init" {
  68. template = "${file("remote-init.tpl.yaml")}"
  69. vars = {
  70. ssh_key = "${file(var.public_key_path)}"
  71. vpn_addr = yandex_vpc_address.vpnaddr.external_ipv4_address.0.address
  72. remote_addr = yandex_vpc_address.remoteaddr.external_ipv4_address.0.address
  73. ipsec_pass = var.ipsec_password
  74. }
  75. }
  76. resource "yandex_vpc_address" "remoteaddr" {
  77. name = "remoteaddr"
  78. external_ipv4_address {
  79. zone_id = "ru-central1-a"
  80. }
  81. }
  82. resource "yandex_compute_instance" "remote-vpn" {
  83. zone = "ru-central1-a"
  84. name = "remote-vpn"
  85. hostname = "remote-vpn"
  86. platform_id = "standard-v2"
  87. resources {
  88. cores = 4
  89. memory = 8
  90. }
  91. boot_disk {
  92. initialize_params {
  93. image_id = data.yandex_compute_image.my_vpn.id
  94. type = "network-ssd"
  95. size = 26
  96. }
  97. }
  98. network_interface {
  99. subnet_id = yandex_vpc_subnet.remote-a.id
  100. ip_address = "192.168.0.5"
  101. nat = true
  102. nat_ip_address = yandex_vpc_address.remoteaddr.external_ipv4_address.0.address
  103. security_group_ids = [yandex_vpc_security_group.sg-remote.id]
  104. }
  105. metadata = {
  106. user-data = "${data.template_file.remote_init.rendered}"
  107. serial-port-enable = 1
  108. }
  109. }