Skip to content

Instantly share code, notes, and snippets.

@metaskills metaskills/global.sh
Created Apr 30, 2019

Embed
What would you like to do?
Cross-Region Replication S3 Buckets - Bash Glue
#!/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
You can’t perform that action at this time.