main.tf 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  123. }
  124. #Uncommnet this when we are ready to make the change.
  125. # module "private_dns_record" {
  126. # source = "../../submodules/dns/private_A_record"
  127. # name = var.instance_name
  128. # ip_addresses = [ aws_instance.instance.private_ip ]
  129. # dns_info = var.dns_info
  130. # reverse_enabled = var.reverse_enabled
  131. # providers = {
  132. # aws.c2 = aws.c2
  133. # }
  134. # }
  135. # module "public_dns_record" {
  136. # source = "../../submodules/dns/public_A_record"
  137. # name = var.instance_name
  138. # ip_addresses = [ aws_eip.instance.public_ip ]
  139. # dns_info = var.dns_info
  140. # providers = {
  141. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  142. # }
  143. # }
  144. #The Cloud init data is to prepare the Salt Master for use.
  145. #This includes secrets from the AWS Secrets Manager, Github connectivity via SSH, and
  146. #prepopulating the salt master private key. May history judge me kindly.
  147. data "template_file" "salt_master_cloud_init" {
  148. # Should these be in a common directory? I suspect they'd be reusable
  149. template = "${file("${path.module}/cloud-init/cloud_init_salt_master.tpl")}"
  150. vars = {
  151. hostname = var.instance_name
  152. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  153. environment = var.environment
  154. salt_master = var.salt_master
  155. proxy = var.proxy
  156. aws_partition = var.aws_partition
  157. aws_partition_alias = var.aws_partition_alias
  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" "salt_master_cloud_init_config" {
  163. gzip = true
  164. base64_encode = true
  165. # Main cloud-config configuration file.
  166. part {
  167. filename = "init.cfg"
  168. content_type = "text/cloud-config"
  169. content = "${data.template_file.salt_master_cloud_init.rendered}"
  170. }
  171. # Additional parts as needed
  172. part {
  173. content_type = "text/x-shellscript"
  174. content = "${file("${path.module}/cloud-init/provision_salt_master.sh")}"
  175. }
  176. }
  177. resource "aws_security_group" "salt_master_security_group" {
  178. name = "salt_master_security_group"
  179. description = "Security Group for Salt Master(s)"
  180. vpc_id = var.vpc_id
  181. tags = merge(var.standard_tags, var.tags)
  182. }
  183. resource "aws_security_group_rule" "http-out" {
  184. description = "For endpoints and troubleshooting"
  185. type = "egress"
  186. from_port = 80
  187. to_port = 80
  188. protocol = "tcp"
  189. cidr_blocks = [ "10.0.0.0/8" ]
  190. security_group_id = aws_security_group.salt_master_security_group.id
  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.salt_master_security_group.id
  200. }
  201. resource "aws_security_group_rule" "saltstack" {
  202. description = "SaltStack"
  203. type = "ingress"
  204. from_port = "4505"
  205. to_port = "4506"
  206. protocol = "tcp"
  207. cidr_blocks = [ "10.0.0.0/8" ]
  208. security_group_id = aws_security_group.salt_master_security_group.id
  209. }
  210. resource "aws_security_group_rule" "saltstack-afs-pop" {
  211. description = "SaltStack - AFS POP"
  212. type = "ingress"
  213. from_port = "4505"
  214. to_port = "4506"
  215. protocol = "tcp"
  216. cidr_blocks = var.afs_pop
  217. security_group_id = aws_security_group.salt_master_security_group.id
  218. }
  219. resource "aws_security_group_rule" "saltstack-afs-azure-pop" {
  220. description = "SaltStack - AFS Azure POP"
  221. type = "ingress"
  222. from_port = "4505"
  223. to_port = "4506"
  224. protocol = "tcp"
  225. cidr_blocks = var.afs_azure_pop
  226. security_group_id = aws_security_group.salt_master_security_group.id
  227. }
  228. resource "aws_security_group_rule" "saltstack-nga-pop" {
  229. description = "SaltStack - NGA POP"
  230. type = "ingress"
  231. from_port = "4505"
  232. to_port = "4506"
  233. protocol = "tcp"
  234. cidr_blocks = var.nga_pop
  235. security_group_id = aws_security_group.salt_master_security_group.id
  236. }
  237. resource "aws_security_group_rule" "saltstack-xdr-interconnects" {
  238. description = "SaltStack - XDR Interconnects"
  239. type = "ingress"
  240. from_port = "4505"
  241. to_port = "4506"
  242. protocol = "tcp"
  243. cidr_blocks = var.xdr_interconnect
  244. security_group_id = aws_security_group.salt_master_security_group.id
  245. }
  246. #TODO: make this better
  247. #for now, just allow 22 outbound anywhere
  248. resource "aws_security_group_rule" "saltstack-github" {
  249. description = "SaltStack - Github Access"
  250. type = "egress"
  251. from_port = "22"
  252. to_port = "22"
  253. protocol = "tcp"
  254. cidr_blocks = [ "0.0.0.0/0" ]
  255. security_group_id = aws_security_group.salt_master_security_group.id
  256. }