create_or_update_dlm_policy 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 - Daily Schedule",
  53. "CopyTags": true,
  54. "TagsToAdd": [
  55. {
  56. "Key": "SnapshotPolicy",
  57. "Value": "Daily"
  58. },
  59. {
  60. "Key": "SnapshotRetention",
  61. "Value": "Daily"
  62. },
  63. {
  64. "Key": "SnapshotCreator",
  65. "Value": "XDR AMI Backups with Cross Region Replication - Daily"
  66. }
  67. ],
  68. "VariableTags": [
  69. {
  70. "Key": "instance-id",
  71. "Value": "\$(instance-id)"
  72. }
  73. ],
  74. "CreateRule": {
  75. "Interval": 24,
  76. "IntervalUnit": "HOURS",
  77. "Times": [
  78. "03:30"
  79. ]
  80. },
  81. "RetainRule": {
  82. "Count": 7
  83. },
  84. "CrossRegionCopyRules": [
  85. {
  86. "TargetRegion": "${TARGET_REGION}",
  87. "Encrypted": true,
  88. "CmkArn": "${KMS_ARN}",
  89. "CopyTags": true,
  90. "RetainRule": {
  91. "Interval": 7,
  92. "IntervalUnit": "DAYS"
  93. }
  94. }
  95. ]
  96. },
  97. {
  98. "Name": "XDR AMI Backups with Cross Region Replication - Weekly Schedule",
  99. "CopyTags": true,
  100. "TagsToAdd": [
  101. {
  102. "Key": "SnapshotPolicy",
  103. "Value": "Daily"
  104. },
  105. {
  106. "Key": "SnapshotRetention",
  107. "Value": "Weekly"
  108. },
  109. {
  110. "Key": "SnapshotCreator",
  111. "Value": "XDR AMI Backups with Cross Region Replication - Weekly"
  112. }
  113. ],
  114. "VariableTags": [
  115. {
  116. "Key": "instance-id",
  117. "Value": "\$(instance-id)"
  118. }
  119. ],
  120. "CreateRule": {
  121. "CronExpression": "cron(30 03 ? * MON *)"
  122. },
  123. "RetainRule": {
  124. "Count": 4
  125. },
  126. "CrossRegionCopyRules": [
  127. {
  128. "TargetRegion": "${TARGET_REGION}",
  129. "Encrypted": true,
  130. "CmkArn": "${KMS_ARN}",
  131. "CopyTags": true,
  132. "RetainRule": {
  133. "Interval": 4,
  134. "IntervalUnit": "WEEKS"
  135. }
  136. }
  137. ]
  138. },
  139. {
  140. "Name": "XDR AMI Backups with Cross Region Replication - Monthly Schedule",
  141. "CopyTags": true,
  142. "TagsToAdd": [
  143. {
  144. "Key": "SnapshotPolicy",
  145. "Value": "Daily"
  146. },
  147. {
  148. "Key": "SnapshotRetention",
  149. "Value": "Monthly"
  150. },
  151. {
  152. "Key": "SnapshotCreator",
  153. "Value": "XDR AMI Backups with Cross Region Replication - Monthly"
  154. }
  155. ],
  156. "VariableTags": [
  157. {
  158. "Key": "instance-id",
  159. "Value": "\$(instance-id)"
  160. }
  161. ],
  162. "CreateRule": {
  163. "CronExpression": "cron(30 03 1 * ? *)"
  164. },
  165. "RetainRule": {
  166. "Count": 12
  167. },
  168. "CrossRegionCopyRules": [
  169. {
  170. "TargetRegion": "${TARGET_REGION}",
  171. "Encrypted": true,
  172. "CmkArn": "${KMS_ARN}",
  173. "CopyTags": true,
  174. "RetainRule": {
  175. "Interval": 12,
  176. "IntervalUnit": "MONTHS"
  177. }
  178. }
  179. ]
  180. }
  181. ],
  182. "Parameters": {
  183. "NoReboot": true
  184. }
  185. }
  186. EOF
  187. POLICIES=$(aws --profile ${PROFILE} --region ${REGION} dlm get-lifecycle-policies)
  188. # Extracts the policy IDs of IMAGE_MANAGEMENT policye
  189. POLICY_ID=$(echo $POLICIES | jq -r '[.Policies[] | select(.PolicyType=="IMAGE_MANAGEMENT") | select(.Tags.SnapshotPolicy=="Daily")] | first | .PolicyId')
  190. if [ "${POLICY_ID}" != 'null' ]; then
  191. echo Updating existing policy ${POLICY_ID}
  192. aws --profile ${PROFILE} --region ${REGION} dlm update-lifecycle-policy --policy-id ${POLICY_ID} \
  193. --execution-role-arn arn:${PARTITION}:iam::${ACCOUNT}:role/dlm-lifecycle-role \
  194. --description "XDR Long Term AMI Backups with Cross Region Replication" \
  195. --state ENABLED \
  196. --policy-details file://${tmpfile}
  197. # At some future date, hopefully tags will be supported on the update
  198. #--tags '{ "Name": "XDR-AMI-XRegion", "SnapshotPolicy": "Daily" }' \
  199. else
  200. echo Creating new policy
  201. aws --profile ${PROFILE} --region ${REGION} dlm create-lifecycle-policy \
  202. --execution-role-arn arn:${PARTITION}:iam::${ACCOUNT}:role/dlm-lifecycle-role \
  203. --description "XDR Long Term AMI Backups with Cross Region Replication" \
  204. --state ENABLED \
  205. --tags '{ "Name": "XDR-AMI-XRegion", "SnapshotPolicy": "Daily" }' \
  206. --policy-details file://${tmpfile}
  207. fi
  208. rm $tmpfile