main.tf 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # Rather than pass in the aws security group, we just look it up. This will
  2. # probably be useful other places, as well.
  3. data "aws_security_group" "typical-host" {
  4. name = "typical-host"
  5. vpc_id = var.vpc_id
  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. subnet_id = var.subnets[0]
  13. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.salt_master_security_group.id ]
  14. description = var.instance_name
  15. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  16. }
  17. resource "aws_eip" "instance" {
  18. vpc = true
  19. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_eip_association" "instance" {
  22. network_interface_id = aws_network_interface.instance.id
  23. allocation_id = aws_eip.instance.id
  24. }
  25. resource "aws_instance" "instance" {
  26. #availability_zone = var.azs[count.index % 2]
  27. tenancy = "default"
  28. ebs_optimized = true
  29. disable_api_termination = var.instance_termination_protection
  30. instance_initiated_shutdown_behavior = "stop"
  31. instance_type = var.instance_type
  32. key_name = "msoc-build"
  33. monitoring = false
  34. iam_instance_profile = "salt-master-instance-profile"
  35. ami = local.ami_map["master"]
  36. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  37. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  38. # that could be removed.
  39. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  40. # These device definitions are optional, but added for clarity.
  41. root_block_device {
  42. volume_type = "gp2"
  43. #volume_size = "60"
  44. delete_on_termination = true
  45. encrypted = true
  46. kms_key_id = data.aws_kms_key.ebs-key.arn
  47. }
  48. ebs_block_device {
  49. # swap
  50. device_name = "/dev/xvdm"
  51. volume_size = 48
  52. delete_on_termination = true
  53. encrypted = true
  54. kms_key_id = data.aws_kms_key.ebs-key.arn
  55. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  56. # This may prompt replacement when the AMI is updated.
  57. # See:
  58. # https://github.com/hashicorp/terraform/issues/19958
  59. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  60. snapshot_id = local.block_device_mappings["master"]["/dev/xvdm"].ebs.snapshot_id
  61. }
  62. ebs_block_device {
  63. # /home
  64. device_name = "/dev/xvdn"
  65. # volume_size = xx
  66. delete_on_termination = true
  67. encrypted = true
  68. kms_key_id = data.aws_kms_key.ebs-key.arn
  69. snapshot_id = local.block_device_mappings["master"]["/dev/xvdn"].ebs.snapshot_id
  70. }
  71. ebs_block_device {
  72. # /var
  73. device_name = "/dev/xvdo"
  74. # volume_size = xx
  75. delete_on_termination = true
  76. encrypted = true
  77. kms_key_id = data.aws_kms_key.ebs-key.arn
  78. snapshot_id = local.block_device_mappings["master"]["/dev/xvdo"].ebs.snapshot_id
  79. }
  80. ebs_block_device {
  81. # /var/tmp
  82. device_name = "/dev/xvdp"
  83. # volume_size = xx
  84. delete_on_termination = true
  85. encrypted = true
  86. kms_key_id = data.aws_kms_key.ebs-key.arn
  87. snapshot_id = local.block_device_mappings["master"]["/dev/xvdp"].ebs.snapshot_id
  88. }
  89. ebs_block_device {
  90. # /var/log
  91. device_name = "/dev/xvdq"
  92. # volume_size = xx
  93. delete_on_termination = true
  94. encrypted = true
  95. kms_key_id = data.aws_kms_key.ebs-key.arn
  96. snapshot_id = local.block_device_mappings["master"]["/dev/xvdq"].ebs.snapshot_id
  97. }
  98. ebs_block_device {
  99. # /var/log/audit
  100. device_name = "/dev/xvdr"
  101. # volume_size = xx
  102. delete_on_termination = true
  103. encrypted = true
  104. kms_key_id = data.aws_kms_key.ebs-key.arn
  105. snapshot_id = local.block_device_mappings["master"]["/dev/xvdr"].ebs.snapshot_id
  106. }
  107. ebs_block_device {
  108. # /tmp
  109. device_name = "/dev/xvds"
  110. # volume_size = xx
  111. delete_on_termination = true
  112. encrypted = true
  113. kms_key_id = data.aws_kms_key.ebs-key.arn
  114. snapshot_id = local.block_device_mappings["master"]["/dev/xvds"].ebs.snapshot_id
  115. }
  116. network_interface {
  117. device_index = 0
  118. network_interface_id = aws_network_interface.instance.id
  119. }
  120. user_data = data.template_cloudinit_config.salt_master_cloud_init_config.rendered
  121. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  122. }
  123. #Uncommnet this when we are ready to make the change.
  124. # module "private_dns_record" {
  125. # source = "../../submodules/dns/private_A_record"
  126. # name = var.instance_name
  127. # ip_addresses = [ aws_instance.instance.private_ip ]
  128. # dns_info = var.dns_info
  129. # reverse_enabled = var.reverse_enabled
  130. # providers = {
  131. # aws.c2 = aws.c2
  132. # }
  133. # }
  134. # module "public_dns_record" {
  135. # source = "../../submodules/dns/public_A_record"
  136. # name = var.instance_name
  137. # ip_addresses = [ aws_eip.instance.public_ip ]
  138. # dns_info = var.dns_info
  139. # providers = {
  140. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  141. # }
  142. # }
  143. #The Cloud init data is to prepare the Salt Master for use.
  144. #This includes secrets from the AWS Secrets Manager, Github connectivity via SSH, and
  145. #prepopulating the salt master private key. May history judge me kindly.
  146. data "template_file" "salt_master_cloud_init" {
  147. # Should these be in a common directory? I suspect they'd be reusable
  148. template = "${file("${path.module}/cloud-init/cloud_init_salt_master.tpl")}"
  149. vars = {
  150. hostname = var.instance_name
  151. fqdn = "${var.instance_name}.${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. }
  158. }
  159. # Render a multi-part cloud-init config making use of the part
  160. # above, and other source files
  161. data "template_cloudinit_config" "salt_master_cloud_init_config" {
  162. gzip = true
  163. base64_encode = true
  164. # Main cloud-config configuration file.
  165. part {
  166. filename = "init.cfg"
  167. content_type = "text/cloud-config"
  168. content = "${data.template_file.salt_master_cloud_init.rendered}"
  169. }
  170. # Additional parts as needed
  171. part {
  172. content_type = "text/x-shellscript"
  173. content = "${file("${path.module}/cloud-init/provision_salt_master.sh")}"
  174. }
  175. }
  176. resource "aws_security_group" "salt_master_security_group" {
  177. name = "salt_master_security_group"
  178. description = "Security Group for Salt Master(s)"
  179. vpc_id = var.vpc_id
  180. tags = merge(var.standard_tags, var.tags)
  181. }
  182. resource "aws_security_group_rule" "http-out" {
  183. description = "For endpoints and troubleshooting"
  184. type = "egress"
  185. from_port = 80
  186. to_port = 80
  187. protocol = "tcp"
  188. cidr_blocks = [ "10.0.0.0/8" ]
  189. security_group_id = aws_security_group.salt_master_security_group.id
  190. }
  191. resource "aws_security_group_rule" "https-out" {
  192. description = "For endpoints and troubleshooting"
  193. type = "egress"
  194. from_port = 443
  195. to_port = 443
  196. protocol = "tcp"
  197. cidr_blocks = [ "10.0.0.0/8" ]
  198. security_group_id = aws_security_group.salt_master_security_group.id
  199. }
  200. resource "aws_security_group_rule" "saltstack" {
  201. description = "SaltStack"
  202. type = "ingress"
  203. from_port = "4505"
  204. to_port = "4506"
  205. protocol = "tcp"
  206. cidr_blocks = [ "10.0.0.0/8" ]
  207. security_group_id = aws_security_group.salt_master_security_group.id
  208. }
  209. resource "aws_security_group_rule" "saltstack-afs-pop" {
  210. description = "SaltStack - AFS POP"
  211. type = "ingress"
  212. from_port = "4505"
  213. to_port = "4506"
  214. protocol = "tcp"
  215. cidr_blocks = var.afs_pop
  216. security_group_id = aws_security_group.salt_master_security_group.id
  217. }
  218. resource "aws_security_group_rule" "saltstack-afs-azure-pop" {
  219. description = "SaltStack - AFS Azure POP"
  220. type = "ingress"
  221. from_port = "4505"
  222. to_port = "4506"
  223. protocol = "tcp"
  224. cidr_blocks = var.afs_azure_pop
  225. security_group_id = aws_security_group.salt_master_security_group.id
  226. }
  227. resource "aws_security_group_rule" "saltstack-nga-pop" {
  228. description = "SaltStack - NGA POP"
  229. type = "ingress"
  230. from_port = "4505"
  231. to_port = "4506"
  232. protocol = "tcp"
  233. cidr_blocks = var.nga_pop
  234. security_group_id = aws_security_group.salt_master_security_group.id
  235. }
  236. resource "aws_security_group_rule" "saltstack-xdr-interconnects" {
  237. description = "SaltStack - XDR Interconnects"
  238. type = "ingress"
  239. from_port = "4505"
  240. to_port = "4506"
  241. protocol = "tcp"
  242. cidr_blocks = var.xdr_interconnect
  243. security_group_id = aws_security_group.salt_master_security_group.id
  244. }
  245. #TODO: make this better
  246. #for now, just allow 22 outbound anywhere
  247. resource "aws_security_group_rule" "saltstack-github" {
  248. description = "SaltStack - Github Access"
  249. type = "egress"
  250. from_port = "22"
  251. to_port = "22"
  252. protocol = "tcp"
  253. cidr_blocks = [ "0.0.0.0/0" ]
  254. security_group_id = aws_security_group.salt_master_security_group.id
  255. }