main.tf 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, { "Name": length(var.instance_count) > 1 ? "${var.instance_name}-${each.value}" : var.instance_name })
  131. volume_tags = merge( var.standard_tags, var.tags, { "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. # Render a multi-part cloud-init config making use of the part
  145. # above, and other source files
  146. data "template_cloudinit_config" "cloud_init_config" {
  147. for_each = toset(var.instance_count)
  148. gzip = true
  149. base64_encode = true
  150. # Main cloud-config configuration file.
  151. part {
  152. filename = "init.cfg"
  153. content_type = "text/cloud-config"
  154. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  155. {
  156. hostname = "${var.instance_name}-${each.value}"
  157. fqdn = "${var.instance_name}-${each.value}.${var.dns_info["private"]["zone"]}"
  158. environment = var.environment
  159. salt_master = var.salt_master
  160. proxy = var.proxy
  161. aws_partition = var.aws_partition
  162. aws_partition_alias = var.aws_partition_alias
  163. aws_region = var.aws_region
  164. }
  165. )
  166. }
  167. }
  168. #----------------------------------------------------------------------------
  169. # Vault Server SG
  170. #----------------------------------------------------------------------------
  171. resource "aws_security_group" "instance_security_group" {
  172. name = "${var.instance_name}_security_group"
  173. description = "Security Group for ${var.instance_name}(s)"
  174. vpc_id = var.vpc_id
  175. tags = merge(var.standard_tags, var.tags)
  176. }
  177. #----------------------------------------------------------------------------
  178. # Vault INGRESS
  179. #----------------------------------------------------------------------------
  180. resource "aws_security_group_rule" "vault_server_from_alb" {
  181. type = "ingress"
  182. from_port = 443
  183. to_port = 443
  184. protocol = "tcp"
  185. source_security_group_id = aws_security_group.vault_ALB_server.id
  186. description = "Allows the servers to receive traffic for troubleshooting"
  187. security_group_id = aws_security_group.instance_security_group.id
  188. }
  189. #----------------------------------------------------------------------------
  190. # Vault EGRESS
  191. #----------------------------------------------------------------------------
  192. resource "aws_security_group_rule" "https-out" {
  193. description = "For endpoints and troubleshooting"
  194. type = "egress"
  195. from_port = 443
  196. to_port = 443
  197. protocol = "tcp"
  198. cidr_blocks = [ "10.0.0.0/8" ]
  199. security_group_id = aws_security_group.instance_security_group.id
  200. }
  201. resource "aws_security_group_rule" "vault_server_to_alb" {
  202. type = "egress"
  203. from_port = 443
  204. to_port = 443
  205. protocol = "tcp"
  206. source_security_group_id = aws_security_group.vault_ALB_server.id
  207. description = "Allow vault to communicate via HTTPS w/ other members in cluster"
  208. security_group_id = aws_security_group.instance_security_group.id
  209. }
  210. #grab the dynamodb endpoint
  211. data "aws_prefix_list" "private_dynamodb" {
  212. name = "com.amazonaws.*.dynamodb"
  213. }
  214. resource "aws_security_group_rule" "vault_server_egress_dynamodb" {
  215. type = "egress"
  216. from_port = 443
  217. to_port = 443
  218. protocol = "tcp"
  219. security_group_id = aws_security_group.instance_security_group.id
  220. description = "Outbound to Dynamodb"
  221. prefix_list_ids = [ data.aws_prefix_list.private_dynamodb.id ]
  222. }