Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypted RDS copy failure across region in AWS CLI

I have a script that copied the unencrypted and encrypted snapshots across regions. Script is working fine with unencrypted snapshot copy but for some reason, encrypted copy is failing with an error:-

An error occurred (InvalidParameterValue) when calling the CopyDBSnapshot operation: PreSignedUrl could not be authenticated.

Here is the code that I am using to copy encrypted snapshots

aws rds copy-db-snapshot 
--source-db-snapshot-identifier $source_identifier \
--target-db-snapshot-identifier ${target_identifier} \
--kms-key-id $Enc_Key_ID\ 
--region $target_region 
--source-region $source_region \
--tags Key="owner",Value="RDS Copy Job"

All variable passed here (Except for Encryption key )is working fine with the unencrypted copy across regions.

I wonder what is going wrong.Any help and suggestion would be a great help for me. (update)

aws rds copy-db-snapshot \
--source-db-snapshot-identifier arn:aws:rds:us-west-1:717934610271:snapshot:rds:rds-snapshot‌​-name-dev-2017-12-22‌​-08-08 \
--target-db-snapshot-identifier test-rds-snapshotname \
--kms-key-id XXXXXX-XXXXXX-XXXXXX-XXXXXX \
--region us-east-1\
--source-region us-west-1 \
--tags Key="owner",Value="RDS Copy Job" 

Above is the full command that is used to copy. I am using ARN to copy

like image 716
Nirbhay Singh Avatar asked Nov 04 '25 22:11

Nirbhay Singh


2 Answers

Specify a KMS Key that is valid in the destination region.

You can copy a snapshot that has been encrypted using an AWS KMS encryption key. If you copy an encrypted snapshot, the copy of the snapshot must also be encrypted. If you copy an encrypted snapshot within the same AWS Region, you can encrypt the copy with the same KMS encryption key as the original snapshot, or you can specify a different KMS encryption key. If you copy an encrypted snapshot across regions, you can't use the same KMS encryption key for the copy as used for the source snapshot, because KMS keys are region-specific. Instead, you must specify a KMS key valid in the destination AWS Region.

Handling Encryption

like image 91
John Hanley Avatar answered Nov 07 '25 02:11

John Hanley


Below script worked for me, the idea is if KMS default key is not created in target AWS region than use kms ID alias/aws/rds, it will create the new KMS id.

#!/bin/bash
if [[ -z $1 ]]; then
    echo "please input source region from which copy"
    exit
fi
if [[ -z $2 ]]; then
    echo "please input destination region"
    exit
fi
REGION_SOURCE=$1
REGION_DESTINATION=$2
RDS_DBSnapshotIdentifier=`/usr/bin/aws rds describe-db-snapshots --region $REGION_SOURCE --query="reverse(sort_by(DBSnapshots, &SnapshotCreateTime))[0]" | /usr/bin/jq -r '.DBSnapshotArn'`
echo "Copying RDS from $REGION_SOURCE to $REGION_DESTINATION"
TODAY_DATE=`/bin/date +"%Y-%m-%d-%H-%M-%S"`

KMS_KEY_ID=`/usr/bin/aws kms list-aliases --region $REGION_DESTINATION| /usr/bin/jq  -r '.[]' | /usr/bin/jq -r '.[] | select( .AliasName == "alias/aws/rds")' | /usr/bin/jq -r '.TargetKeyId'`
if [[ $KMS_KEY_ID == null ]]; then
   KMS_KEY_ID="alias/aws/rds"
fi

/usr/bin/aws rds copy-db-snapshot --kms-key-id $KMS_KEY_ID --source-db-snapshot-identifier $RDS_DBSnapshotIdentifier  --target-db-snapshot-identifier "RDS-COPY-${TODAY_DATE}-from-${REGION_SOURCE}-to-${REGION_DESTINATION}"  --region $REGION_DESTINATION  --source-region $REGION_SOURCE
like image 34
Ramratan Gupta Avatar answered Nov 07 '25 00:11

Ramratan Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!