Last active
November 1, 2023 22:34
-
-
Save mambroziak/33ff9cb5e9f22267280c to your computer and use it in GitHub Desktop.
AWS EC2 Instance Metadata to BASH Script Variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# JQ is required to more easily parse json. | |
AWS_IAM_ROLE=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/` | |
AWS_ACCESS_KEY_ID=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/$AWS_IAM_ROLE/ | jq -r '.AccessKeyId'` | |
AWS_SECRET_ACCESS_KEY=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/$AWS_IAM_ROLE/ | jq -r '.SecretAccessKey'` | |
AWS_TOKEN=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/$AWS_IAM_ROLE/ | jq -r '.Token'` | |
AWS_AZ=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone` | |
AWS_DEFAULT_REGION="`echo \"$AWS_AZ\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`" | |
LOCAL_IP=`curl -sL http://169.254.169.254/latest/meta-data/local-ipv4` | |
PUBLIC_IP=`curl -sL http://169.254.169.254/latest/meta-data/public-ipv4` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, nice script, I think AWS_TOKEN should be updated to AWS_SESSION_TOKEN