main.tf 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. subnet_id = var.subnets[0]
  11. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.instance.id ]
  12. description = var.instance_name
  13. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  14. }
  15. resource "aws_eip" "instance" {
  16. vpc = true
  17. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  18. }
  19. resource "aws_eip_association" "instance" {
  20. network_interface_id = aws_network_interface.instance.id
  21. allocation_id = aws_eip.instance.id
  22. }
  23. resource "aws_instance" "instance" {
  24. #availability_zone = var.azs[count.index % 2]
  25. tenancy = "default"
  26. ebs_optimized = true
  27. disable_api_termination = var.instance_termination_protection
  28. instance_initiated_shutdown_behavior = "stop"
  29. instance_type = var.instance_type
  30. key_name = "msoc-build"
  31. monitoring = false
  32. iam_instance_profile = aws_iam_instance_profile.teleport.name
  33. ami = local.ami_map[local.ami_selection]
  34. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  35. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  36. # that could be removed.
  37. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  38. # These device definitions are optional, but added for clarity.
  39. root_block_device {
  40. volume_type = "gp3"
  41. #volume_size = "60"
  42. delete_on_termination = true
  43. encrypted = true
  44. kms_key_id = data.aws_kms_key.ebs-key.arn
  45. }
  46. ebs_block_device {
  47. # swap
  48. device_name = "/dev/xvdm"
  49. volume_type = "gp3"
  50. #volume_size = 48
  51. delete_on_termination = true
  52. encrypted = true
  53. kms_key_id = data.aws_kms_key.ebs-key.arn
  54. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  55. # This may prompt replacement when the AMI is updated.
  56. # See:
  57. # https://github.com/hashicorp/terraform/issues/19958
  58. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  59. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  60. }
  61. ebs_block_device {
  62. # /home
  63. device_name = "/dev/xvdn"
  64. volume_type = "gp3"
  65. # volume_size = xx
  66. delete_on_termination = true
  67. encrypted = true
  68. kms_key_id = data.aws_kms_key.ebs-key.arn
  69. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  70. }
  71. ebs_block_device {
  72. # /var
  73. device_name = "/dev/xvdo"
  74. volume_type = "gp3"
  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_type = "gp3"
  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_type = "gp3"
  95. # volume_size = xx
  96. delete_on_termination = true
  97. encrypted = true
  98. kms_key_id = data.aws_kms_key.ebs-key.arn
  99. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  100. }
  101. ebs_block_device {
  102. # /var/log/audit
  103. device_name = "/dev/xvdr"
  104. volume_type = "gp3"
  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/xvdr"].ebs.snapshot_id
  110. }
  111. ebs_block_device {
  112. # /tmp
  113. device_name = "/dev/xvds"
  114. volume_type = "gp3"
  115. # volume_size = xx
  116. delete_on_termination = true
  117. encrypted = true
  118. kms_key_id = data.aws_kms_key.ebs-key.arn
  119. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  120. }
  121. network_interface {
  122. device_index = 0
  123. network_interface_id = aws_network_interface.instance.id
  124. }
  125. user_data = data.template_cloudinit_config.cloud_init_config.rendered
  126. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  127. volume_tags = merge( var.standard_tags, var.tags, var.instance_tags, { Name = var.instance_name })
  128. }
  129. module "private_dns_record" {
  130. source = "../../submodules/dns/private_A_record"
  131. name = var.instance_name
  132. ip_addresses = [ aws_instance.instance.private_ip ]
  133. dns_info = var.dns_info
  134. reverse_enabled = var.reverse_enabled
  135. providers = {
  136. aws.c2 = aws.c2
  137. }
  138. }
  139. # Render a multi-part cloud-init config making use of the part
  140. # above, and other source files
  141. data "template_cloudinit_config" "cloud_init_config" {
  142. gzip = true
  143. base64_encode = true
  144. # Main cloud-config configuration file.
  145. part {
  146. filename = "init.cfg"
  147. content_type = "text/cloud-config"
  148. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  149. {
  150. hostname = var.instance_name
  151. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  152. environment = var.environment
  153. salt_master = var.salt_master
  154. proxy = var.proxy
  155. aws_partition = var.aws_partition
  156. aws_partition_alias = var.aws_partition_alias
  157. aws_region = var.aws_region
  158. }
  159. )
  160. }
  161. }