Created
April 30, 2019 02:49
-
-
Save metaskills/3d0ce9fc42fa5e3f6be6b0148694e660 to your computer and use it in GitHub Desktop.
Cross-Region Replication S3 Buckets - Bash Glue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ -z ${STAGE_ENV+x} ]; then | |
echo "Missing STAGE_ENV env variable!" | |
exit 126 | |
fi | |
REGION1="us-east-1" | |
REGION2="us-west-2" | |
OUTPUTS1=$( | |
aws cloudformation describe-stacks \ | |
--region "${REGION1}" \ | |
--stack-name "mylambda-${STAGE_ENV}-${REGION1}" | \ | |
jq -r ".Stacks[0].Outputs[]" | |
) | |
OUTPUTS2=$( | |
aws cloudformation describe-stacks \ | |
--region "${REGION2}" \ | |
--stack-name "mylambda-${STAGE_ENV}-${REGION2}" | \ | |
jq -r ".Stacks[0].Outputs[]" | |
) | |
echo "---------------------------------------" | |
echo "Creating S3 Cross-Region Replication..." | |
echo "---------------------------------------" | |
ROLE=$(echo "$OUTPUTS1" | jq -r "select(.OutputKey == \"MyBucketRepRoleArn\").OutputValue") | |
BUCKET1=$(echo "$OUTPUTS1" | jq -r "select(.OutputKey == \"MyBucketName\").OutputValue") | |
BUCKET2=$(echo "$OUTPUTS2" | jq -r "select(.OutputKey == \"MyBucketName\").OutputValue") | |
BUCKETARN1=$(echo "$OUTPUTS1" | jq -r "select(.OutputKey == \"MyBucketArn\").OutputValue") | |
BUCKETARN2=$(echo "$OUTPUTS2" | jq -r "select(.OutputKey == \"MyBucketArn\").OutputValue") | |
REPCONF1=$(cat <<EOT | |
{ | |
"Role": "${ROLE}", | |
"Rules": [ | |
{ | |
"ID": "my-bucket-${REGION1}", | |
"Destination": { | |
"Bucket": "${BUCKETARN2}", | |
"StorageClass": "STANDARD" | |
}, | |
"Prefix": "", | |
"Status": "Enabled" | |
} | |
] | |
} | |
EOT | |
) | |
echo $REPCONF1 > /tmp/repconf1.json | |
REPCONF2=$(cat <<EOT | |
{ | |
"Role": "${ROLE}", | |
"Rules": [ | |
{ | |
"ID": "my-bucket-${REGION2}", | |
"Destination": { | |
"Bucket": "${BUCKETARN1}", | |
"StorageClass": "STANDARD" | |
}, | |
"Prefix": "", | |
"Status": "Enabled" | |
} | |
] | |
} | |
EOT | |
) | |
echo $REPCONF2 > /tmp/repconf2.json | |
pipenv run aws s3api put-bucket-replication \ | |
--bucket "${BUCKET1}" \ | |
--replication-configuration file:///tmp/repconf1.json | |
pipenv run aws s3api put-bucket-replication \ | |
--bucket "${BUCKET2}" \ | |
--replication-configuration file:///tmp/repconf2.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment