main.tf 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. iops = 6000 # etcd requires extra performance
  75. throughput = 250 # etcd requires extra performance
  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 = 16
  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.instance.id
  124. }
  125. user_data = data.template_cloudinit_config.cloud-init.rendered
  126. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  127. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  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 = var.instance_name
  141. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  142. environment = var.environment
  143. salt_master = var.salt_master
  144. proxy = var.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" {
  158. source = "../../submodules/dns/private_A_record"
  159. name = "${var.instance_name}-0"
  160. ip_addresses = [ aws_instance.instance.private_ip ]
  161. dns_info = var.dns_info
  162. reverse_enabled = var.reverse_enabled
  163. providers = {
  164. aws.c2 = aws.c2
  165. }
  166. }
  167. #----------------------------------------------------------------------------
  168. # Server SG
  169. #----------------------------------------------------------------------------
  170. resource "aws_security_group" "instance_security_group" {
  171. name = "${var.instance_name}_security_group"
  172. description = "Security Group for ${var.instance_name}(s)"
  173. vpc_id = var.vpc_id
  174. tags = merge(var.standard_tags, var.tags)
  175. }
  176. #----------------------------------------------------------------------------
  177. # INGRESS
  178. #----------------------------------------------------------------------------
  179. resource "aws_security_group_rule" "sensu_ui" {
  180. type = "ingress"
  181. from_port = 8000
  182. to_port = 8000
  183. protocol = "tcp"
  184. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  185. description = "Sensu UI"
  186. security_group_id = aws_security_group.instance_security_group.id
  187. }
  188. resource "aws_security_group_rule" "sensu_agent_internal" {
  189. type = "ingress"
  190. from_port = 8081
  191. to_port = 8081
  192. protocol = "tcp"
  193. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  194. description = "Internal Sensu Agents"
  195. security_group_id = aws_security_group.instance_security_group.id
  196. }
  197. resource "aws_security_group_rule" "sensu_api" {
  198. type = "ingress"
  199. from_port = 8080
  200. to_port = 8080
  201. protocol = "tcp"
  202. source_security_group_id = aws_security_group.sensu_alb_server_internal.id
  203. description = "Sensu API"
  204. security_group_id = aws_security_group.instance_security_group.id
  205. }
  206. resource "aws_security_group_rule" "sensu_agent_external" {
  207. type = "ingress"
  208. from_port = 8081
  209. to_port = 8081
  210. protocol = "tcp"
  211. source_security_group_id = aws_security_group.sensu_alb_server_external.id
  212. description = "External Sensu Agents"
  213. security_group_id = aws_security_group.instance_security_group.id
  214. }
  215. resource "aws_security_group_rule" "sensu_api_external" {
  216. type = "ingress"
  217. from_port = 8080
  218. to_port = 8080
  219. protocol = "tcp"
  220. source_security_group_id = aws_security_group.sensu_alb_server_external.id
  221. description = "External Sensu API"
  222. security_group_id = aws_security_group.instance_security_group.id
  223. }