main.tf 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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(var.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.instance_type
  28. key_name = "msoc-build"
  29. monitoring = false
  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. # These device definitions are optional, but added for clarity.
  37. root_block_device {
  38. volume_type = "gp3"
  39. #volume_size = "60"
  40. delete_on_termination = true
  41. encrypted = true
  42. kms_key_id = data.aws_kms_key.ebs-key.arn
  43. }
  44. ebs_block_device {
  45. # swap
  46. device_name = "/dev/xvdm"
  47. volume_type = "gp3"
  48. volume_size = 48
  49. delete_on_termination = true
  50. encrypted = true
  51. kms_key_id = data.aws_kms_key.ebs-key.arn
  52. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  53. # This may prompt replacement when the AMI is updated.
  54. # See:
  55. # https://github.com/hashicorp/terraform/issues/19958
  56. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  57. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  58. }
  59. ebs_block_device {
  60. # /home
  61. device_name = "/dev/xvdn"
  62. volume_type = "gp3"
  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_type = "gp3"
  73. # volume_size = xx
  74. delete_on_termination = true
  75. encrypted = true
  76. kms_key_id = data.aws_kms_key.ebs-key.arn
  77. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  78. }
  79. ebs_block_device {
  80. # /var/tmp
  81. device_name = "/dev/xvdp"
  82. volume_type = "gp3"
  83. # volume_size = xx
  84. delete_on_termination = true
  85. encrypted = true
  86. kms_key_id = data.aws_kms_key.ebs-key.arn
  87. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  88. }
  89. ebs_block_device {
  90. # /var/log
  91. device_name = "/dev/xvdq"
  92. volume_type = "gp3"
  93. volume_size = 16
  94. delete_on_termination = true
  95. encrypted = true
  96. kms_key_id = data.aws_kms_key.ebs-key.arn
  97. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  98. }
  99. ebs_block_device {
  100. # /var/log/audit
  101. device_name = "/dev/xvdr"
  102. volume_type = "gp3"
  103. # volume_size = xx
  104. delete_on_termination = true
  105. encrypted = true
  106. kms_key_id = data.aws_kms_key.ebs-key.arn
  107. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  108. }
  109. ebs_block_device {
  110. # /tmp
  111. device_name = "/dev/xvds"
  112. volume_type = "gp3"
  113. # volume_size = xx
  114. delete_on_termination = true
  115. encrypted = true
  116. kms_key_id = data.aws_kms_key.ebs-key.arn
  117. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  118. }
  119. network_interface {
  120. device_index = 0
  121. network_interface_id = aws_network_interface.instance.id
  122. }
  123. user_data = data.template_cloudinit_config.cloud-init.rendered
  124. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  125. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  126. }
  127. # Render a multi-part cloud-init config making use of the part
  128. # above, and other source files
  129. data "template_cloudinit_config" "cloud-init" {
  130. gzip = true
  131. base64_encode = true
  132. # Main cloud-config configuration file.
  133. part {
  134. filename = "init.cfg"
  135. content_type = "text/cloud-config"
  136. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  137. {
  138. hostname = var.instance_name
  139. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  140. environment = var.environment
  141. salt_master = var.salt_master
  142. proxy = var.proxy
  143. aws_partition = var.aws_partition
  144. aws_partition_alias = var.aws_partition_alias
  145. aws_region = var.aws_region
  146. }
  147. )
  148. }
  149. # Additional parts as needed
  150. #part {
  151. # content_type = "text/x-shellscript"
  152. # content = "ffbaz"
  153. #}
  154. }
  155. module "private_dns_record" {
  156. source = "../../submodules/dns/private_A_record"
  157. name = "${var.instance_name}-0"
  158. ip_addresses = [ aws_instance.instance.private_ip ]
  159. dns_info = var.dns_info
  160. reverse_enabled = var.reverse_enabled
  161. providers = {
  162. aws.c2 = aws.c2
  163. }
  164. }
  165. #----------------------------------------------------------------------------
  166. # Server SG
  167. #----------------------------------------------------------------------------
  168. resource "aws_security_group" "instance_security_group" {
  169. name = "${var.instance_name}_security_group"
  170. description = "Security Group for ${var.instance_name}(s)"
  171. vpc_id = var.vpc_id
  172. tags = merge(var.standard_tags, var.tags)
  173. }
  174. #----------------------------------------------------------------------------
  175. # INGRESS
  176. #----------------------------------------------------------------------------
  177. resource "aws_security_group_rule" "sensu_ui" {
  178. type = "ingress"
  179. from_port = 8000
  180. to_port = 8000
  181. protocol = "tcp"
  182. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  183. description = "Sensu UI"
  184. security_group_id = aws_security_group.instance_security_group.id
  185. }
  186. resource "aws_security_group_rule" "sensu_agent_internal" {
  187. type = "ingress"
  188. from_port = 8081
  189. to_port = 8081
  190. protocol = "tcp"
  191. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  192. description = "Internal Sensu Agents"
  193. security_group_id = aws_security_group.instance_security_group.id
  194. }
  195. resource "aws_security_group_rule" "sensu_api" {
  196. type = "ingress"
  197. from_port = 8080
  198. to_port = 8080
  199. protocol = "tcp"
  200. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  201. description = "Sensu API"
  202. security_group_id = aws_security_group.instance_security_group.id
  203. }
  204. resource "aws_security_group_rule" "sensu_agent_external" {
  205. type = "ingress"
  206. from_port = 8081
  207. to_port = 8081
  208. protocol = "tcp"
  209. source_security_group_id = aws_security_group.sensu_alb_server_external.id
  210. description = "External Sensu Agents"
  211. security_group_id = aws_security_group.instance_security_group.id
  212. }
  213. resource "aws_security_group_rule" "sensu_api_external" {
  214. type = "ingress"
  215. from_port = 8080
  216. to_port = 8080
  217. protocol = "tcp"
  218. source_security_group_id = aws_security_group.sensu_alb_server_external.id
  219. description = "External Sensu API"
  220. security_group_id = aws_security_group.instance_security_group.id
  221. }