AWS CLI JMESPath Query help using query option in cli - amazon-web-services

I have the following cli command
aws ecs list-services --cluster ecs-cluster-1
Giving this JSON
{
"serviceArns": [
"arn:aws:ecs:us-east-1:XXXXXXXXXXXXX:service/ecs-cluster-1/app4",
"arn:aws:ecs:us-east-1:XXXXXXXXXXXXX:service/ecs-cluster-1/app3",
"arn:aws:ecs:us-east-1:XXXXXXXXXXXXX:service/ecs-cluster-1/app1",
"arn:aws:ecs:us-east-1:XXXXXXXXXXXXX:service/ecs-cluster-1/app4"
]
}
How do I get app1 ARN back by matching name of the app (app1) using --query option?
Expected Output
arn:aws:ecs:us-east-1:XXXXXXXXXXXXX:service/ecs-cluster-1/app1
Please note that this JSON array is not ordered.

You can either use contains or ends_with for the filtering part.
Then you want to stop the project and get the first item of the array in order to have only the application ARN you are interested in.
Stopping a projection is covered in the pipe expression tutorial of the documentation.
So, given the expression
serviceArns[?ends_with(#,'app1')]|[0]
You end up with the expected
"arn:aws:ecs:us-east-1:XXXXXXXXXXXXX:service/ecs-cluster-1/app1"
In the AWS command line interface, this will then be:
aws ecs list-service \
--cluster ecs-cluster-1 \
--query "serviceArns[?ends_with(#,'app1')]|[0]"

Assuming it is the first entry in the list, you can use:
--query servicesArns[0]
Depending on your operating system, you might need to quote it:
--query 'servicesArns[0]'
If you are looking for the entry that 'contains' app1, use:
--query serviceArns[?contains(#, 'app1') == `true`]|[0]
Those back-ticks always cause me problems. You can play around and potentially use other tick-marks.
Good references for JMESPath:
JMESPath Tutorial
JMESPath Specification

Related

JMESPath query for aws cli EMR

How do I perform a "regex" type match on a start of a string in jmespath?
aws emr list-clusters --active --query 'Clusters[?Name==`My-Cluster`].Id' --output text
I was looking at the answers in AWS CLI EMR get Master node Instance ID and tag it and I can use most of the solution. However the my cluster name is variable (based on time it was built). So i need to edit the Name=='My-Cluster' to be able to search with a wild card at the end of the name. This way I can find My-Cluser-082022 then next month find MY-Cluster-092022 next month.
You could probably use starts_with().
It would be something like:
Clusters[starts_with(Name, 'My-Cluster')].Id
Some good resources:
JMESPath Tutorial
JMESPath Specification

AWS : How to filter RDS Global Cluster name using QUERY

I want to query for AWS RDS Global Cluster without using --global-cluster-identifier option because in my automation, my code does not know the identifier so I want to fetch the cluster name using Engine and filter on the GlobalClusterIdentifer whether it contains the given value or not.
Here is my cli command which displays either Engine or GlobalClusterIdentifer. How to use --query option and get it done.
aws rds describe-global-clusters --query 'GlobalClusters[].Engine' --output text
aws rds describe-global-clusters --query 'GlobalClusters[].GlobalClusterIdentifer' --output text
So what I need is, I want to query engine type which is aurora-postgresql and fetch the GlobalClusterIdentfier of the filtered engine.
Could someone help me on this?
You should be able to use the following
aws rds describe-global-clusters --query 'GlobalClusters[?Engine==`aurora-postgresql`].GlobalClusterIdentifier

AWS CLI list-policies to find a policy with a specific name

I am trying to locate a policy in AWS with a specific name via the aws cli. I tried get-policy first but it threw and error. Now I am trying list-policies and putting in a prefix. According to the documentation if I start and end the string with a forward slash it should search but it hasn't been working. I get an empty array back... any ideas?
aws iam list-policies --scope Local --path-prefix /policyname.xyz/
It is an issue with AWS CLI V2.
The issue is still open on the github repository of the AWS SDK since 11 Jan.
You can check the detail here:
https://github.com/aws/aws-sdk/issues/36
Complete list of issues:
https://github.com/aws/aws-sdk/issues
You can use the --query flag. For example, for exact search,
aws iam list-policies --query 'Policies[?PolicyName==`policyname.xyz`]'
If you want more flexible search, you can refer to https://jmespath.org/specification.html for some functions for example 'to start with policynamexxx'
aws iam list-policies --query 'Policies[?starts_with(PolicyName,`policynamexxx`)]'

aws lambda list-functions filter out just function names?

I just want to get back a list of function names. Ideally I want to get all functions (just their name) starting with "some-prefix*". Can I do this with the cli?
Really want this as a cli command if possible (I want to avoid python or another sdk). I see there is a --cli-input-json arg, can I use that for filtering?
You can do that. Use the --query option. The CLI would look like this:
aws lambda list-functions --region us-east-1 --query 'Functions[].FunctionName' --output text
To get the list of functions whose name begin with some-prefix:
aws lambda list-functions --region us-east-1 --query 'Functions[?starts_with(FunctionName, `some-prefix`) == `true`].FunctionName' --output text
To get the complete JSON, the CLI would be:
aws lambda list-functions --region us-east-1
Details about the query parameter can be found here.
As the answer is already given by #krishna, but I was looking for a way to print all function name without specifying a prefix. So here you can get all lambda function name in particular region my default is us-west-2.
aws lambda list-functions --query 'Functions[*].[FunctionName]'
Or as I want them out in text format and space separated to use in my bash script so here you can get in text and single line space separated.
aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text | tr '\r\n' ' '
I have come here for some help to clean up all lambda functions that I have created while following an AWS developer certification tutorial. If anyone is in the same boat, I have created a script to programmatically delete all lambda functions in my AWS account (NOT for production use)
#!/bin/bash
# STOP: DON'T USE/RUN THIS SCRIPT UNLESS YOU ARE 100% SURE WHAT YOU ARE DOING
# I am a learner and created 20+ lambda functions while following a tutorial
# I wrote this script to programatically cleanup functions at the end of course
# precondition: your aws cli is configured
# get all functions from aws account
functions=(`aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text`)
for i in "${functions[#]}"
do
#delete functions 1-by-1
aws lambda delete-function --function-name "$i"
echo "deleted $i"
done
Incase, someone is looking for a similar query with string present in the lambda function name as a substring, try below
aws lambda list-functions --region us-east-1 --query 'FunctionName[?contains(FunctionName, 'containing-string'] == 'true'].[FunctionName]' --output text
Note - the '[]' brackets around '.FunctionName' will provide with each fucntionName on a new line.
You can easily get the list of all lambda functions in given region using below command:
aws lambda list-functions --region us-east-1 | jq -r .Functions[].FunctionName
Download the jq (Lightweight and flexible command-line JSON processor) from here:
https://stedolan.github.io/jq/download/

EC2 instance from AWS command line

I am very new to AWS. I've got a Windows instance running and have my aws command line configured. I've read through the AWS docs but can't seem to find exactly what I'm looking for.
How do I view my current instances from the command line?
If by view your current instances, you mean list all running instances from the command line, you can call the describe-instances command:
aws ec2 describe-instances
This will list all of your current instances.
See describe-instances
As noted in the answer by Rodrigo M, you should use describe-instances to view your EC2 instances. In general, the help command is the best way to explore the CLI. Start with aws ec2 help and try the various options. You can get more details on subcommands with aws ec2 describe-instances help as well.
The output is a bit verbose and by default in JSON. This can be a bit overwhelming and hard to read without additional processing. I'd recommend getting familiar with the --query aws CLI parameter if you intend to use the CLI interactively.
In particular, I use this for a quick overview of my EC2 instances:
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`] | [0].Value, State.Name, PublicDnsName]' --output table
To check one particular attribute on an instance:
aws ec2 describe-instances --query Reservations[0].Instances[0].InstanceType --output text --instance-ids <my-instance-id>
The CLI is very powerful once you get comfortable with learning the commands and managing the output. It's also helpful for learning the porgramming APIs as well, since aws CLI commands generally map one-to-one with an API call.