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

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.

Related

AWS CLI Filter / Query Newbie Questions

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.

How to return less information from aws ec2 describe-instances

When running aws ec2 describe-instances a LOT of information is returned per instance. Is there a way to easily review a table of instances and their states? (i.e. without much of the other information)
Note: some other IAAS/PAAS tools typically display less information unless you explicitly ask for more with --verbose or similar. Whereas aws seems to give a lot by default
This will return a few fields likely to be of interest, namely:
Availability Zone
State Name (e.g. running, stopped etc)
Launch Time
Instance Type (e.g. t2.medium)
Instance ID
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone, State.Name, LaunchTime, InstanceType, InstanceId]' --output text
The above can easily be edited to include (or remove fields) as necessary
This comes from here thanks #Dusan Bajic

Retrieve the value of certain tag from within the instance

I am trying to retrieve the value of certain tag (say environment) of a instance with specific name.
Is it somehow possible to do this using describe-tags or any script? Unfortunately I cannot use describe-instance.
Thanks in advance.
To make it clear if EC2 instance name is ABC-app-dev and there is user tag 'environment' with value 'dev', I want to get the value of environment i.e. dev from within the instance itself.
The difficulty here is that the Name of an instance is actually just a tag itself. Thus, you have to search for a tag associate with an instance by specifying the Name tag, then look at the other tags.
The describe-tags command can list the Name tags:
aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Name"
You would then need to find the specific instance with the matching name:
aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Name" "Name=value,Values=MY-NAME"
However, all this returns is the tag you have already specified, rather than the other tags for the instance.
You really need a 2-step process that first finds the Instance ID, then retrieves the tags for that instance:
aws ec2 describe-tags --filters "Name=resource-id,Values=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Name" "Name=value,Values=MY-NAME" --query Tags[].ResourceId --output text`" "Name=key,Values=MY-TAG-NAME" --query Tags[].Value --output text

AWS CLI add name to results of describe-instances query

I'm prefacing this with a call for any non-AWS-written guides for their CLI stuff... their examples are few and far between and tell me nothing about the required syntax and further, fleshed-out reading would be welcome.
I'm trying to replicate a few tasks we do in the GUI with a script to save time. Currently I'm trying to find out the state of an instance by feeding it the name of the instance (aka, the Name tag). The issue is that the output I'm getting is only the state, with no identifying information. Sometimes users will put in a wildcard and get multiple instances back, and I would like to display the name of each to differentiate.
My successful query for the state of an instance looks like so;
aws ec2 describe-instances --query "Reservations[].Instances[].State[]" --filter Name=tag:Name,Values="${userinput}" --output text
With an output of
16 running
16 running
16 running
16 running
16 running
16 running
While it is correct that all of these matched my input because of the wildcard, eg test*, I need to know what each one is called. Not the instance id, the name, ie test01, test02, etc.
I would have expected it to be
aws ec2 describe-instances --query "Reservations[].Instances[].State[].Tags[?Key=='Name'].Value" --filter Name=tag:Name,Values="${state}" --output text
but that outputs an error, or
aws ec2 describe-instances --query "Reservations[].Instances[].State[].[Tags[?Key=='Name'].Value]" --filter Name=tag:Name,Values="${state}" --output text
but that gives me None
How can I add the name column to the output?
The text output format is kind of ugly because it prints multiple lines per instance, but here's a working version:
aws ec2 describe-instances --query "Reservations[].Instances[].[State.Name, Tags[?Key=='Name'].Value[]]" --filter Name=tag:Name,Values="${userinput}" --output text
I couldn't figure out how to get each instance on one line using just the AWS CLI tool, but here's a version that prints one line per instance by piping to sed:
aws ec2 describe-instances --query "Reservations[].Instances[].[State.Name, Tags[?Key=='Name'].Value[]]" --filter Name=tag:Name,Values="${userinput}" --output text | sed 'N;s/\n/ /'

How do I use AWS CLI to list all instances with name, state, instance size and AZ in the same line

How do I use AWS CLI to list all instances with name, state, instance size and AZ in the same line?
I got close with this:
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[], Placement.AvailabilityZone,InstanceType,State.Name]' --output text
But that outputs the instance name below the rest. I want to keep them on the same line so I can copy to a spreadsheet.
You need to change Tags[?Key==Name].Value[] to Tags[?Key==Name].Value[] | [0]; I think it's because Tags[?Key==Name].Value[] returns an array which the text output format doesn't know how to put on a single line, piping to [0] extracts the (single) element out for you. So your full query should be :
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[] | [0], Placement.AvailabilityZone,InstanceType,State.Name]' --output text
If you don't wish to scratch your eyes out with the piping syntax, consider this simple shell workaround:
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[], Placement.AvailabilityZone,InstanceType,State.Name]' | \
while read Zone Type State ; do
read Name
echo "$Name $Zone $Type $State"
done
Assuming that the original command returns 2 lines like these for each machine:
my-ec2-host-xyz
us-east-1d t2.micro running
The output of the above script will be:
my-ec2-host-xyz us-east-1d t2.micro running
This hack is easily understood and can readily be adapted to --output text of any complexity.