server.tf 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. data "template_file" "cloud-init-vmray-server" {
  75. # Should these be in a common directory? I suspect they'd be reusable
  76. template = file("${path.module}/cloud-init/cloud-init.tpl")
  77. vars = {
  78. hostname = "vmray-server"
  79. fqdn = "vmray-server.${var.dns_info["private"]["zone"]}"
  80. environment = var.environment
  81. salt_master = var.salt_master
  82. proxy = var.proxy
  83. aws_partition = var.aws_partition
  84. aws_partition_alias = var.aws_partition_alias
  85. aws_region = var.aws_region
  86. }
  87. }
  88. # Render a multi-part cloud-init config making use of the part
  89. # above, and other source files
  90. data "template_cloudinit_config" "cloud-init-vmray-server" {
  91. gzip = true
  92. base64_encode = true
  93. # Main cloud-config configuration file.
  94. part {
  95. filename = "init.cfg"
  96. content_type = "text/cloud-config"
  97. content = data.template_file.cloud-init-vmray-server.rendered
  98. }
  99. # Additional parts as needed
  100. #part {
  101. # content_type = "text/x-shellscript"
  102. # content = "ffbaz"
  103. #}
  104. }
  105. module "private_dns_record_vmray_server" {
  106. source = "../../submodules/dns/private_A_record"
  107. name = "vmray-server"
  108. ip_addresses = [ aws_instance.vmray-server-instance.private_ip ]
  109. dns_info = var.dns_info
  110. reverse_enabled = true
  111. providers = {
  112. aws.c2 = aws.c2
  113. }
  114. }