-
-
Save hummus/8592113 to your computer and use it in GitHub Desktop.
wget http://stedolan.github.io/jq/download/linux64/jq | |
aws ec2 describe-instances --filters "Name=tag:Name,Values=$NAME" \ | |
"Name=instance-state-name,Values=running" \ | |
| jq -r \ | |
".Reservations[] | .Instances[] | .InstanceId" \ | |
aws ec2 describe-volumes --filters \ | |
"Name=status,Values=available" \ | |
| jq -r ".Volumes[] | .VolumeId" \ | |
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.LaunchTime > "2015-01-28") | select(.State.Code != 48) | [.LaunchTime, .State.Name, (.Tags[]|select(.Key=="Name")|.Value)]' | |
# no dotdee | |
aws ec2 describe-instances | jq --arg yest $(python -c "import datetime; t = datetime.datetime.now() - datetime.timedelta(hours=24); print t.strftime('%Y-%m-%d')") -r '.Reservations[].Instances[] | select(.LaunchTime > $yest) | select(.State.Code != 48) | (.Tags[]//[]|select(.Key=="Name")|.Value) as $name | "Host \($name) \nHostname \(.PrivateIpAddress)"' >> ~/.ssh/config |
Thanks!
This is really helpful, thanks.
Adding this jq filter does a regex match on the "Name" tag.
| select((.Tags[]|select(.Key=="Name")|.Value) | match("REGEXHERE") )
I found that this filter says Do I have a tag whose Key is Name and do I have a tag whose Value is regex
. What it does not do is determine if the tag whose key is Name has a Value of the regex.
If you have multiple matching values, you'll get output for each value match.
Scenario
You should not assume that all vpc has .Tags[]
. I just faced the scenario below with the output of:
aws ec2 describe-vpcs > outputfile.json
Error
jq -r '.Vpcs[]|[.VpcId,.CidrBlock, (.Tags[]|select(.Key=="Name")|.Value)] | @csv' outputfile.json
jq: error (at outputfile.json:nnn): Cannot iterate over null (null)
Solution
Make Tags optional..
jq -r '.Vpcs[]|[.VpcId,.CidrBlock, (.Tags[]?|select(.Key=="Name")|.Value)] | @csv' outputfile.json
Please help me to get active Iam key of a user with create date. Thanks Neeraj
aws ec2 describe-instances | jq -r '.Reservations[]|.Instances[]|[(.Tags[]?|select(.Key=="Name")|.Value), (.Tags[]?|select(.Key=="Group-Name")|.Value),.InstanceId,.PrivateIpAddress]|@csv'|sort
Scenario
You should not assume that all vpc has
.Tags[]
. I just faced the scenario below with the output of:
aws ec2 describe-vpcs > outputfile.json
Error
jq -r '.Vpcs[]|[.VpcId,.CidrBlock, (.Tags[]|select(.Key=="Name")|.Value)] | @csv' outputfile.json
jq: error (at outputfile.json:nnn): Cannot iterate over null (null)
Solution
Make Tags optional..
jq -r '.Vpcs[]|[.VpcId,.CidrBlock, (.Tags[]?|select(.Key=="Name")|.Value)] | @csv' outputfile.json
is there a way to assign a value to "Name" if the tag does not exist?.
For example:
aws ec2 describe-instances | jq -c '.Reservations[].Instances[] | { instanceid:.InstanceId, name:(.Tags[]? | select(.Key=="Name") | .Value)}'
will not return the instances that have tags but not the "Name" tag.
Appreciate your insights.
Found this article recently. Not sure how many examples are here and there but still it is worth linking, I believe.
https://medium.com/circuitpeople/aws-cli-with-jq-and-bash-9d54e2eabaf1
Thanks ! Very helpful.