main.tf 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #------------------------------------
  16. # EC2 ASG
  17. #------------------------------------
  18. resource "aws_launch_template" "customer_portal" {
  19. name = "customer-portal-lt"
  20. instance_type = "t3a.medium"
  21. image_id = local.ami_map[local.ami_selection]
  22. user_data = data.template_cloudinit_config.cloud-init.rendered
  23. ebs_optimized = true
  24. tags = merge(local.standard_tags, var.instance_tags, var.tags)
  25. key_name = "msoc-build"
  26. metadata_options {
  27. http_endpoint = "enabled"
  28. # checkov:skip=CKV_AWS_79:see tfsec explanation
  29. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  30. # tfsec:ignore:aws-autoscaling-enforce-http-token-imds
  31. http_tokens = "optional"
  32. }
  33. iam_instance_profile {
  34. name = aws_iam_instance_profile.portal_server_instance_profile.name
  35. }
  36. network_interfaces {
  37. delete_on_termination = true
  38. associate_public_ip_address = false
  39. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.customer_portal.id]
  40. }
  41. block_device_mappings {
  42. device_name = "/dev/sda1"
  43. ebs {
  44. volume_type = "gp3"
  45. volume_size = "100"
  46. delete_on_termination = true
  47. encrypted = true
  48. kms_key_id = data.aws_kms_key.ebs-key.arn
  49. }
  50. }
  51. block_device_mappings {
  52. # swap
  53. device_name = "/dev/xvdm"
  54. ebs {
  55. volume_type = "gp3"
  56. volume_size = "8"
  57. delete_on_termination = true
  58. encrypted = true
  59. kms_key_id = data.aws_kms_key.ebs-key.arn
  60. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  61. # This may prompt replacement when the AMI is updated.
  62. # See:
  63. # https://github.com/hashicorp/terraform/issues/19958
  64. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  65. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  66. }
  67. }
  68. block_device_mappings {
  69. # /home
  70. device_name = "/dev/xvdn"
  71. ebs {
  72. volume_type = "gp3"
  73. volume_size = "4"
  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. }
  80. block_device_mappings {
  81. # /var
  82. device_name = "/dev/xvdo"
  83. ebs {
  84. volume_type = "gp3"
  85. volume_size = "15"
  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. }
  92. block_device_mappings {
  93. # /var/tmp
  94. device_name = "/dev/xvdp"
  95. ebs {
  96. volume_type = "gp3"
  97. volume_size = "4"
  98. delete_on_termination = true
  99. encrypted = true
  100. kms_key_id = data.aws_kms_key.ebs-key.arn
  101. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  102. }
  103. }
  104. block_device_mappings {
  105. # /var/log
  106. device_name = "/dev/xvdq"
  107. ebs {
  108. volume_type = "gp3"
  109. volume_size = "8"
  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/xvdq"].ebs.snapshot_id
  114. }
  115. }
  116. block_device_mappings {
  117. # /var/log/audit
  118. device_name = "/dev/xvdr"
  119. ebs {
  120. volume_type = "gp3"
  121. volume_size = "8"
  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/xvdr"].ebs.snapshot_id
  126. }
  127. }
  128. block_device_mappings {
  129. # /tmp
  130. device_name = "/dev/xvds"
  131. ebs {
  132. volume_type = "gp3"
  133. volume_size = "4"
  134. delete_on_termination = true
  135. encrypted = true
  136. kms_key_id = data.aws_kms_key.ebs-key.arn
  137. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  138. }
  139. }
  140. tag_specifications {
  141. resource_type = "instance"
  142. tags = merge(var.tags, var.instance_tags, { "Name" : var.instance_name }) # This may have no effect?
  143. }
  144. tag_specifications {
  145. resource_type = "volume"
  146. tags = merge(var.tags, { "Name" : var.instance_name }) # This may have no effect
  147. }
  148. lifecycle {
  149. create_before_destroy = true
  150. }
  151. }
  152. resource "aws_autoscaling_group" "customer_portal" {
  153. name = "customer-portal-asg"
  154. launch_template {
  155. id = aws_launch_template.customer_portal.id
  156. version = "$Latest"
  157. }
  158. vpc_zone_identifier = var.private_subnets
  159. min_size = 1
  160. max_size = 2
  161. desired_capacity = 2
  162. wait_for_capacity_timeout = 0
  163. health_check_type = "EC2"
  164. tag {
  165. key = "Name"
  166. value = var.instance_name
  167. propagate_at_launch = true
  168. }
  169. # Must ignore changes to attachments, or tf will flip flop
  170. lifecycle {
  171. ignore_changes = [load_balancers, target_group_arns]
  172. }
  173. }
  174. # Render a multi-part cloud-init config making use of the part
  175. # above, and other source files
  176. data "template_cloudinit_config" "cloud-init" {
  177. gzip = true
  178. base64_encode = true
  179. # Main cloud-config configuration file.
  180. part {
  181. filename = "init.cfg"
  182. content_type = "text/cloud-config"
  183. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  184. {
  185. zone = var.dns_info["private"]["zone"]
  186. environment = var.environment
  187. salt_master = local.salt_master
  188. proxy = local.proxy
  189. aws_partition = var.aws_partition
  190. aws_partition_alias = var.aws_partition_alias
  191. aws_region = var.aws_region
  192. }
  193. )
  194. }
  195. # Additional parts as needed
  196. #part {
  197. # content_type = "text/x-shellscript"
  198. # content = "ffbaz"
  199. #}
  200. }
  201. #------------------------------------
  202. # S3 Bucket What is this used for? Uncomment if needed.
  203. #------------------------------------
  204. # resource "aws_s3_bucket" "customer-portal" {
  205. # bucket = "dps-customer-portal-${terraform.workspace}"
  206. # acl = "private"
  207. # tags = merge(local.standard_tags, var.tags, )
  208. # }
  209. #----------------------------------------------------------------------------
  210. # Portal Security Group
  211. #----------------------------------------------------------------------------
  212. resource "aws_security_group" "customer_portal" {
  213. name = "customer_portal_http_inbound_sg"
  214. description = "Allow Customer Portal HTTP Inbound From ALB"
  215. vpc_id = var.vpc_id
  216. }
  217. #----------------------------------------------------------------------------
  218. # INGRESS
  219. #----------------------------------------------------------------------------
  220. resource "aws_security_group_rule" "customer_portal" {
  221. type = "ingress"
  222. description = "HTTPS - Inbound"
  223. from_port = 443
  224. to_port = 443
  225. protocol = "tcp"
  226. security_group_id = aws_security_group.customer_portal.id
  227. source_security_group_id = aws_security_group.customer_portal_alb.id
  228. }
  229. #----------------------------------------------------------------------------
  230. # EGRESS
  231. #----------------------------------------------------------------------------
  232. resource "aws_security_group_rule" "customer_portal_postgres_outbound" {
  233. type = "egress"
  234. description = "Postgres - Outbound"
  235. from_port = 5432
  236. to_port = 5432
  237. protocol = "tcp"
  238. security_group_id = aws_security_group.customer_portal.id
  239. source_security_group_id = aws_security_group.postgres.id
  240. }
  241. resource "aws_security_group_rule" "customer_portal_http_outbound" {
  242. type = "egress"
  243. description = "HTTP - Outbound"
  244. from_port = 80
  245. to_port = 80
  246. protocol = "tcp"
  247. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-egress-sgr
  248. security_group_id = aws_security_group.customer_portal.id
  249. }
  250. resource "aws_security_group_rule" "customer_portal_https_outbound" {
  251. type = "egress"
  252. description = "HTTPS - Outbound"
  253. from_port = 443
  254. to_port = 443
  255. protocol = "tcp"
  256. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-egress-sgr
  257. security_group_id = aws_security_group.customer_portal.id
  258. }
  259. resource "aws_security_group_rule" "customer_portal_smtps_outbound" {
  260. type = "egress"
  261. description = "SMTPS - Outbound"
  262. from_port = 465
  263. to_port = 465
  264. protocol = "tcp"
  265. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-egress-sgr
  266. security_group_id = aws_security_group.customer_portal.id
  267. }
  268. ### Output environment ID for purposes
  269. #output portal_env_id {
  270. # value = "${aws_elastic_beanstalk_environment.mdr-customer-portal-env.id}"
  271. #}