main.tf 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. # TODO: switch this to Launch Template for gp3 volume usage.
  19. # https://github.com/terraform-community-modules/tf_aws_asg_elb/issues/11
  20. module "customer_portal_asg" {
  21. source = "terraform-aws-modules/autoscaling/aws"
  22. version = "3.9.0"
  23. name = "customer-portal"
  24. lc_name = "customer-portal-lc"
  25. iam_instance_profile = aws_iam_instance_profile.portal_server_instance_profile.name
  26. image_id = local.ami_map[local.ami_selection]
  27. instance_type = var.instance_type
  28. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.customer_portal.id ]
  29. user_data = data.template_cloudinit_config.cloud-init.rendered
  30. key_name = "msoc-build"
  31. ebs_optimized = true
  32. target_group_arns = [ aws_alb_target_group.portal.arn, ]
  33. root_block_device = [
  34. {
  35. volume_type = "gp2"
  36. volume_size = "100"
  37. delete_on_termination = true
  38. encrypted = true
  39. kms_key_id = data.aws_kms_key.ebs-key.arn
  40. },
  41. ]
  42. ebs_block_device = [
  43. {
  44. # swap
  45. device_name = "/dev/xvdm"
  46. #volume_size = xx
  47. delete_on_termination = true
  48. encrypted = true
  49. kms_key_id = data.aws_kms_key.ebs-key.arn
  50. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  51. # This may prompt replacement when the AMI is updated.
  52. # See:
  53. # https://github.com/hashicorp/terraform/issues/19958
  54. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  55. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  56. },
  57. {
  58. # /home
  59. device_name = "/dev/xvdn"
  60. # volume_size = xx
  61. delete_on_termination = true
  62. encrypted = true
  63. kms_key_id = data.aws_kms_key.ebs-key.arn
  64. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  65. },
  66. {
  67. # /var
  68. device_name = "/dev/xvdo"
  69. # volume_size = xx
  70. delete_on_termination = true
  71. encrypted = true
  72. kms_key_id = data.aws_kms_key.ebs-key.arn
  73. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  74. },
  75. {
  76. # /var/tmp
  77. device_name = "/dev/xvdp"
  78. # volume_size = xx
  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/xvdp"].ebs.snapshot_id
  83. },
  84. {
  85. # /var/log
  86. device_name = "/dev/xvdq"
  87. # volume_size = xx
  88. delete_on_termination = true
  89. encrypted = true
  90. kms_key_id = data.aws_kms_key.ebs-key.arn
  91. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  92. },
  93. {
  94. # /var/log/audit
  95. device_name = "/dev/xvdr"
  96. # volume_size = xx
  97. delete_on_termination = true
  98. encrypted = true
  99. kms_key_id = data.aws_kms_key.ebs-key.arn
  100. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  101. },
  102. {
  103. # /tmp
  104. device_name = "/dev/xvds"
  105. # volume_size = xx
  106. delete_on_termination = true
  107. encrypted = true
  108. kms_key_id = data.aws_kms_key.ebs-key.arn
  109. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  110. },
  111. ]
  112. # Auto scaling group
  113. asg_name = "customer-portal-asg"
  114. vpc_zone_identifier = var.private_subnets
  115. health_check_type = "EC2"
  116. min_size = 1
  117. max_size = 2
  118. desired_capacity = 2
  119. wait_for_capacity_timeout = 0
  120. tags_as_map = merge(var.standard_tags, var.tags)
  121. }
  122. data "template_file" "cloud-init" {
  123. # Should these be in a common directory? I suspect they'd be reusable
  124. template = file("${path.module}/cloud-init/cloud-init.tpl")
  125. vars = {
  126. zone = var.dns_info["private"]["zone"]
  127. environment = var.environment
  128. salt_master = var.salt_master
  129. proxy = var.proxy
  130. aws_partition = var.aws_partition
  131. aws_partition_alias = var.aws_partition_alias
  132. aws_region = var.aws_region
  133. }
  134. }
  135. # Render a multi-part cloud-init config making use of the part
  136. # above, and other source files
  137. data "template_cloudinit_config" "cloud-init" {
  138. gzip = true
  139. base64_encode = true
  140. # Main cloud-config configuration file.
  141. part {
  142. filename = "init.cfg"
  143. content_type = "text/cloud-config"
  144. content = data.template_file.cloud-init.rendered
  145. }
  146. # Additional parts as needed
  147. #part {
  148. # content_type = "text/x-shellscript"
  149. # content = "ffbaz"
  150. #}
  151. }
  152. #------------------------------------
  153. # S3 Bucket What is this used for? Uncomment if needed.
  154. #------------------------------------
  155. # resource "aws_s3_bucket" "customer-portal" {
  156. # bucket = "dps-customer-portal-${terraform.workspace}"
  157. # acl = "private"
  158. # tags = merge(var.standard_tags, var.tags, )
  159. # }
  160. #------------------------------------
  161. # Security Groups
  162. #------------------------------------
  163. resource "aws_security_group" "customer_portal" {
  164. name = "customer_portal_http_inbound_sg"
  165. description = "Allow Customer Portal HTTP Inbound From ALB"
  166. vpc_id = var.vpc_id
  167. }
  168. resource "aws_security_group_rule" "customer_portal" {
  169. protocol = "tcp"
  170. type = "ingress"
  171. from_port = 443
  172. to_port = 443
  173. security_group_id = aws_security_group.customer_portal.id
  174. source_security_group_id = aws_security_group.customer_portal_alb.id
  175. }
  176. resource "aws_security_group_rule" "customer_portal_postgres_outbound" {
  177. type = "egress"
  178. from_port = 5432
  179. to_port = 5432
  180. protocol = "tcp"
  181. security_group_id = aws_security_group.customer_portal.id
  182. source_security_group_id = aws_security_group.postgres.id
  183. }
  184. resource "aws_security_group_rule" "customer_portal_http_outbound" {
  185. type = "egress"
  186. from_port = 80
  187. to_port = 80
  188. protocol = "tcp"
  189. cidr_blocks = ["0.0.0.0/0"]
  190. security_group_id = aws_security_group.customer_portal.id
  191. }
  192. resource "aws_security_group_rule" "customer_portal_https_outbound" {
  193. type = "egress"
  194. from_port = 443
  195. to_port = 443
  196. protocol = "tcp"
  197. cidr_blocks = ["0.0.0.0/0"]
  198. security_group_id = aws_security_group.customer_portal.id
  199. }
  200. ### Output environment ID for purposes
  201. #output portal_env_id {
  202. # value = "${aws_elastic_beanstalk_environment.mdr-customer-portal-env.id}"
  203. #}