backup_server.tf 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. resource "aws_network_interface" "ghe-backup-interface" {
  6. subnet_id = var.private_subnets[0]
  7. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.ghe_backup_server.id]
  8. description = "ghe-backup"
  9. tags = merge(local.standard_tags, var.tags, { Name = "ghe-backup" })
  10. }
  11. resource "aws_instance" "ghe-backup-instance" {
  12. tenancy = "default"
  13. ebs_optimized = true
  14. disable_api_termination = var.instance_termination_protection
  15. instance_initiated_shutdown_behavior = "stop"
  16. instance_type = "t3a.medium"
  17. key_name = "msoc-build"
  18. monitoring = false
  19. iam_instance_profile = "msoc-default-instance-profile"
  20. metadata_options {
  21. http_tokens = "required"
  22. }
  23. ami = local.ami_map[local.ami_selection]
  24. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  25. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  26. # that could be removed.
  27. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  28. #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  29. # These device definitions are optional, but added for clarity.
  30. root_block_device {
  31. volume_type = "gp3"
  32. #volume_size = "60"
  33. delete_on_termination = true
  34. encrypted = true
  35. kms_key_id = data.aws_kms_key.ebs-key.arn
  36. }
  37. ebs_block_device {
  38. # swap
  39. device_name = "/dev/xvdm"
  40. volume_type = "gp3"
  41. volume_size = 8
  42. delete_on_termination = true
  43. encrypted = true
  44. kms_key_id = data.aws_kms_key.ebs-key.arn
  45. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  46. # This may prompt replacement when the AMI is updated.
  47. # See:
  48. # https://github.com/hashicorp/terraform/issues/19958
  49. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  50. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  51. }
  52. ebs_block_device {
  53. # /home
  54. device_name = "/dev/xvdn"
  55. volume_type = "gp3"
  56. # volume_size = xx
  57. delete_on_termination = true
  58. encrypted = true
  59. kms_key_id = data.aws_kms_key.ebs-key.arn
  60. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  61. }
  62. ebs_block_device {
  63. # /var
  64. device_name = "/dev/xvdo"
  65. volume_type = "gp3"
  66. # volume_size = xx
  67. delete_on_termination = true
  68. encrypted = true
  69. kms_key_id = data.aws_kms_key.ebs-key.arn
  70. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  71. }
  72. ebs_block_device {
  73. # /var/tmp
  74. device_name = "/dev/xvdp"
  75. volume_type = "gp3"
  76. # volume_size = xx
  77. delete_on_termination = true
  78. encrypted = true
  79. kms_key_id = data.aws_kms_key.ebs-key.arn
  80. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var/log
  84. device_name = "/dev/xvdq"
  85. volume_type = "gp3"
  86. # volume_size = xx
  87. delete_on_termination = true
  88. encrypted = true
  89. kms_key_id = data.aws_kms_key.ebs-key.arn
  90. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  91. }
  92. ebs_block_device {
  93. # /var/log/audit
  94. device_name = "/dev/xvdr"
  95. volume_type = "gp3"
  96. # volume_size = xx
  97. delete_on_termination = true
  98. encrypted = true
  99. kms_key_id = data.aws_kms_key.ebs-key.arn
  100. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  101. }
  102. ebs_block_device {
  103. # /tmp
  104. device_name = "/dev/xvds"
  105. volume_type = "gp3"
  106. # volume_size = xx
  107. delete_on_termination = true
  108. encrypted = true
  109. kms_key_id = data.aws_kms_key.ebs-key.arn
  110. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  111. }
  112. network_interface {
  113. device_index = 0
  114. network_interface_id = aws_network_interface.ghe-backup-interface.id
  115. }
  116. user_data = data.template_cloudinit_config.cloud-init.rendered
  117. tags = merge(local.standard_tags, var.tags, var.backup_instance_tags, { Name = "ghe-backup" })
  118. volume_tags = merge(local.standard_tags, var.tags, { Name = "ghe-backup" })
  119. }
  120. # Render a multi-part cloud-init config making use of the part
  121. # above, and other source files
  122. data "template_cloudinit_config" "cloud-init" {
  123. gzip = true
  124. base64_encode = true
  125. # Main cloud-config configuration file.
  126. part {
  127. filename = "init.cfg"
  128. content_type = "text/cloud-config"
  129. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  130. {
  131. hostname = "ghe-backup"
  132. fqdn = "ghe-backup.${var.dns_info["private"]["zone"]}"
  133. environment = var.environment
  134. salt_master = local.salt_master
  135. proxy = local.proxy
  136. aws_partition = var.aws_partition
  137. aws_partition_alias = var.aws_partition_alias
  138. aws_region = var.aws_region
  139. }
  140. )
  141. }
  142. # Additional parts as needed
  143. #part {
  144. # content_type = "text/x-shellscript"
  145. # content = "ffbaz"
  146. #}
  147. }
  148. module "private_dns_record_ghe_backup" {
  149. source = "../../submodules/dns/private_A_record"
  150. name = "ghe-backup"
  151. ip_addresses = [aws_instance.ghe-backup-instance.private_ip]
  152. dns_info = var.dns_info
  153. reverse_enabled = var.reverse_enabled
  154. providers = {
  155. aws.c2 = aws.c2
  156. }
  157. }