main.tf 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. data "aws_security_group" "typical-host" {
  6. name = "typical-host"
  7. vpc_id = var.vpc_id
  8. }
  9. # Use the default EBS key
  10. data "aws_kms_key" "ebs-key" {
  11. key_id = "alias/ebs_root_encrypt_decrypt"
  12. }
  13. resource "aws_network_interface" "security-center-interface" {
  14. subnet_id = var.private_subnets[0]
  15. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.security_center.id]
  16. description = "security-center"
  17. tags = merge(local.standard_tags, var.tags, { Name = "security-center" })
  18. }
  19. resource "aws_instance" "security-center-instance" {
  20. tenancy = "default"
  21. ebs_optimized = true
  22. disable_api_termination = var.instance_termination_protection
  23. instance_initiated_shutdown_behavior = "stop"
  24. instance_type = "m5a.xlarge"
  25. key_name = "msoc-build"
  26. monitoring = false
  27. iam_instance_profile = "msoc-default-instance-profile"
  28. ami = local.ami_map[local.ami_selection]
  29. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  30. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  31. # that could be removed.
  32. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  33. #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  34. metadata_options {
  35. http_endpoint = "enabled"
  36. http_tokens = "optional" # tfsec:ignore:aws-ec2-enforce-http-token-imds required by salt s3 sources
  37. }
  38. # These device definitions are optional, but added for clarity.
  39. root_block_device {
  40. volume_type = "gp3"
  41. volume_size = var.environment == "prod" ? "500" : "250"
  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 = 8
  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.security-center-interface.id
  124. }
  125. user_data = data.template_cloudinit_config.cloud-init.rendered
  126. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = "security-center-0" })
  127. volume_tags = merge(local.standard_tags, var.tags, { Name = "security-center-0" })
  128. }
  129. # Render a multi-part cloud-init config making use of the part
  130. # above, and other source files
  131. data "template_cloudinit_config" "cloud-init" {
  132. gzip = true
  133. base64_encode = true
  134. # Main cloud-config configuration file.
  135. part {
  136. filename = "init.cfg"
  137. content_type = "text/cloud-config"
  138. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  139. {
  140. hostname = "security-center-0"
  141. fqdn = "security-center-0.${var.dns_info["private"]["zone"]}"
  142. environment = var.environment
  143. salt_master = local.salt_master
  144. proxy = local.proxy
  145. aws_partition = var.aws_partition
  146. aws_partition_alias = var.aws_partition_alias
  147. aws_region = var.aws_region
  148. }
  149. )
  150. }
  151. # Additional parts as needed
  152. #part {
  153. # content_type = "text/x-shellscript"
  154. # content = "ffbaz"
  155. #}
  156. }
  157. module "private_dns_record_security-center" {
  158. source = "../../../submodules/dns/private_A_record"
  159. name = "security-center-0"
  160. ip_addresses = [aws_instance.security-center-instance.private_ip]
  161. dns_info = var.dns_info
  162. reverse_enabled = var.reverse_enabled
  163. providers = {
  164. aws.c2 = aws.c2
  165. }
  166. }