main.tf 5.6 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(var.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 = var.instance_type
  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. # These device definitions are optional, but added for clarity.
  35. root_block_device {
  36. volume_type = "gp3"
  37. volume_size = "250"
  38. delete_on_termination = true
  39. encrypted = true
  40. kms_key_id = data.aws_kms_key.ebs-key.arn
  41. }
  42. ebs_block_device {
  43. # swap
  44. device_name = "/dev/xvdm"
  45. volume_type = "gp3"
  46. volume_size = 8
  47. delete_on_termination = true
  48. encrypted = true
  49. kms_key_id = data.aws_kms_key.ebs-key.arn
  50. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  51. # This may prompt replacement when the AMI is updated.
  52. # See:
  53. # https://github.com/hashicorp/terraform/issues/19958
  54. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  55. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  56. }
  57. ebs_block_device {
  58. # /home
  59. device_name = "/dev/xvdn"
  60. volume_type = "gp3"
  61. # volume_size = xx
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  66. }
  67. ebs_block_device {
  68. # /var
  69. device_name = "/dev/xvdo"
  70. volume_type = "gp3"
  71. # volume_size = xx
  72. delete_on_termination = true
  73. encrypted = true
  74. kms_key_id = data.aws_kms_key.ebs-key.arn
  75. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  76. }
  77. ebs_block_device {
  78. # /var/tmp
  79. device_name = "/dev/xvdp"
  80. volume_type = "gp3"
  81. # volume_size = xx
  82. delete_on_termination = true
  83. encrypted = true
  84. kms_key_id = data.aws_kms_key.ebs-key.arn
  85. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  86. }
  87. ebs_block_device {
  88. # /var/log
  89. device_name = "/dev/xvdq"
  90. volume_type = "gp3"
  91. # volume_size = xx
  92. delete_on_termination = true
  93. encrypted = true
  94. kms_key_id = data.aws_kms_key.ebs-key.arn
  95. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  96. }
  97. ebs_block_device {
  98. # /var/log/audit
  99. device_name = "/dev/xvdr"
  100. volume_type = "gp3"
  101. # volume_size = xx
  102. delete_on_termination = true
  103. encrypted = true
  104. kms_key_id = data.aws_kms_key.ebs-key.arn
  105. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  106. }
  107. ebs_block_device {
  108. # /tmp
  109. device_name = "/dev/xvds"
  110. volume_type = "gp3"
  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.security-center-interface.id
  120. }
  121. user_data = data.template_cloudinit_config.cloud-init.rendered
  122. tags = merge( var.standard_tags, var.tags, { Name = "security-center-0" })
  123. volume_tags = merge( var.standard_tags, var.tags, { Name = "security-center-0" })
  124. }
  125. data "template_file" "cloud-init" {
  126. # Should these be in a common directory? I suspect they'd be reusable
  127. template = file("${path.module}/cloud-init/cloud-init.tpl")
  128. vars = {
  129. hostname = "security-center-0"
  130. fqdn = "security-center-0.${var.dns_info["private"]["zone"]}"
  131. environment = var.environment
  132. salt_master = var.salt_master
  133. proxy = var.proxy
  134. aws_partition = var.aws_partition
  135. aws_partition_alias = var.aws_partition_alias
  136. aws_region = var.aws_region
  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" {
  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 = data.template_file.cloud-init.rendered
  149. }
  150. # Additional parts as needed
  151. #part {
  152. # content_type = "text/x-shellscript"
  153. # content = "ffbaz"
  154. #}
  155. }
  156. module "private_dns_record_security-center" {
  157. source = "../../../submodules/dns/private_A_record"
  158. name = "security-center-0"
  159. ip_addresses = [ aws_instance.security-center-instance.private_ip ]
  160. dns_info = var.dns_info
  161. reverse_enabled = var.reverse_enabled
  162. providers = {
  163. aws.c2 = aws.c2
  164. }
  165. }