Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save aws-scripting-guy/884ffa9d44bd14f7493a670543284552 to your computer and use it in GitHub Desktop.
Save aws-scripting-guy/884ffa9d44bd14f7493a670543284552 to your computer and use it in GitHub Desktop.
AWS EC2 metadata. Check attached IAM role from EC2 instance. Get temporary credentials.
# Get IAM Role name from Instance Profile Id
curl http://169.254.169.254/latest/meta-data/iam/info
# Get credentials
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/<role-name>
# More info
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
@matheusoliveira
Copy link

I found a simpler way (no need to process InstanceProfileArn to get the role name):

role_name=$( curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/ )
curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${role_name}

@renatoargh
Copy link

very useful, thanks a lot guys

@udaykirankavaturu
Copy link

Can we get the role ARN in the meta data?

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