main.tf 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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(local.standard_tags, var.tags, { Name = var.instance_name })
  14. }
  15. resource "aws_eip" "instance" {
  16. vpc = true
  17. tags = merge(local.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 = "t3a.large"
  30. key_name = "msoc-build"
  31. monitoring = false
  32. iam_instance_profile = aws_iam_instance_profile.teleport.name
  33. metadata_options {
  34. http_endpoint = "enabled"
  35. http_tokens = "optional" # tfsec:ignore:aws-ec2-enforce-http-token-imds salt s3 sources require optional tokens; see https://github.com/saltstack/salt/issues/60668
  36. }
  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 = "gp3"
  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_type = "gp3"
  54. #volume_size = 48
  55. delete_on_termination = true
  56. encrypted = true
  57. kms_key_id = data.aws_kms_key.ebs-key.arn
  58. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  59. # This may prompt replacement when the AMI is updated.
  60. # See:
  61. # https://github.com/hashicorp/terraform/issues/19958
  62. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  63. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  64. }
  65. ebs_block_device {
  66. # /home
  67. device_name = "/dev/xvdn"
  68. volume_type = "gp3"
  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/xvdn"].ebs.snapshot_id
  74. }
  75. ebs_block_device {
  76. # /var
  77. device_name = "/dev/xvdo"
  78. volume_type = "gp3"
  79. # volume_size = xx
  80. delete_on_termination = true
  81. encrypted = true
  82. kms_key_id = data.aws_kms_key.ebs-key.arn
  83. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  84. }
  85. ebs_block_device {
  86. # /var/tmp
  87. device_name = "/dev/xvdp"
  88. volume_type = "gp3"
  89. # volume_size = xx
  90. delete_on_termination = true
  91. encrypted = true
  92. kms_key_id = data.aws_kms_key.ebs-key.arn
  93. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  94. }
  95. ebs_block_device {
  96. # /var/log
  97. device_name = "/dev/xvdq"
  98. volume_type = "gp3"
  99. # volume_size = xx
  100. delete_on_termination = true
  101. encrypted = true
  102. kms_key_id = data.aws_kms_key.ebs-key.arn
  103. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  104. }
  105. ebs_block_device {
  106. # /var/log/audit
  107. device_name = "/dev/xvdr"
  108. volume_type = "gp3"
  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_type = "gp3"
  119. # volume_size = xx
  120. delete_on_termination = true
  121. encrypted = true
  122. kms_key_id = data.aws_kms_key.ebs-key.arn
  123. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  124. }
  125. network_interface {
  126. device_index = 0
  127. network_interface_id = aws_network_interface.instance.id
  128. }
  129. user_data = data.template_cloudinit_config.cloud_init_config.rendered
  130. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  131. volume_tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = var.instance_name })
  132. }
  133. module "private_dns_record" {
  134. source = "../../submodules/dns/private_A_record"
  135. name = var.instance_name
  136. ip_addresses = [aws_instance.instance.private_ip]
  137. dns_info = var.dns_info
  138. reverse_enabled = var.reverse_enabled
  139. providers = {
  140. aws.c2 = aws.c2
  141. }
  142. }
  143. # Render a multi-part cloud-init config making use of the part
  144. # above, and other source files
  145. data "template_cloudinit_config" "cloud_init_config" {
  146. gzip = true
  147. base64_encode = true
  148. # Main cloud-config configuration file.
  149. part {
  150. filename = "init.cfg"
  151. content_type = "text/cloud-config"
  152. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  153. {
  154. hostname = var.instance_name
  155. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  156. environment = var.environment
  157. salt_master = local.salt_master
  158. proxy = local.proxy
  159. aws_partition = var.aws_partition
  160. aws_partition_alias = var.aws_partition_alias
  161. aws_region = var.aws_region
  162. }
  163. )
  164. }
  165. }