main.tf 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = var.rhsso_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(var.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 = var.rhsso_instance_count
  19. # vpc = true
  20. # tags = merge(var.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  21. #}
  22. #resource "aws_eip_association" "instance" {
  23. # count = var.rhsso_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 = var.rhsso_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.instance_type
  34. key_name = "msoc-build"
  35. monitoring = false
  36. iam_instance_profile = "msoc-default-instance-profile"
  37. ami = local.ami_map[local.ami_selection]
  38. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  39. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  40. # that could be removed.
  41. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  42. # These device definitions are optional, but added for clarity.
  43. root_block_device {
  44. volume_type = "gp2"
  45. #volume_size = "60"
  46. delete_on_termination = true
  47. encrypted = true
  48. kms_key_id = data.aws_kms_key.ebs-key.arn
  49. }
  50. ebs_block_device {
  51. # swap
  52. device_name = "/dev/xvdm"
  53. #volume_size = 48
  54. delete_on_termination = true
  55. encrypted = true
  56. kms_key_id = data.aws_kms_key.ebs-key.arn
  57. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  58. # This may prompt replacement when the AMI is updated.
  59. # See:
  60. # https://github.com/hashicorp/terraform/issues/19958
  61. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  62. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  63. }
  64. ebs_block_device {
  65. # /home
  66. device_name = "/dev/xvdn"
  67. # volume_size = xx
  68. delete_on_termination = true
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  72. }
  73. ebs_block_device {
  74. # /var
  75. device_name = "/dev/xvdo"
  76. # volume_size = xx
  77. delete_on_termination = true
  78. encrypted = true
  79. kms_key_id = data.aws_kms_key.ebs-key.arn
  80. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var/tmp
  84. device_name = "/dev/xvdp"
  85. # volume_size = xx
  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/xvdp"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var/log
  93. device_name = "/dev/xvdq"
  94. # volume_size = xx
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var/log/audit
  102. device_name = "/dev/xvdr"
  103. # volume_size = xx
  104. delete_on_termination = true
  105. encrypted = true
  106. kms_key_id = data.aws_kms_key.ebs-key.arn
  107. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  108. }
  109. ebs_block_device {
  110. # /tmp
  111. device_name = "/dev/xvds"
  112. # volume_size = xx
  113. delete_on_termination = true
  114. encrypted = true
  115. kms_key_id = data.aws_kms_key.ebs-key.arn
  116. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  117. }
  118. network_interface {
  119. device_index = 0
  120. network_interface_id = aws_network_interface.instance[count.index].id
  121. }
  122. user_data = data.template_cloudinit_config.cloud_init_config[count.index].rendered
  123. tags = merge( var.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  124. volume_tags = merge( var.standard_tags, var.tags, { Name = "rhsso-${count.index}" })
  125. }
  126. module "private_dns_record" {
  127. count = var.rhsso_instance_count
  128. source = "../../submodules/dns/private_A_record"
  129. name = "rhsso-${count.index}"
  130. ip_addresses = [ aws_instance.instance[count.index].private_ip ]
  131. dns_info = var.dns_info
  132. reverse_enabled = var.reverse_enabled
  133. providers = {
  134. aws.c2 = aws.c2
  135. }
  136. }
  137. #module "public_dns_record" {
  138. # source = "../../submodules/dns/public_A_record"
  139. #
  140. # name = var.instance_name
  141. # ip_addresses = [ aws_eip.instance.public_ip ]
  142. # dns_info = var.dns_info
  143. #
  144. # providers = {
  145. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  146. # }
  147. #}
  148. # Render a multi-part cloud-init config making use of the part
  149. # above, and other source files
  150. data "template_cloudinit_config" "cloud_init_config" {
  151. count = var.rhsso_instance_count
  152. gzip = true
  153. base64_encode = true
  154. # Main cloud-config configuration file.
  155. part {
  156. filename = "init.cfg"
  157. content_type = "text/cloud-config"
  158. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  159. {
  160. hostname = "rhsso-${count.index}"
  161. fqdn = "rhsso-${count.index}.${var.dns_info["private"]["zone"]}"
  162. environment = var.environment
  163. salt_master = var.salt_master
  164. proxy = var.proxy
  165. aws_partition = var.aws_partition
  166. aws_partition_alias = var.aws_partition_alias
  167. aws_region = var.aws_region
  168. }
  169. )
  170. }
  171. }