create_dlm_policy 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #! /bin/bash
  2. #
  3. # Creates the XDR DLM Policy to backup AMIs daily and copy them cross-region.
  4. #
  5. # NOTE: If you create a new policy, the old policy will remain. Use the modify
  6. # script instead. And even if you delete the old policy, the images created by
  7. # it will remain and continue to incur charges.
  8. set -euo pipefail
  9. PARTITION=$1
  10. REGION=$2
  11. ACCOUNT=$3
  12. ACCOUNT_NAME=$4
  13. # Fix for some accounts having -gov already appended and some not.
  14. # Accounts in gov will get it appended.
  15. ACCOUNT_NAME=${ACCOUNT_NAME%%-gov}
  16. if [[ ${REGION} == "us-gov-east-1" ]]; then
  17. PROFILE=${ACCOUNT_NAME}-gov
  18. TARGET_REGION="us-gov-west-1"
  19. elif [[ ${REGION} == "us-gov-west-1" ]]; then
  20. PROFILE=${ACCOUNT_NAME}-gov
  21. TARGET_REGION="us-gov-east-1"
  22. elif [[ ${REGION} == "us-east-1" ]]; then
  23. PROFILE=${ACCOUNT_NAME}
  24. TARGET_REGION="us-west-1"
  25. elif [[ ${REGION} == "us-west-1" ]]; then
  26. PROFILE=${ACCOUNT_NAME}
  27. TARGET_REGION="us-east-1"
  28. else
  29. >&2 echo ERROR: Could not determine target region from source region \"${REGION}\"
  30. exit -1
  31. fi
  32. # Fix the accounts that we foolish prepended 'afs-' to.
  33. PROFILE=${PROFILE##afs-}
  34. # Find the target region key ARN, since we can't use aliases here
  35. KMS_KEY_ID=$(aws --profile ${PROFILE} --region ${TARGET_REGION} kms list-aliases | jq -r '.Aliases[] | select(.AliasName=="alias/ami_backup_key") | .TargetKeyId')
  36. KMS_ARN=$(aws --profile ${PROFILE} --region ${TARGET_REGION} kms describe-key --key-id ${KMS_KEY_ID} | jq -r '.KeyMetadata.Arn')
  37. tmpfile=$(mktemp /tmp/create_dlm_policy.XXXXXXX)
  38. cat > ${tmpfile} <<EOF
  39. {
  40. "PolicyType": "IMAGE_MANAGEMENT",
  41. "ResourceTypes": [
  42. "INSTANCE"
  43. ],
  44. "TargetTags": [
  45. {
  46. "Key": "Snapshot",
  47. "Value": "Daily"
  48. }
  49. ],
  50. "Schedules": [
  51. {
  52. "Name": "XDR AMI Backups with Cross Region Replication",
  53. "CopyTags": true,
  54. "TagsToAdd": [
  55. {
  56. "Key": "SnapshotPolicy",
  57. "Value": "Daily"
  58. },
  59. {
  60. "Key": "SnapshotCreator",
  61. "Value": "XDR AMI Backups with Cross Region Replication"
  62. }
  63. ],
  64. "VariableTags": [
  65. {
  66. "Key": "instance-id",
  67. "Value": "\$(instance-id)"
  68. }
  69. ],
  70. "CreateRule": {
  71. "Interval": 24,
  72. "IntervalUnit": "HOURS",
  73. "Times": [
  74. "03:30"
  75. ]
  76. },
  77. "RetainRule": {
  78. "Count": 2
  79. },
  80. "CrossRegionCopyRules": [
  81. {
  82. "TargetRegion": "${TARGET_REGION}",
  83. "Encrypted": true,
  84. "CmkArn": "${KMS_ARN}",
  85. "CopyTags": true,
  86. "RetainRule": {
  87. "Interval": 2,
  88. "IntervalUnit": "DAYS"
  89. }
  90. }
  91. ]
  92. }
  93. ],
  94. "Parameters": {
  95. "NoReboot": true
  96. }
  97. }
  98. EOF
  99. aws --profile ${PROFILE} --region ${REGION} dlm create-lifecycle-policy \
  100. --execution-role-arn arn:${PARTITION}:iam::${ACCOUNT}:role/dlm-lifecycle-role \
  101. --description "XDR AMI Backups with Cross Region Replication" \
  102. --state ENABLED \
  103. --tags '{ "Name": "XDR-AMI-XRegion", "SnapshotPolicy": "Daily" }' \
  104. --policy-details file://${tmpfile}
  105. rm $tmpfile