Skip to content

Instantly share code, notes, and snippets.

@htnosm
Created August 6, 2023 21:54
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 htnosm/c3aea46e581d7d296627d1ab2fe74d35 to your computer and use it in GitHub Desktop.
Save htnosm/c3aea46e581d7d296627d1ab2fe74d35 to your computer and use it in GitHub Desktop.
Get the credentials from AWS CloudShell and output to credential file format.
#!/usr/bin/env bash
# aws-cloudshell-cred.sh
# Get the credentials from AWS CloudShell and output to credential file format.
CONTAINER_CREDENTIALS=$(curl -sSf -H "Authorization: ${AWS_CONTAINER_AUTHORIZATION_TOKEN}" "${AWS_CONTAINER_CREDENTIALS_FULL_URI}")
if [ $? -ne 0 ]; then
echo "failed get container credentials."
exit 1
fi
echo "# Expiration: $(echo "${CONTAINER_CREDENTIALS}" | jq -r .Expiration)"
OUTPUT_CRED="/tmp/cred.$(date +'%Y%m%d_%H%M%S')"
echo "# <<<<< .aws/credentials"
cat <<_EOF | tee "${OUTPUT_CRED}"
export AWS_ACCESS_KEY_ID=$(echo "${CONTAINER_CREDENTIALS}" | jq -r .AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo "${CONTAINER_CREDENTIALS}" | jq -r .SecretAccessKey)
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
export AWS_REGION=${AWS_REGION}
export AWS_SECURITY_TOKEN=$(echo "${CONTAINER_CREDENTIALS}" | jq -r .Token)
_EOF
echo "# >>>>> .aws/credentials"
echo "# -----"
echo "# Output: ${OUTPUT_CRED}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment