Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vsujeesh/a11581800863e5e70c3147ad23bf629c to your computer and use it in GitHub Desktop.
Save vsujeesh/a11581800863e5e70c3147ad23bf629c to your computer and use it in GitHub Desktop.
AWS CloudShell get credentials
#!/usr/bin/env bash
# Retrieve AWS credentials from AWS CloudShell
# shellcheck disable=SC2001
HOST=$(echo "$AWS_CONTAINER_CREDENTIALS_FULL_URI" | sed 's|/latest.*||')
TOKEN=$(curl -s -X PUT "$HOST"/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
OUTPUT=$(curl -s "$HOST/latest/meta-data/container/security-credentials" -H "X-aws-ec2-metadata-token: $TOKEN")
echo "export AWS_ACCESS_KEY_ID=$(echo "$OUTPUT" | jq -r '.AccessKeyId')"
echo "export AWS_SECRET_ACCESS_KEY=$(echo "$OUTPUT" | jq -r '.SecretAccessKey')"
echo "export AWS_SESSION_TOKEN=$(echo "$OUTPUT" | jq -r '.Token')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment