main.tf 8.6 KB

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