main.tf 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. # Rather than pass in the aws security group, we just look it up. This will
  6. # probably be useful other places, as well.
  7. data "aws_security_group" "typical-host" {
  8. name = "typical-host"
  9. vpc_id = var.vpc_id
  10. }
  11. # Use the default EBS key
  12. data "aws_kms_key" "ebs-key" {
  13. key_id = "alias/ebs_root_encrypt_decrypt"
  14. }
  15. resource "aws_network_interface" "instance" {
  16. for_each = toset(var.instance_count)
  17. subnet_id = var.subnets[each.value - 1]
  18. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.instance_security_group.id]
  19. description = "${var.instance_name}-${each.value}"
  20. tags = merge(local.standard_tags, var.tags, { Name = "${var.instance_name}-${each.value}" })
  21. }
  22. # resource "aws_eip" "instance" {
  23. # for_each = toset(var.instance_count)
  24. # vpc = true
  25. # tags = merge(local.standard_tags, var.tags, { Name = "${var.instance_name}-${each.value}" })
  26. # }
  27. # resource "aws_eip_association" "instance" {
  28. # for_each = toset(var.instance_count)
  29. # network_interface_id = aws_network_interface.instance[each.key].id
  30. # allocation_id = aws_eip.instance[each.key].id
  31. # }
  32. resource "aws_instance" "instance" {
  33. # TODO: Make instance count numeric. While Instance count is currently a list of strings so
  34. # that the names are nicely 1, 2, and 3, but this could also have been solved with a '+1'. A count
  35. # seems like it would be cleaner.
  36. for_each = toset(var.instance_count)
  37. #availability_zone = var.azs[count.index % 2]
  38. tenancy = "default"
  39. ebs_optimized = true
  40. disable_api_termination = var.instance_termination_protection
  41. instance_initiated_shutdown_behavior = "stop"
  42. instance_type = var.environment == "prod" ? "t3a.medium" : "t3a.small"
  43. key_name = "msoc-build"
  44. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  45. iam_instance_profile = module.instance_profile.profile_id
  46. metadata_options {
  47. http_endpoint = "enabled"
  48. # checkov:skip=CKV_AWS_79:see tfsec explanation
  49. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  50. http_tokens = "optional"
  51. }
  52. ami = local.ami_map[local.ami_selection]
  53. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  54. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  55. # that could be removed.
  56. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  57. # These device definitions are optional, but added for clarity.
  58. root_block_device {
  59. volume_type = "gp2"
  60. #volume_size = "60"
  61. delete_on_termination = true
  62. encrypted = true
  63. kms_key_id = data.aws_kms_key.ebs-key.arn
  64. }
  65. ebs_block_device {
  66. # swap
  67. device_name = "/dev/xvdm"
  68. volume_size = 48
  69. delete_on_termination = true
  70. encrypted = true
  71. kms_key_id = data.aws_kms_key.ebs-key.arn
  72. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  73. # This may prompt replacement when the AMI is updated.
  74. # See:
  75. # https://github.com/hashicorp/terraform/issues/19958
  76. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  77. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  78. }
  79. ebs_block_device {
  80. # /home
  81. device_name = "/dev/xvdn"
  82. # volume_size = xx
  83. delete_on_termination = true
  84. encrypted = true
  85. kms_key_id = data.aws_kms_key.ebs-key.arn
  86. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  87. }
  88. ebs_block_device {
  89. # /var
  90. device_name = "/dev/xvdo"
  91. # volume_size = xx
  92. delete_on_termination = true
  93. encrypted = true
  94. kms_key_id = data.aws_kms_key.ebs-key.arn
  95. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  96. }
  97. ebs_block_device {
  98. # /var/tmp
  99. device_name = "/dev/xvdp"
  100. # volume_size = xx
  101. delete_on_termination = true
  102. encrypted = true
  103. kms_key_id = data.aws_kms_key.ebs-key.arn
  104. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  105. }
  106. ebs_block_device {
  107. # /var/log
  108. device_name = "/dev/xvdq"
  109. # volume_size = xx
  110. delete_on_termination = true
  111. encrypted = true
  112. kms_key_id = data.aws_kms_key.ebs-key.arn
  113. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  114. }
  115. ebs_block_device {
  116. # /var/log/audit
  117. device_name = "/dev/xvdr"
  118. # volume_size = xx
  119. delete_on_termination = true
  120. encrypted = true
  121. kms_key_id = data.aws_kms_key.ebs-key.arn
  122. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  123. }
  124. ebs_block_device {
  125. # /tmp
  126. device_name = "/dev/xvds"
  127. # volume_size = xx
  128. delete_on_termination = true
  129. encrypted = true
  130. kms_key_id = data.aws_kms_key.ebs-key.arn
  131. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  132. }
  133. network_interface {
  134. device_index = 0
  135. network_interface_id = aws_network_interface.instance[each.key].id
  136. }
  137. #TODO switch to dynamic tag
  138. user_data = data.template_cloudinit_config.cloud_init_config[each.key].rendered
  139. tags = merge(local.standard_tags, var.tags, { "Name" : length(var.instance_count) > 1 ? "${var.instance_name}-${each.value}" : var.instance_name })
  140. volume_tags = merge(local.standard_tags, var.tags, var.instance_tags[tonumber(each.key) - 1], { "Name" : length(var.instance_count) > 1 ? "${var.instance_name}-${each.value}" : var.instance_name })
  141. }
  142. module "private_dns_record" {
  143. for_each = toset(var.instance_count)
  144. source = "../../submodules/dns/private_A_record"
  145. name = "${var.instance_name}-${each.value}"
  146. ip_addresses = [aws_instance.instance[each.key].private_ip]
  147. dns_info = var.dns_info
  148. reverse_enabled = var.reverse_enabled
  149. providers = {
  150. aws.c2 = aws.c2
  151. }
  152. }
  153. # Render a multi-part cloud-init config making use of the part
  154. # above, and other source files
  155. data "template_cloudinit_config" "cloud_init_config" {
  156. for_each = toset(var.instance_count)
  157. gzip = true
  158. base64_encode = true
  159. # Main cloud-config configuration file.
  160. part {
  161. filename = "init.cfg"
  162. content_type = "text/cloud-config"
  163. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  164. {
  165. hostname = "${var.instance_name}-${each.value}"
  166. fqdn = "${var.instance_name}-${each.value}.${var.dns_info["private"]["zone"]}"
  167. environment = var.environment
  168. salt_master = local.salt_master
  169. proxy = local.proxy
  170. aws_partition = var.aws_partition
  171. aws_partition_alias = var.aws_partition_alias
  172. aws_region = var.aws_region
  173. }
  174. )
  175. }
  176. }
  177. #----------------------------------------------------------------------------
  178. # Vault Server Security Group
  179. #----------------------------------------------------------------------------
  180. resource "aws_security_group" "instance_security_group" {
  181. name = "${var.instance_name}_security_group"
  182. description = "Security Group for ${var.instance_name}(s)"
  183. vpc_id = var.vpc_id
  184. tags = merge(local.standard_tags, var.tags)
  185. }
  186. #----------------------------------------------------------------------------
  187. # INGRESS
  188. #----------------------------------------------------------------------------
  189. resource "aws_security_group_rule" "vault_server_from_alb" {
  190. type = "ingress"
  191. description = "Allows the servers to receive traffic for troubleshooting"
  192. from_port = 443
  193. to_port = 443
  194. protocol = "tcp"
  195. source_security_group_id = aws_security_group.vault_ALB_server.id
  196. security_group_id = aws_security_group.instance_security_group.id
  197. }
  198. #----------------------------------------------------------------------------
  199. # EGRESS
  200. #----------------------------------------------------------------------------
  201. resource "aws_security_group_rule" "https-out" {
  202. type = "egress"
  203. description = "For endpoints and troubleshooting"
  204. from_port = 443
  205. to_port = 443
  206. protocol = "tcp"
  207. cidr_blocks = ["10.0.0.0/8"]
  208. security_group_id = aws_security_group.instance_security_group.id
  209. }
  210. resource "aws_security_group_rule" "vault_server_to_alb" {
  211. type = "egress"
  212. description = "Allow Vault to communicate via HTTPS w/ other members in cluster"
  213. from_port = 443
  214. to_port = 443
  215. protocol = "tcp"
  216. source_security_group_id = aws_security_group.vault_ALB_server.id
  217. security_group_id = aws_security_group.instance_security_group.id
  218. }
  219. #grab the dynamodb endpoint
  220. data "aws_prefix_list" "private_dynamodb" {
  221. name = "com.amazonaws.*.dynamodb"
  222. }
  223. resource "aws_security_group_rule" "vault_server_egress_dynamodb" {
  224. type = "egress"
  225. description = "Vault HTTPS - Outbound to Dynamodb"
  226. from_port = 443
  227. to_port = 443
  228. protocol = "tcp"
  229. security_group_id = aws_security_group.instance_security_group.id
  230. prefix_list_ids = [data.aws_prefix_list.private_dynamodb.id]
  231. }