server.tf 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Handy tool to find AMIs:
  2. # https://cloud-images.ubuntu.com/locator/ec2/
  3. #data "aws_ami" "ubuntu_pro" {
  4. # # Ubuntu Pro 18.04 Product ID: fc15dd7b-2aa0-47e5-bb05-94c75950b5de
  5. #
  6. # most_recent = true
  7. # owners = [ "513442679011" ] # This seems like it might change sometimes?
  8. #
  9. # # sadly, it looks like the image isn't named correctly in govcloud. I've given
  10. # # canonical feedback on this, but for now we'll use the ami id directly.
  11. # #filter {
  12. # # name = "name"
  13. # # values = ["ubuntu-pro/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*", ]
  14. # #}
  15. #
  16. # filter {
  17. # name = "image-id"
  18. # values = [ "ami-0fe6338c47e61cd5d" ]
  19. # }
  20. #
  21. # filter {
  22. # name = "root-device-type"
  23. # values = ["ebs"]
  24. # }
  25. #
  26. # filter {
  27. # name = "virtualization-type"
  28. # values = ["hvm"]
  29. # }
  30. #}
  31. data "aws_security_group" "typical-host" {
  32. name = "typical-host"
  33. vpc_id = var.vpc_id
  34. }
  35. # Use the default EBS key
  36. data "aws_kms_key" "ebs-key" {
  37. key_id = "alias/ebs_root_encrypt_decrypt"
  38. }
  39. resource "aws_network_interface" "vmray-server-interface" {
  40. subnet_id = var.private_subnets[0]
  41. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.vmray_server_sg.id ]
  42. description = "vmray-server"
  43. tags = merge(var.standard_tags, var.tags, { Name = "vmray-server" })
  44. }
  45. resource "aws_instance" "vmray-server-instance" {
  46. tenancy = "default"
  47. ebs_optimized = true
  48. disable_api_termination = var.instance_termination_protection
  49. instance_initiated_shutdown_behavior = "stop"
  50. instance_type = var.instance_types["vmray-server"]
  51. key_name = "msoc-build"
  52. monitoring = false
  53. iam_instance_profile = "msoc-default-instance-profile"
  54. ami = data.aws_ami.ubuntu2004.image_id
  55. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  56. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  57. # that could be removed.
  58. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  59. root_block_device {
  60. volume_type = "gp3"
  61. volume_size = "60"
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. }
  66. network_interface {
  67. device_index = 0
  68. network_interface_id = aws_network_interface.vmray-server-interface.id
  69. }
  70. user_data = data.template_cloudinit_config.cloud-init-vmray-server.rendered
  71. tags = merge( var.standard_tags, var.tags, { Name = "vmray-server" })
  72. volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-server" })
  73. }
  74. # Secrets example:
  75. # 1. Find the secret
  76. # 2. Get the secret
  77. # 3. Decode the json
  78. data "aws_secretsmanager_secret" "ubuntu" {
  79. name = "Ubuntu"
  80. provider = aws.c2
  81. }
  82. data "aws_secretsmanager_secret_version" "ubuntu" {
  83. secret_id = data.aws_secretsmanager_secret.ubuntu.id
  84. provider = aws.c2
  85. }
  86. locals {
  87. secret_ubuntu = jsondecode(data.aws_secretsmanager_secret_version.ubuntu.secret_string)
  88. }
  89. data "template_file" "cloud-init-vmray-server" {
  90. # Should these be in a common directory? I suspect they'd be reusable
  91. template = file("${path.module}/cloud-init/cloud-init.tpl")
  92. vars = {
  93. hostname = "vmray-server"
  94. fqdn = "vmray-server.${var.dns_info["private"]["zone"]}"
  95. environment = var.environment
  96. salt_master = var.salt_master
  97. proxy = var.proxy
  98. aws_partition = var.aws_partition
  99. aws_partition_alias = var.aws_partition_alias
  100. aws_region = var.aws_region
  101. ua_key = local.secret_ubuntu["ua_key"]
  102. }
  103. }
  104. # Render a multi-part cloud-init config making use of the part
  105. # above, and other source files
  106. data "template_cloudinit_config" "cloud-init-vmray-server" {
  107. gzip = true
  108. base64_encode = true
  109. # Main cloud-config configuration file.
  110. part {
  111. filename = "init.cfg"
  112. content_type = "text/cloud-config"
  113. content = data.template_file.cloud-init-vmray-server.rendered
  114. }
  115. # Additional parts as needed
  116. #part {
  117. # content_type = "text/x-shellscript"
  118. # content = "ffbaz"
  119. #}
  120. }
  121. module "private_dns_record_vmray_server" {
  122. source = "../../submodules/dns/private_A_record"
  123. name = "vmray-server"
  124. ip_addresses = [ aws_instance.vmray-server-instance.private_ip ]
  125. dns_info = var.dns_info
  126. reverse_enabled = true
  127. providers = {
  128. aws.c2 = aws.c2
  129. }
  130. }