main.tf 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. # Rather than pass in the aws security group, we just look it up. This will
  6. # probably be useful other places, as well.
  7. data "aws_security_group" "typical-host" {
  8. name = "typical-host"
  9. vpc_id = var.vpc_id
  10. }
  11. # Use the default EBS key
  12. data "aws_kms_key" "ebs-key" {
  13. key_id = "alias/ebs_root_encrypt_decrypt"
  14. }
  15. resource "aws_network_interface" "instance" {
  16. subnet_id = var.private_subnets[0]
  17. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.instance_security_group.id]
  18. description = var.instance_name
  19. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_instance" "instance" {
  22. #availability_zone = var.azs[count.index % 2]
  23. tenancy = "default"
  24. ebs_optimized = true
  25. disable_api_termination = var.instance_termination_protection
  26. instance_initiated_shutdown_behavior = "stop"
  27. instance_type = var.environment == "prod" ? "m5a.xlarge" : "t3a.medium"
  28. key_name = "msoc-build"
  29. monitoring = true
  30. iam_instance_profile = "msoc-default-instance-profile"
  31. ami = local.ami_map[local.ami_selection]
  32. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  33. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  34. # that could be removed.
  35. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  36. metadata_options {
  37. http_endpoint = "enabled"
  38. http_tokens = "required"
  39. }
  40. # These device definitions are optional, but added for clarity.
  41. root_block_device {
  42. volume_type = "gp3"
  43. #volume_size = "60"
  44. delete_on_termination = true
  45. encrypted = true
  46. kms_key_id = data.aws_kms_key.ebs-key.arn
  47. }
  48. ebs_block_device {
  49. # swap
  50. device_name = "/dev/xvdm"
  51. volume_type = "gp3"
  52. volume_size = 48
  53. delete_on_termination = true
  54. encrypted = true
  55. kms_key_id = data.aws_kms_key.ebs-key.arn
  56. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  57. # This may prompt replacement when the AMI is updated.
  58. # See:
  59. # https://github.com/hashicorp/terraform/issues/19958
  60. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  61. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  62. }
  63. ebs_block_device {
  64. # /home
  65. device_name = "/dev/xvdn"
  66. volume_type = "gp3"
  67. # volume_size = xx
  68. delete_on_termination = true
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  72. }
  73. ebs_block_device {
  74. # /var
  75. device_name = "/dev/xvdo"
  76. volume_type = "gp3"
  77. # volume_size = xx
  78. iops = 6000 # etcd requires extra performance
  79. throughput = 250 # etcd requires extra performance
  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 = 16
  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.rendered
  130. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  131. volume_tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  132. }
  133. # Render a multi-part cloud-init config making use of the part
  134. # above, and other source files
  135. data "template_cloudinit_config" "cloud-init" {
  136. gzip = true
  137. base64_encode = true
  138. # Main cloud-config configuration file.
  139. part {
  140. filename = "init.cfg"
  141. content_type = "text/cloud-config"
  142. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  143. {
  144. hostname = var.instance_name
  145. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  146. environment = var.environment
  147. salt_master = local.salt_master
  148. proxy = local.proxy
  149. aws_partition = var.aws_partition
  150. aws_partition_alias = var.aws_partition_alias
  151. aws_region = var.aws_region
  152. }
  153. )
  154. }
  155. # Additional parts as needed
  156. #part {
  157. # content_type = "text/x-shellscript"
  158. # content = "ffbaz"
  159. #}
  160. }
  161. module "private_dns_record" {
  162. source = "../../submodules/dns/private_A_record"
  163. name = "${var.instance_name}-0"
  164. ip_addresses = [aws_instance.instance.private_ip]
  165. dns_info = var.dns_info
  166. reverse_enabled = var.reverse_enabled
  167. providers = {
  168. aws.c2 = aws.c2
  169. }
  170. }
  171. #----------------------------------------------------------------------------
  172. # Server SG
  173. #----------------------------------------------------------------------------
  174. resource "aws_security_group" "instance_security_group" {
  175. name = "${var.instance_name}_security_group"
  176. description = "Security Group for ${var.instance_name}(s)"
  177. vpc_id = var.vpc_id
  178. tags = merge(local.standard_tags, var.tags)
  179. }
  180. #----------------------------------------------------------------------------
  181. # INGRESS
  182. #----------------------------------------------------------------------------
  183. resource "aws_security_group_rule" "sensu_ui" {
  184. type = "ingress"
  185. from_port = 8000
  186. to_port = 8000
  187. protocol = "tcp"
  188. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  189. description = "Sensu UI"
  190. security_group_id = aws_security_group.instance_security_group.id
  191. }
  192. resource "aws_security_group_rule" "sensu_agent_internal" {
  193. type = "ingress"
  194. from_port = 8081
  195. to_port = 8081
  196. protocol = "tcp"
  197. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  198. description = "Internal Sensu Agents"
  199. security_group_id = aws_security_group.instance_security_group.id
  200. }
  201. resource "aws_security_group_rule" "sensu_api" {
  202. type = "ingress"
  203. from_port = 8080
  204. to_port = 8080
  205. protocol = "tcp"
  206. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  207. description = "Sensu API"
  208. security_group_id = aws_security_group.instance_security_group.id
  209. }
  210. resource "aws_security_group_rule" "sensu_agent_external" {
  211. type = "ingress"
  212. from_port = 8081
  213. to_port = 8081
  214. protocol = "tcp"
  215. source_security_group_id = module.elb.security_group_id
  216. description = "External Sensu Agents"
  217. security_group_id = aws_security_group.instance_security_group.id
  218. }
  219. resource "aws_security_group_rule" "sensu_api_external" {
  220. type = "ingress"
  221. from_port = 8080
  222. to_port = 8080
  223. protocol = "tcp"
  224. source_security_group_id = module.elb.security_group_id
  225. description = "External Sensu API"
  226. security_group_id = aws_security_group.instance_security_group.id
  227. }