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