create_dlm_policy 3.3 KB

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