main.tf 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = "t3a.2xlarge"
  19. key_name = "msoc-build"
  20. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  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 = element(var.private_subnets, count.index)
  25. metadata_options {
  26. http_endpoint = "enabled"
  27. # checkov:skip=CKV_AWS_79:see tfsec explanation
  28. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  29. http_tokens = "optional"
  30. }
  31. ami = local.ami_map[local.ami_selection]
  32. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  33. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  34. # that could be removed.
  35. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  36. # These device definitions are optional, but added for clarity.
  37. root_block_device {
  38. volume_type = local.ebs_volume_type
  39. volume_size = "200"
  40. delete_on_termination = true
  41. encrypted = true
  42. kms_key_id = data.aws_kms_key.ebs-key.arn
  43. }
  44. ebs_block_device {
  45. # swap
  46. device_name = "/dev/xvdm"
  47. volume_type = local.ebs_volume_type
  48. #volume_size = 48
  49. delete_on_termination = true
  50. encrypted = true
  51. kms_key_id = data.aws_kms_key.ebs-key.arn
  52. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  53. # This may prompt replacement when the AMI is updated.
  54. # See:
  55. # https://github.com/hashicorp/terraform/issues/19958
  56. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  57. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  58. }
  59. ebs_block_device {
  60. # /home
  61. device_name = "/dev/xvdn"
  62. volume_type = local.ebs_volume_type
  63. # volume_size = xx
  64. delete_on_termination = true
  65. encrypted = true
  66. kms_key_id = data.aws_kms_key.ebs-key.arn
  67. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  68. }
  69. ebs_block_device {
  70. # /var
  71. device_name = "/dev/xvdo"
  72. volume_type = local.ebs_volume_type
  73. volume_size = 200
  74. delete_on_termination = true
  75. encrypted = true
  76. kms_key_id = data.aws_kms_key.ebs-key.arn
  77. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  78. }
  79. ebs_block_device {
  80. # /var/tmp
  81. device_name = "/dev/xvdp"
  82. volume_type = local.ebs_volume_type
  83. # volume_size = xx
  84. delete_on_termination = true
  85. encrypted = true
  86. kms_key_id = data.aws_kms_key.ebs-key.arn
  87. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  88. }
  89. ebs_block_device {
  90. # /var/log
  91. device_name = "/dev/xvdq"
  92. volume_type = local.ebs_volume_type
  93. # volume_size = xx
  94. delete_on_termination = true
  95. encrypted = true
  96. kms_key_id = data.aws_kms_key.ebs-key.arn
  97. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  98. }
  99. ebs_block_device {
  100. # /var/log/audit
  101. device_name = "/dev/xvdr"
  102. volume_type = local.ebs_volume_type
  103. # volume_size = xx
  104. delete_on_termination = true
  105. encrypted = true
  106. kms_key_id = data.aws_kms_key.ebs-key.arn
  107. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  108. }
  109. ebs_block_device {
  110. # /tmp
  111. device_name = "/dev/xvds"
  112. volume_type = local.ebs_volume_type
  113. volume_size = 20
  114. delete_on_termination = true
  115. encrypted = true
  116. kms_key_id = data.aws_kms_key.ebs-key.arn
  117. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  118. }
  119. user_data = data.template_cloudinit_config.cloud_init_config[count.index].rendered
  120. tags = merge(local.standard_tags, var.tags, { Name = "${local.server_name_stem}-${count.index}" })
  121. volume_tags = merge(local.standard_tags, var.tags, { Name = "${local.server_name_stem}-${count.index}" })
  122. }
  123. module "private_dns_record" {
  124. count = local.instance_count
  125. source = "../../submodules/dns/private_A_record"
  126. name = "${local.server_name_stem}-${count.index}"
  127. ip_addresses = [aws_instance.instance[count.index].private_ip]
  128. dns_info = var.dns_info
  129. reverse_enabled = var.reverse_enabled
  130. providers = {
  131. aws.c2 = aws.c2
  132. }
  133. }
  134. #module "public_dns_record" {
  135. # source = "../../submodules/dns/public_A_record"
  136. #
  137. # name = var.instance_name
  138. # ip_addresses = [ aws_eip.instance.public_ip ]
  139. # dns_info = var.dns_info
  140. #
  141. # providers = {
  142. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  143. # }
  144. #}
  145. # Render a multi-part cloud-init config making use of the part
  146. # above, and other source files
  147. data "template_cloudinit_config" "cloud_init_config" {
  148. count = local.instance_count
  149. gzip = true
  150. base64_encode = true
  151. # Main cloud-config configuration file.
  152. part {
  153. filename = "init.cfg"
  154. content_type = "text/cloud-config"
  155. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  156. {
  157. hostname = "${local.server_name_stem}-${count.index}"
  158. fqdn = "${local.server_name_stem}-${count.index}.${var.dns_info["private"]["zone"]}"
  159. environment = var.environment
  160. salt_master = local.salt_master
  161. proxy = local.proxy
  162. aws_partition = var.aws_partition
  163. aws_partition_alias = var.aws_partition_alias
  164. aws_region = var.aws_region
  165. }
  166. )
  167. }
  168. # mount /dev/xvdf at /opt/tqbackup
  169. part {
  170. content_type = "text/cloud-boothook"
  171. content = file("${path.module}/cloud-init/opt_tqbackup.boothook")
  172. }
  173. }