main.tf 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Some instance variables
  2. locals {
  3. ami_selection = "tq" # master, minion, ...
  4. server_name_stem = "threatq"
  5. instance_count = 1
  6. }
  7. # Use the default EBS key
  8. data "aws_kms_key" "ebs-key" {
  9. key_id = "alias/ebs_root_encrypt_decrypt"
  10. }
  11. resource "aws_network_interface" "instance" {
  12. count = local.instance_count
  13. subnet_id = var.public_subnets[count.index % 3]
  14. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.instance.id ]
  15. description = "${local.server_name_stem}-${count.index}"
  16. tags = merge(var.standard_tags, var.tags, { Name = "${local.server_name_stem}-${count.index}" })
  17. }
  18. resource "aws_instance" "instance" {
  19. count = local.instance_count
  20. tenancy = "default"
  21. ebs_optimized = true
  22. disable_api_termination = var.instance_termination_protection
  23. instance_initiated_shutdown_behavior = "stop"
  24. instance_type = var.instance_type
  25. key_name = "msoc-build"
  26. monitoring = false
  27. iam_instance_profile = "msoc-default-instance-profile"
  28. ami = local.ami_map[local.ami_selection]
  29. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  30. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  31. # that could be removed.
  32. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  33. # These device definitions are optional, but added for clarity.
  34. root_block_device {
  35. volume_type = "gp2"
  36. #volume_size = "60"
  37. delete_on_termination = true
  38. encrypted = true
  39. kms_key_id = data.aws_kms_key.ebs-key.arn
  40. }
  41. ebs_block_device {
  42. # swap
  43. device_name = "/dev/xvdm"
  44. #volume_size = 48
  45. delete_on_termination = true
  46. encrypted = true
  47. kms_key_id = data.aws_kms_key.ebs-key.arn
  48. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  49. # This may prompt replacement when the AMI is updated.
  50. # See:
  51. # https://github.com/hashicorp/terraform/issues/19958
  52. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  53. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  54. }
  55. ebs_block_device {
  56. # /home
  57. device_name = "/dev/xvdn"
  58. # volume_size = xx
  59. delete_on_termination = true
  60. encrypted = true
  61. kms_key_id = data.aws_kms_key.ebs-key.arn
  62. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  63. }
  64. ebs_block_device {
  65. # /var
  66. device_name = "/dev/xvdo"
  67. # volume_size = xx
  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_size = xx
  77. delete_on_termination = true
  78. encrypted = true
  79. kms_key_id = data.aws_kms_key.ebs-key.arn
  80. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var/log
  84. device_name = "/dev/xvdq"
  85. # volume_size = xx
  86. delete_on_termination = true
  87. encrypted = true
  88. kms_key_id = data.aws_kms_key.ebs-key.arn
  89. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var/log/audit
  93. device_name = "/dev/xvdr"
  94. # volume_size = xx
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /tmp
  102. device_name = "/dev/xvds"
  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/xvds"].ebs.snapshot_id
  108. }
  109. network_interface {
  110. device_index = 0
  111. network_interface_id = aws_network_interface.instance[count.index].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. }