main.tf 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. # Use the default EBS key
  6. data "aws_kms_key" "ebs-key" {
  7. key_id = "alias/ebs_root_encrypt_decrypt"
  8. }
  9. resource "aws_network_interface" "instance" {
  10. count = local.instance_count
  11. subnet_id = var.public_subnets[count.index % 3]
  12. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.instance.id]
  13. description = "rhsso-${count.index}"
  14. tags = merge(local.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  15. }
  16. # Even though it's in the public subnet, no EIP, as it's handled by the NLB. It must use the proxy for outbound access.
  17. #resource "aws_eip" "instance" {
  18. # count = local.instance_count
  19. # vpc = true
  20. # tags = merge(local.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  21. #}
  22. #resource "aws_eip_association" "instance" {
  23. # count = local.instance_count
  24. # network_interface_id = aws_network_interface.instance[count.index].id
  25. # allocation_id = aws_eip.instance[count.index].id
  26. #}
  27. resource "aws_instance" "instance" {
  28. count = local.instance_count
  29. tenancy = "default"
  30. ebs_optimized = true
  31. disable_api_termination = var.instance_termination_protection
  32. instance_initiated_shutdown_behavior = "stop"
  33. instance_type = var.environment == "prod" ? "m5a.large" : "t3a.large"
  34. key_name = "msoc-build"
  35. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  36. iam_instance_profile = "msoc-default-instance-profile"
  37. metadata_options {
  38. http_endpoint = "enabled"
  39. # checkov:skip=CKV_AWS_79:see tfsec explanation
  40. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  41. http_tokens = "optional"
  42. }
  43. ami = local.ami_map[local.ami_selection]
  44. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  45. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  46. # that could be removed.
  47. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  48. # These device definitions are optional, but added for clarity.
  49. root_block_device {
  50. volume_type = "gp2"
  51. #volume_size = "60"
  52. delete_on_termination = true
  53. encrypted = true
  54. kms_key_id = data.aws_kms_key.ebs-key.arn
  55. }
  56. ebs_block_device {
  57. # swap
  58. device_name = "/dev/xvdm"
  59. #volume_size = 48
  60. delete_on_termination = true
  61. encrypted = true
  62. kms_key_id = data.aws_kms_key.ebs-key.arn
  63. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  64. # This may prompt replacement when the AMI is updated.
  65. # See:
  66. # https://github.com/hashicorp/terraform/issues/19958
  67. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  68. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  69. }
  70. ebs_block_device {
  71. # /home
  72. device_name = "/dev/xvdn"
  73. # volume_size = xx
  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. ebs_block_device {
  80. # /var
  81. device_name = "/dev/xvdo"
  82. # volume_size = xx
  83. delete_on_termination = true
  84. encrypted = true
  85. kms_key_id = data.aws_kms_key.ebs-key.arn
  86. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  87. }
  88. ebs_block_device {
  89. # /var/tmp
  90. device_name = "/dev/xvdp"
  91. # volume_size = xx
  92. delete_on_termination = true
  93. encrypted = true
  94. kms_key_id = data.aws_kms_key.ebs-key.arn
  95. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  96. }
  97. ebs_block_device {
  98. # /var/log
  99. device_name = "/dev/xvdq"
  100. # volume_size = xx
  101. delete_on_termination = true
  102. encrypted = true
  103. kms_key_id = data.aws_kms_key.ebs-key.arn
  104. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  105. }
  106. ebs_block_device {
  107. # /var/log/audit
  108. device_name = "/dev/xvdr"
  109. # volume_size = xx
  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/xvdr"].ebs.snapshot_id
  114. }
  115. ebs_block_device {
  116. # /tmp
  117. device_name = "/dev/xvds"
  118. # volume_size = xx
  119. delete_on_termination = true
  120. encrypted = true
  121. kms_key_id = data.aws_kms_key.ebs-key.arn
  122. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  123. }
  124. network_interface {
  125. device_index = 0
  126. network_interface_id = aws_network_interface.instance[count.index].id
  127. }
  128. user_data = data.template_cloudinit_config.cloud_init_config[count.index].rendered
  129. tags = merge(local.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  130. volume_tags = merge(local.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  131. }
  132. module "private_dns_record" {
  133. count = local.instance_count
  134. source = "../../submodules/dns/private_A_record"
  135. name = "rhsso-${count.index}"
  136. ip_addresses = [aws_instance.instance[count.index].private_ip]
  137. dns_info = var.dns_info
  138. reverse_enabled = var.reverse_enabled
  139. providers = {
  140. aws.c2 = aws.c2
  141. }
  142. }
  143. #module "public_dns_record" {
  144. # source = "../../submodules/dns/public_A_record"
  145. #
  146. # name = var.instance_name
  147. # ip_addresses = [ aws_eip.instance.public_ip ]
  148. # dns_info = var.dns_info
  149. #
  150. # providers = {
  151. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  152. # }
  153. #}
  154. # Render a multi-part cloud-init config making use of the part
  155. # above, and other source files
  156. data "template_cloudinit_config" "cloud_init_config" {
  157. count = local.instance_count
  158. gzip = true
  159. base64_encode = true
  160. # Main cloud-config configuration file.
  161. part {
  162. filename = "init.cfg"
  163. content_type = "text/cloud-config"
  164. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  165. {
  166. hostname = "rhsso-${count.index}"
  167. fqdn = "rhsso-${count.index}.${var.dns_info["private"]["zone"]}"
  168. environment = var.environment
  169. salt_master = local.salt_master
  170. proxy = local.proxy
  171. aws_partition = var.aws_partition
  172. aws_partition_alias = var.aws_partition_alias
  173. aws_region = var.aws_region
  174. }
  175. )
  176. }
  177. }