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
Related
Pretty new to AWS CLI and --query and --filter. I've been training around with some commands I've found searching around. I have a few questions if anyone can assist that would be greatly appreciated.
I'm attempting to display a table of EC2 Instances and filtering results. How would I correctly retrieve hostname info in this query because when I do the following below, in the table the Hostname column shows "None":
aws ec2 describe-instances
--query "Reservations[*].Instances[*].{Instance:InstanceId,PrivateIP:PrivateIpAddress,Type:InstanceType,Hostname:hostname|[0].Value,Status:State.Name}"
--filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values='*'"
--output table
Does AWS have a master list of "Identifiers" and what is needed to put on the other right side of the ":" (if I'm saying this correctly) and what I mean Identifiers is
Instance:InstanceId,PrivateIP:PrivateIpAddress,Type:InstanceType,etc".
As I would like to add more things to the table but I'm confused on
the syntax as I sometimes get a parse error COMMA or I get None for
that specific column like I am for Hostname.
Examples:
- Instance: "what other options can I input here besides InstanceID"
- Hostname: "what do I enter here to get the hostname info"
- Type: "what other options can I input here besides Instance"
https://i.stack.imgur.com/5Htgw.png
The Hostname:hostname|[0].Value part in your command would be Hostname:PublicIpAddress. To check the item names, I just run aws ec2 describe-instances and see which name is used for the value we want.
It's rather complicated because --filters option in aws ec2 describe-instances has its name syntax.
To determine how old the aws ec2 instances are, and to destroy those nodes if they are older than 90 days, I need to check the creation date of the node.
What is the correct way to do that using COMMAND LINE?
What I tried?
I tried the command ec2metadata, but the output doesn't contain creation date.
You can get this information with
aws configservice get-resource-config-history --resource-type AWS::EC2::Instance --resource-id i-xxxxxxxx
The last element in this json is what you need.
You can check get-resource-config-history and find resources between specific dates.
You can also get creation date of your root volume with
aws ec2 describe-volumes --volume-ids vol-xxxxx
This can also give you age of your root volume (if not changed).
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
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
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`)]'