AWS CLI Filter / Query Newbie Questions - amazon-web-services

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.

Related

AWS CLI List Multiple Tags with Unknown Values

From the AWS CLI I want to get all EC2 instances with the tag key "schedulev2" where the values are "na". I also want to list the tag keys for "application", "Name" and "environment" where all of those are unknown (wildcard), this is where I get stuck. I've tried different filter options but I'm not able to figure this out. I'd like to list this all on one line for each instance as text so I can export it to Excel. We have 1000s of instances. This is what I have so far.
aws ec2 describe-tags --filters Name=key,Values=schedulev2 --output text

AWS CLI EC2 describe-instances command; how does the `ip-address` filter work?

I am learning how to use the CLI tool for AWS and I come across this command:
aws ec2 describe-instances --filters "Name=ip-address,Values=my.test.ip.address"
The command works fine and I can see the EC2 instance details in the command output, however, it does not have any field with the name ip-address. Instead, it has PublicIp and PublicIpAddress fields that contain the ip address which I am looking for.
How does this filter work?
There is a list of keys which can be used to filter on. This can be found in the documentation for the describe-instances command: --filter. One of them ip-address:
ip-address - The public IPv4 address of the instance.
I did not take a look on the source code for the describe-instances, but I believe it parses the JSON response and it has a preconfigured path for each of the filters.
Update:
After takin a look at the source code if aws-cli on GitHub, all the commands are transformed into API calls and send to AWS. All the filtering is happening in the back-end.

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

monitor all running aws instances with grafana

Im trying to identify a way to list all running instances with grafana. Have any of you been able to do this?
Essentially I want a grafana dashboard to display:
Instance-ID >> Region >> Status(up or down) >> Current running time
Do any of you know a way to achieve this?
I don't know if you can do that in grafana, but you can see just the information you want (except for the running time) in the prometheus alert module.
You can find it if you go to prometheus:9090 -> Status -> Targets
It should look something like this
Tom
For anyone that comes to this thread, I have resolved my predicament by using the aws cli.
Essentially I broke it down into two pieces, first get the list of regions, then second get all the instance information i required:
echo Grabbing instances in all regions, please wait..
for region in $(aws ec2 describe-regions --output text | cut -f3);
do
> $region.txt;
$quote="'";
aws ec2 describe-instances --region $region --query 'Reservations[].Instances[].[Tags[?Key==`Name`]| [0].Value,State.Name,InstanceType,PublicIpAddress,Placement.AvailabilityZone,LaunchTime]' --filters Name=instance-state-name,Values=running --output json >> /home/ubuntu/$region.txt; done

How can I retrieve the OwnerId with the describe-instances command?

I am trying to display the OwnerId of instances in a table format using the describe-instances command, but I can't seem to do that. Is anyone able to help? Here is the command I have currently:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].{IP:PublicIpAddress,AZ:Placement.AvailabilityZone,STATE:State.Name,KEY:KeyName,VPC:VpcId,ID:InstanceId, INSTANCETYPE:InstanceType}'
Thanks,
Bharath
This has two aspects:
Output Format
The AWS Command Line Interface provides the option to control the output format by means of the --output parameter, see How to Select the Output Format for details - currently it supports json, text and table, so --output table is what you are after in this regard
OwnerId
The OwnerId is a property of the parent Reservations[*] collection, so you'll need to compose your query differently by starting from there and add the Instances[0]. path to each other property as follows:
$ aws ec2 describe-instances --query 'Reservations[*].{OWNERID:OwnerId, IP:Instances[0].PublicIpAddress, AZ:Instances[0].Placement.AvailabilityZone, STATE:Instances[0].State.Name, KEY:Instances[0].KeyName, VPC:Instances[0].VpcId, ID:Instances[0].InstanceId, INSTANCETYPE:Instances[0].InstanceType}' --output table
Please note that .Instances[*] is a collection too for a reason, albeit rarely used, which means the facilitated Reservations[*].Instances[0] path will only work for the regular case that instances are started one at a time, i.e. not something like aws ec2 run-instances --count 2, see run-instances for details:
--count (string)
Number of instances to launch. If a single number is provided, it is
assumed to be the minimum to launch (defaults to 1). If a range is
provided in the form min:max then the first number is interpreted as
the minimum number of instances to launch and the second is
interpreted as the maximum number of instances to launch.