3 Ways to retrieve values from awscli json output
The AWS CLI (AWS Command Line Interface) is a tool provided by AWS to manage your resources and it is commonly used as part of the DevOps process. For example, assuming a role, baking an image, taking a snapshot, uploading files to s3, increasing auto scaling capacity limit, and the list can go on and on.
By default, when using AWS CLI to run commands, the output will be in json
format. There are times where you need to retrieve certain values from the output and use it for your next command but the system does not have the binary that you want to use due to various reasons (e.g. security). In this blog post, we will explore some of the methods that you can use to retrieve values from json
.
To be consistent, the AWS CLI command we will be using for all of the examples is aws ec2 describe-network-interfaces --network-interface-ids eni-0c53811a076216ede
with the output below and we will be retrieving the PrivateIpAddress
value (i.e. 172.31.46.30
).
{
"NetworkInterfaces": [
{
"AvailabilityZone": "ap-southeast-1b",
"Description": "test",
"Groups": [
{
"GroupName": "private",
"GroupId": "sg-12345678910111213"
}
],
"InterfaceType": "interface",
"Ipv6Addresses": [],
"MacAddress": "06:d1:de:87:c0:b0",
"NetworkInterfaceId": "eni-0c53811a076216ede",
"OwnerId": "012345678910"…