ami_backups.tf 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # At this time, terraform does not support DLM AMI policies, only snapshots.
  2. # So we do it ourselves
  3. #
  4. # NOTE: This will not update an existing policy, but will create one if it's missing.
  5. # Grab the current policy name. This turned out to be unnecessary for my purposes, but
  6. # will be useful if in the future we decide to implement a 'modify' resource.
  7. #
  8. # WARNING: External data sources are run before the apply, and even before any decision
  9. # is made whether or not to apply, so do not make changes in such a script.
  10. data "external" "get_dlm_policies" {
  11. program = ["bin/get_current_dlm_policies", var.aws_partition, var.aws_region, var.aws_account_id, var.account_name]
  12. depends_on = [null_resource.create_dlm_policy]
  13. }
  14. output "dlm_policies" {
  15. value = data.external.get_dlm_policies.result
  16. }
  17. locals {
  18. policy_id = lookup(data.external.get_dlm_policies.result, "PolicyId", "NULL")
  19. }
  20. # In rare cases, you may need/want to manually recreate this. To do so, run
  21. # terragrunt taint null_resource.create_dlm_policy
  22. resource "null_resource" "create_dlm_policy" {
  23. #count = data.external.get_dlm_policies.result["PolicyId"] == "null" ? 1 : 0
  24. #count = data.external.get_dlm_policies.result["PolicyId"] == "policy-02af49210b5b375d5" ? 1 : 0
  25. triggers = {
  26. aws_partition = var.aws_partition
  27. aws_region = var.aws_region
  28. aws_account_id = var.aws_account_id
  29. account_name = var.account_name
  30. }
  31. provisioner "local-exec" {
  32. command = "bin/create_or_update_dlm_policy ${var.aws_partition} ${var.aws_region} ${var.aws_account_id} ${var.account_name}"
  33. }
  34. #provisioner "local-exec" {
  35. # when = destroy
  36. # command = "bin/destroy_dlm_policy ${self.triggers.aws_partition} ${self.triggers.aws_region} ${self.triggers.aws_account_id} ${self.triggers.account_name}"
  37. #}
  38. }
  39. output "help" {
  40. value = "If you need to update/recreate the policy, run: terragrunt taint null_resource.create_dlm_policy"
  41. }