Issue
I want to get the instance name of the instance on which the code is deployed in shell script and store it in a variable.
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/iam/info
I tried the above command and got a json output which looked something like this:
{
"Code":"Success"
"LastUpdated":"date",
"InstanceProfileArn" : "arn:677.. /instance-name"
"InstanceProfileId" : "",
}
Reference: Retrieve instance metadata - Amazon Elastic Compute Cloud
Now, I want to fetch the InstanceProfileArn
and store it in a variable as this contains instance name. I want to use this for some other functionality.
Can someone help me with this?
Solution
You can pipe the JSON to jq
to extract the value that you are interested in. Use the -r
option to make it output just the string value:
--raw-output / -r:
With this option, if the filter´s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.
ARN=`curl http://169.254.169.254/latest/meta-data/iam/info | jq -r .InstanceProfileArn`
Answered By - Jukka Matilainen Answer Checked By - Timothy Miller (WPSolving Admin)