main.tf 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Some instance variables
  2. locals {
  3. ami_selection = "tq" # master, minion, ...
  4. server_name_stem = "threatq"
  5. instance_count = 1
  6. ebs_volume_type = "gp3"
  7. }
  8. # Use the default EBS key
  9. data "aws_kms_key" "ebs-key" {
  10. key_id = "alias/ebs_root_encrypt_decrypt"
  11. }
  12. resource "aws_instance" "instance" {
  13. count = local.instance_count
  14. tenancy = "default"
  15. ebs_optimized = true
  16. disable_api_termination = var.instance_termination_protection
  17. instance_initiated_shutdown_behavior = "stop"
  18. instance_type = var.instance_type
  19. key_name = "msoc-build"
  20. monitoring = false
  21. iam_instance_profile = "msoc-default-instance-profile"
  22. associate_public_ip_address = false
  23. vpc_security_group_ids = [ data.aws_security_group.typical-host.id, aws_security_group.instance.id ]
  24. subnet_id = var.public_subnets[count.index % 3]
  25. ami = local.ami_map[local.ami_selection]
  26. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  27. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  28. # that could be removed.
  29. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  30. # These device definitions are optional, but added for clarity.
  31. root_block_device {
  32. volume_type = local.ebs_volume_type
  33. volume_size = "60"
  34. delete_on_termination = true
  35. encrypted = true
  36. kms_key_id = data.aws_kms_key.ebs-key.arn
  37. }
  38. ebs_block_device {
  39. # swap
  40. device_name = "/dev/xvdm"
  41. volume_type = local.ebs_volume_type
  42. #volume_size = 48
  43. delete_on_termination = true
  44. encrypted = true
  45. kms_key_id = data.aws_kms_key.ebs-key.arn
  46. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  47. # This may prompt replacement when the AMI is updated.
  48. # See:
  49. # https://github.com/hashicorp/terraform/issues/19958
  50. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  51. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  52. }
  53. ebs_block_device {
  54. # /home
  55. device_name = "/dev/xvdn"
  56. volume_type = local.ebs_volume_type
  57. # volume_size = xx
  58. delete_on_termination = true
  59. encrypted = true
  60. kms_key_id = data.aws_kms_key.ebs-key.arn
  61. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  62. }
  63. ebs_block_device {
  64. # /var
  65. device_name = "/dev/xvdo"
  66. volume_type = local.ebs_volume_type
  67. volume_size = 80
  68. delete_on_termination = true
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  72. }
  73. ebs_block_device {
  74. # /var/tmp
  75. device_name = "/dev/xvdp"
  76. volume_type = local.ebs_volume_type
  77. # volume_size = xx
  78. delete_on_termination = true
  79. encrypted = true
  80. kms_key_id = data.aws_kms_key.ebs-key.arn
  81. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  82. }
  83. ebs_block_device {
  84. # /var/log
  85. device_name = "/dev/xvdq"
  86. volume_type = local.ebs_volume_type
  87. # volume_size = xx
  88. delete_on_termination = true
  89. encrypted = true
  90. kms_key_id = data.aws_kms_key.ebs-key.arn
  91. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  92. }
  93. ebs_block_device {
  94. # /var/log/audit
  95. device_name = "/dev/xvdr"
  96. volume_type = local.ebs_volume_type
  97. # volume_size = xx
  98. delete_on_termination = true
  99. encrypted = true
  100. kms_key_id = data.aws_kms_key.ebs-key.arn
  101. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  102. }
  103. ebs_block_device {
  104. # /tmp
  105. device_name = "/dev/xvds"
  106. volume_type = local.ebs_volume_type
  107. # volume_size = xx
  108. delete_on_termination = true
  109. encrypted = true
  110. kms_key_id = data.aws_kms_key.ebs-key.arn
  111. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  112. }
  113. user_data = data.template_cloudinit_config.cloud_init_config[count.index].rendered
  114. tags = merge( var.standard_tags, var.tags, { Name = "${local.server_name_stem}-${count.index}" })
  115. volume_tags = merge( var.standard_tags, var.tags, { Name = "${local.server_name_stem}-${count.index}" })
  116. }
  117. module "private_dns_record" {
  118. count = local.instance_count
  119. source = "../../submodules/dns/private_A_record"
  120. name = "${local.server_name_stem}-${count.index}"
  121. ip_addresses = [ aws_instance.instance[count.index].private_ip ]
  122. dns_info = var.dns_info
  123. reverse_enabled = var.reverse_enabled
  124. providers = {
  125. aws.c2 = aws.c2
  126. }
  127. }
  128. #module "public_dns_record" {
  129. # source = "../../submodules/dns/public_A_record"
  130. #
  131. # name = var.instance_name
  132. # ip_addresses = [ aws_eip.instance.public_ip ]
  133. # dns_info = var.dns_info
  134. #
  135. # providers = {
  136. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  137. # }
  138. #}
  139. # Render a multi-part cloud-init config making use of the part
  140. # above, and other source files
  141. data "template_cloudinit_config" "cloud_init_config" {
  142. count = local.instance_count
  143. gzip = true
  144. base64_encode = true
  145. # Main cloud-config configuration file.
  146. part {
  147. filename = "init.cfg"
  148. content_type = "text/cloud-config"
  149. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  150. {
  151. hostname = "${local.server_name_stem}-${count.index}"
  152. fqdn = "${local.server_name_stem}-${count.index}.${var.dns_info["private"]["zone"]}"
  153. environment = var.environment
  154. salt_master = var.salt_master
  155. proxy = var.proxy
  156. aws_partition = var.aws_partition
  157. aws_partition_alias = var.aws_partition_alias
  158. aws_region = var.aws_region
  159. }
  160. )
  161. }
  162. # mount /dev/xvdf at /opt/tqbackup
  163. part {
  164. content_type = "text/cloud-boothook"
  165. content = file("${path.module}/cloud-init/opt_tqbackup.boothook")
  166. }
  167. }