main.tf 8.6 KB

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