Skip to content

Instantly share code, notes, and snippets.

@thapakazi
Created October 26, 2022 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thapakazi/d44ab3914c11c8d755aa8d9b83c044e6 to your computer and use it in GitHub Desktop.
Save thapakazi/d44ab3914c11c8d755aa8d9b83c044e6 to your computer and use it in GitHub Desktop.
duplicate existing secrets on aws secrets manager with prefix: /env/secret_name -> /env1/secret_name
#!/bin/bash
export AWS_PROFILE=nonproduction
export AWS_REGION=us-east-1
source_env=staging
dest_env=testing
SECRETS_DIR=/tmp/secrets && mkdir -p $SECRETS_DIR
SECRETS_ALL=$SECRETS_DIR/secrets.json
aws secretsmanager list-secrets | jq -r '.SecretList[] | .Name + " " + .ARN' | grep $source_env | grep -v grep > $SECRETS_ALL
while read line; do
secret_key=$(echo $line|cut -d' ' -f1)
new_secret_key=$(echo $secret_key | sed "s/$source_env/$dest_env/" )
tmp_secret_key=$(echo $secret_key | sed "s!/!_!g")
secret_arn=$(echo $line|cut -d' ' -f2)
echo "get: $secret_key"
aws secretsmanager get-secret-value --secret-id $secret_arn | jq -r .SecretString > $SECRETS_DIR/$tmp_secret_key
echo "set: $new_secret_key"
aws --profile secretsmanager create-secret --name $new_secret_key --secret-string file://$SECRETS_DIR/$tmp_secret_key
done < $SECRETS_ALL
echo "Verify everything ok and clean up tmp files inside $SECRETS_DIR, ENJOY!! "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment