AWS CLI with jq examples

Let’s say I have an EC2 instance with the instance id of ‘i-08725957c043b6acd’ and it has the ec2 tag: env=preprod. To pull this info out using the aws cli and jq, we run:

$ aws ec2 describe-tags --output json --filters "Name=resource-id,Values=i-08725987c041b6acd" | jq --raw-output '.Tags[]|select(.Key=="env")|.Value'
preprod

Note, you can obtain the instance_id locally like this:

$ instance_id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)