backup_server.tf 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(var.standard_tags, var.tags, { Name = "github-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 = var.backup_instance_type
  17. key_name = "msoc-build"
  18. monitoring = false
  19. iam_instance_profile = "msoc-default-instance-profile"
  20. ami = local.ami_map[local.ami_selection]
  21. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  22. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  23. # that could be removed.
  24. #lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  25. lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  26. # These device definitions are optional, but added for clarity.
  27. root_block_device {
  28. volume_type = "gp3"
  29. #volume_size = "60"
  30. delete_on_termination = true
  31. encrypted = true
  32. kms_key_id = data.aws_kms_key.ebs-key.arn
  33. }
  34. ebs_block_device {
  35. # swap
  36. device_name = "/dev/xvdm"
  37. volume_type = "gp3"
  38. volume_size = 8
  39. delete_on_termination = true
  40. encrypted = true
  41. kms_key_id = data.aws_kms_key.ebs-key.arn
  42. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  43. # This may prompt replacement when the AMI is updated.
  44. # See:
  45. # https://github.com/hashicorp/terraform/issues/19958
  46. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  47. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  48. }
  49. ebs_block_device {
  50. # /home
  51. device_name = "/dev/xvdn"
  52. volume_type = "gp3"
  53. # volume_size = xx
  54. delete_on_termination = true
  55. encrypted = true
  56. kms_key_id = data.aws_kms_key.ebs-key.arn
  57. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  58. }
  59. ebs_block_device {
  60. # /var
  61. device_name = "/dev/xvdo"
  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/xvdo"].ebs.snapshot_id
  68. }
  69. ebs_block_device {
  70. # /var/tmp
  71. device_name = "/dev/xvdp"
  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/xvdp"].ebs.snapshot_id
  78. }
  79. ebs_block_device {
  80. # /var/log
  81. device_name = "/dev/xvdq"
  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/xvdq"].ebs.snapshot_id
  88. }
  89. ebs_block_device {
  90. # /var/log/audit
  91. device_name = "/dev/xvdr"
  92. volume_type = "gp3"
  93. # volume_size = xx
  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/xvdr"].ebs.snapshot_id
  98. }
  99. ebs_block_device {
  100. # /tmp
  101. device_name = "/dev/xvds"
  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/xvds"].ebs.snapshot_id
  108. }
  109. network_interface {
  110. device_index = 0
  111. network_interface_id = aws_network_interface.ghe-backup-interface.id
  112. }
  113. user_data = data.template_cloudinit_config.cloud-init.rendered
  114. tags = merge( var.standard_tags, var.tags, { Name = "github-backup" })
  115. volume_tags = merge( var.standard_tags, var.tags, { Name = "github-backup" })
  116. }
  117. data "template_file" "cloud-init" {
  118. # Should these be in a common directory? I suspect they'd be reusable
  119. template = file("${path.module}/cloud-init/cloud-init.tpl")
  120. vars = {
  121. hostname = "ghe-backup"
  122. fqdn = "ghe-backup.${var.dns_info["private"]["zone"]}"
  123. environment = var.environment
  124. salt_master = var.salt_master
  125. proxy = var.proxy
  126. aws_partition = var.aws_partition
  127. aws_partition_alias = var.aws_partition_alias
  128. aws_region = var.aws_region
  129. }
  130. }
  131. # Render a multi-part cloud-init config making use of the part
  132. # above, and other source files
  133. data "template_cloudinit_config" "cloud-init" {
  134. gzip = true
  135. base64_encode = true
  136. # Main cloud-config configuration file.
  137. part {
  138. filename = "init.cfg"
  139. content_type = "text/cloud-config"
  140. content = data.template_file.cloud-init.rendered
  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. }