main.tf 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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
  45. iam_instance_profile = module.instance_profile.profile_id
  46. ami = local.ami_map[local.ami_selection]
  47. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  48. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  49. # that could be removed.
  50. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  51. # These device definitions are optional, but added for clarity.
  52. root_block_device {
  53. volume_type = "gp2"
  54. #volume_size = "60"
  55. delete_on_termination = true
  56. encrypted = true
  57. kms_key_id = data.aws_kms_key.ebs-key.arn
  58. }
  59. ebs_block_device {
  60. # swap
  61. device_name = "/dev/xvdm"
  62. volume_size = 48
  63. delete_on_termination = true
  64. encrypted = true
  65. kms_key_id = data.aws_kms_key.ebs-key.arn
  66. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  67. # This may prompt replacement when the AMI is updated.
  68. # See:
  69. # https://github.com/hashicorp/terraform/issues/19958
  70. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  71. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  72. }
  73. ebs_block_device {
  74. # /home
  75. device_name = "/dev/xvdn"
  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/xvdn"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var
  84. device_name = "/dev/xvdo"
  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/xvdo"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var/tmp
  93. device_name = "/dev/xvdp"
  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/xvdp"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var/log
  102. device_name = "/dev/xvdq"
  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/xvdq"].ebs.snapshot_id
  108. }
  109. ebs_block_device {
  110. # /var/log/audit
  111. device_name = "/dev/xvdr"
  112. # volume_size = xx
  113. delete_on_termination = true
  114. encrypted = true
  115. kms_key_id = data.aws_kms_key.ebs-key.arn
  116. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  117. }
  118. ebs_block_device {
  119. # /tmp
  120. device_name = "/dev/xvds"
  121. # volume_size = xx
  122. delete_on_termination = true
  123. encrypted = true
  124. kms_key_id = data.aws_kms_key.ebs-key.arn
  125. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  126. }
  127. network_interface {
  128. device_index = 0
  129. network_interface_id = aws_network_interface.instance[each.key].id
  130. }
  131. #TODO switch to dynamic tag
  132. user_data = data.template_cloudinit_config.cloud_init_config[each.key].rendered
  133. tags = merge(local.standard_tags, var.tags, { "Name" : length(var.instance_count) > 1 ? "${var.instance_name}-${each.value}" : var.instance_name })
  134. 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 })
  135. }
  136. module "private_dns_record" {
  137. for_each = toset(var.instance_count)
  138. source = "../../submodules/dns/private_A_record"
  139. name = "${var.instance_name}-${each.value}"
  140. ip_addresses = [aws_instance.instance[each.key].private_ip]
  141. dns_info = var.dns_info
  142. reverse_enabled = var.reverse_enabled
  143. providers = {
  144. aws.c2 = aws.c2
  145. }
  146. }
  147. # Render a multi-part cloud-init config making use of the part
  148. # above, and other source files
  149. data "template_cloudinit_config" "cloud_init_config" {
  150. for_each = toset(var.instance_count)
  151. gzip = true
  152. base64_encode = true
  153. # Main cloud-config configuration file.
  154. part {
  155. filename = "init.cfg"
  156. content_type = "text/cloud-config"
  157. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  158. {
  159. hostname = "${var.instance_name}-${each.value}"
  160. fqdn = "${var.instance_name}-${each.value}.${var.dns_info["private"]["zone"]}"
  161. environment = var.environment
  162. salt_master = local.salt_master
  163. proxy = local.proxy
  164. aws_partition = var.aws_partition
  165. aws_partition_alias = var.aws_partition_alias
  166. aws_region = var.aws_region
  167. }
  168. )
  169. }
  170. }
  171. #----------------------------------------------------------------------------
  172. # Vault Server SG
  173. #----------------------------------------------------------------------------
  174. resource "aws_security_group" "instance_security_group" {
  175. name = "${var.instance_name}_security_group"
  176. description = "Security Group for ${var.instance_name}(s)"
  177. vpc_id = var.vpc_id
  178. tags = merge(local.standard_tags, var.tags)
  179. }
  180. #----------------------------------------------------------------------------
  181. # Vault INGRESS
  182. #----------------------------------------------------------------------------
  183. resource "aws_security_group_rule" "vault_server_from_alb" {
  184. type = "ingress"
  185. from_port = 443
  186. to_port = 443
  187. protocol = "tcp"
  188. source_security_group_id = aws_security_group.vault_ALB_server.id
  189. description = "Allows the servers to receive traffic for troubleshooting"
  190. security_group_id = aws_security_group.instance_security_group.id
  191. }
  192. #----------------------------------------------------------------------------
  193. # Vault EGRESS
  194. #----------------------------------------------------------------------------
  195. resource "aws_security_group_rule" "https-out" {
  196. description = "For endpoints and troubleshooting"
  197. type = "egress"
  198. from_port = 443
  199. to_port = 443
  200. protocol = "tcp"
  201. cidr_blocks = ["10.0.0.0/8"]
  202. security_group_id = aws_security_group.instance_security_group.id
  203. }
  204. resource "aws_security_group_rule" "vault_server_to_alb" {
  205. type = "egress"
  206. from_port = 443
  207. to_port = 443
  208. protocol = "tcp"
  209. source_security_group_id = aws_security_group.vault_ALB_server.id
  210. description = "Allow vault to communicate via HTTPS w/ other members in cluster"
  211. security_group_id = aws_security_group.instance_security_group.id
  212. }
  213. #grab the dynamodb endpoint
  214. data "aws_prefix_list" "private_dynamodb" {
  215. name = "com.amazonaws.*.dynamodb"
  216. }
  217. resource "aws_security_group_rule" "vault_server_egress_dynamodb" {
  218. type = "egress"
  219. from_port = 443
  220. to_port = 443
  221. protocol = "tcp"
  222. security_group_id = aws_security_group.instance_security_group.id
  223. description = "Outbound to Dynamodb"
  224. prefix_list_ids = [data.aws_prefix_list.private_dynamodb.id]
  225. }