main.tf 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. instance_name = "${ var.prefix }-splunk-sh"
  5. is_moose = length(regexall("moose", var.prefix)) > 0 ? true : false
  6. }
  7. # Rather than pass in the aws security group, we just look it up. This will
  8. # probably be useful other places, as well.
  9. data "aws_security_group" "typical-host" {
  10. name = "typical-host"
  11. vpc_id = var.vpc_id
  12. }
  13. # Use the default EBS key
  14. data "aws_kms_key" "ebs-key" {
  15. key_id = "alias/ebs_root_encrypt_decrypt"
  16. }
  17. resource "aws_network_interface" "instance" {
  18. subnet_id = var.subnets[0]
  19. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.searchhead_security_group.id ]
  20. description = local.instance_name
  21. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  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 = "msoc-default-instance-profile"
  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 = "gp2"
  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_size = 48
  50. delete_on_termination = true
  51. encrypted = true
  52. kms_key_id = data.aws_kms_key.ebs-key.arn
  53. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  54. # This may prompt replacement when the AMI is updated.
  55. # See:
  56. # https://github.com/hashicorp/terraform/issues/19958
  57. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  58. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  59. }
  60. ebs_block_device {
  61. # /home
  62. device_name = "/dev/xvdn"
  63. # volume_size = xx
  64. delete_on_termination = true
  65. encrypted = true
  66. kms_key_id = data.aws_kms_key.ebs-key.arn
  67. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  68. }
  69. ebs_block_device {
  70. # /var
  71. device_name = "/dev/xvdo"
  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/xvdo"].ebs.snapshot_id
  77. }
  78. ebs_block_device {
  79. # /var/tmp
  80. device_name = "/dev/xvdp"
  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_size = xx
  91. delete_on_termination = true
  92. encrypted = true
  93. kms_key_id = data.aws_kms_key.ebs-key.arn
  94. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  95. }
  96. ebs_block_device {
  97. # /var/log/audit
  98. device_name = "/dev/xvdr"
  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/xvdr"].ebs.snapshot_id
  104. }
  105. ebs_block_device {
  106. # /tmp
  107. device_name = "/dev/xvds"
  108. # volume_size = xx
  109. delete_on_termination = true
  110. encrypted = true
  111. kms_key_id = data.aws_kms_key.ebs-key.arn
  112. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  113. }
  114. network_interface {
  115. device_index = 0
  116. network_interface_id = aws_network_interface.instance.id
  117. }
  118. user_data = data.template_cloudinit_config.cloud-init.rendered
  119. tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  120. volume_tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  121. }
  122. module "private_dns_record" {
  123. source = "../../../submodules/dns/private_A_record"
  124. name = local.instance_name
  125. ip_addresses = [ aws_instance.instance.private_ip ]
  126. dns_info = var.dns_info
  127. reverse_enabled = var.reverse_enabled
  128. providers = {
  129. aws.c2 = aws.c2
  130. }
  131. }
  132. data "template_file" "cloud-init" {
  133. # Should these be in a common directory? I suspect they'd be reusable
  134. template = file("${path.module}/cloud-init/cloud-init.tpl")
  135. vars = {
  136. hostname = local.instance_name
  137. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  138. environment = var.environment
  139. salt_master = var.salt_master
  140. proxy = var.proxy
  141. aws_partition = var.aws_partition
  142. aws_partition_alias = var.aws_partition_alias
  143. }
  144. }
  145. # Render a multi-part cloud-init config making use of the part
  146. # above, and other source files
  147. data "template_cloudinit_config" "cloud-init" {
  148. gzip = true
  149. base64_encode = true
  150. # Main cloud-config configuration file.
  151. part {
  152. filename = "init.cfg"
  153. content_type = "text/cloud-config"
  154. content = data.template_file.cloud-init.rendered
  155. }
  156. # Additional parts as needed
  157. #part {
  158. # content_type = "text/x-shellscript"
  159. # content = "ffbaz"
  160. #}
  161. }
  162. ## Searchhead
  163. #
  164. # Summary:
  165. # Ingress:
  166. # tcp/8000 - Splunk Web - vpc-access, legacy openvpn, legacy bastion, Phantom
  167. # tcp/8089 - Splunk API - vpc-access, legacy openvpn, legacy bastion, Phantom
  168. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  169. #
  170. # Egress:
  171. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  172. resource "aws_security_group" "searchhead_security_group" {
  173. name = "searchhead_security_group"
  174. description = "Security Group for Splunk Searchhead Instance(s)"
  175. vpc_id = var.vpc_id
  176. tags = merge(var.standard_tags, var.tags)
  177. }
  178. # Ingress
  179. resource "aws_security_group_rule" "splunk-web-in" {
  180. description = "Web access"
  181. type = "ingress"
  182. from_port = 8000
  183. to_port = 8000
  184. protocol = "tcp"
  185. cidr_blocks = toset(concat(var.cidr_map["vpc-access"], var.cidr_map["vpc-private-services"]))
  186. security_group_id = aws_security_group.searchhead_security_group.id
  187. }
  188. resource "aws_security_group_rule" "splunk-api-in" {
  189. description = "Splunk API"
  190. type = "ingress"
  191. from_port = 8089
  192. to_port = 8089
  193. protocol = "tcp"
  194. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  195. var.cidr_map["vpc-private-services"],
  196. var.splunk_legacy_cidr,
  197. [ var.vpc_cidr ],
  198. ))
  199. security_group_id = aws_security_group.searchhead_security_group.id
  200. }
  201. # Egress
  202. resource "aws_security_group_rule" "splunk-api-out" {
  203. description = "Splunk API Outbound to talk to indexers"
  204. type = "egress"
  205. from_port = 8089
  206. to_port = 8089
  207. protocol = "tcp"
  208. cidr_blocks = toset(concat([ var.vpc_cidr ], var.splunk_legacy_cidr))
  209. security_group_id = aws_security_group.searchhead_security_group.id
  210. }