main.tf 6.4 KB

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