Skip to content

Instantly share code, notes, and snippets.

@dclark
Created August 25, 2023 11:37
Show Gist options
  • Save dclark/b014ac10540ca2d6911c643b8956fc50 to your computer and use it in GitHub Desktop.
Save dclark/b014ac10540ca2d6911c643b8956fc50 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')"
@dclark
Copy link
Author

dclark commented Aug 25, 2023

Retrieve AWS Credentials from CloudShell.

Run the following, which is a base64 representation of the script above.

$>echo -n "IyEvdXNyL2Jpbi9lbnYgYmFzaAoKIyBSZXRyaWV2ZSBBV1MgY3JlZGVudGlhbHMgZnJvbSBBV1MgQ2xvdWRTaGVsbAoKIyBzaGVsbGNoZWNrIGRpc2FibGU9U0MyMDAxCkhPU1Q9JChlY2hvICIkQVdTX0NPTlRBSU5FUl9DUkVERU5USUFMU19GVUxMX1VSSSIgfCBzZWQgJ3N8L2xhdGVzdC4qfHwnKQpUT0tFTj0kKGN1cmwgLXMgLVggUFVUICIkSE9TVCIvbGF0ZXN0L2FwaS90b2tlbiAtSCAiWC1hd3MtZWMyLW1ldGFkYXRhLXRva2VuLXR0bC1zZWNvbmRzOiA2MCIpCk9VVFBVVD0kKGN1cmwgLXMgIiRIT1NUL2xhdGVzdC9tZXRhLWRhdGEvY29udGFpbmVyL3NlY3VyaXR5LWNyZWRlbnRpYWxzIiAtSCAiWC1hd3MtZWMyLW1ldGFkYXRhLXRva2VuOiAkVE9LRU4iKQplY2hvICJleHBvcnQgQVdTX0FDQ0VTU19LRVlfSUQ9JChlY2hvICIkT1VUUFVUIiB8IGpxIC1yICcuQWNjZXNzS2V5SWQnKSIKZWNobyAiZXhwb3J0IEFXU19TRUNSRVRfQUNDRVNTX0tFWT0kKGVjaG8gIiRPVVRQVVQiIHwganEgLXIgJy5TZWNyZXRBY2Nlc3NLZXknKSIKZWNobyAiZXhwb3J0IEFXU19TRVNTSU9OX1RPS0VOPSQoZWNobyAiJE9VVFBVVCIgfCBqcSAtciAnLlRva2VuJykiCg==" | base64 -d | sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment