Possible: AWS CLI Describe Not Equal To - amazon-web-services

I am currently using the AWS CLI to select instances and I have the following query:
aws ec2 describe-instances --filter "Name=instance.group-name,Values=my-cluster" "Name=instance-state-name,Values=running,pending,stopped" 'Name=tag:Name, Values=someInstance*'
This works and selects all the instances that start with someInstance.
However, I want to do the opposite, select all other instances that do NOT match this. I have tried using a regex but this doesn't work:
aws ec2 describe-instances --filter "Name=instance.group-name,Values=my-cluster" "Name=instance-state-name,Values=running,pending,stopped" 'Name=tag:Creater, Values=^(?!someInstance).*$'
Is this possible?

This can be accomplished by using JQ -
aws ec2 describe-instances --filter "Name=instance.group-name,Values=my-group" "Name=instance-state-name,Values=running,pending,stopped" | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Creator"}^C{Value: "myExclusion"}]}) | not)'

Related

How to fetch second last AMI ID using AWS CLI

Using below AWS CLI command, I am able to fetch recently created AMI ID by sorting CreationDate.
aws ec2 describe-images --owners 1234567890 --filters 'Name=name,Values=*AMI*' 'Name=state,Values=available' --output json | jq -r '.Images | sort_by(.CreationDate) | last(.[]).ImageId'
I also wanted to get the second last AMI ID using AWS CLI. Could someone help me out on this?
You can use indices, like in python ([-2]):
aws ec2 describe-images --owners 1234567890 --filters 'Name=name,Values=*AMI*' 'Name=state,Values=available' --output json | jq -r '.Images | sort_by(.CreationDate)[-2].ImageId'

AWS CLI - aws ec2 describe-instances to retrieve keypair mame for each EC2 instance

I am trying to query keypair names that are attached to each EC2 instance, the ec2 describe-instances below works fine, it does exactly what I need, but the column under the {keypair.Name} displays [NONE] I am not sure if I am using the proper parameter name - I know there are few keypairNames.epm attached on my EC2 instances when I login to the console, but I am not seeing that on my report that I run by the command below. Any input is much appreciated.. Thx !
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0],InstanceId,Platform,State.Name,PrivateIpAddress,PublicIpAddress,InstanceType,PublicDnsName,keypair.Name]' --output table --region us-west-2
There is no field called keypair in the Instances dictionary.
The closest is KeyName:
{
"Reservations": [
{
"Instances": [
{
"InstanceId": "i-xxx",
"KeyName": "foo",
...
Therefore, you would use:
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0],InstanceId,Platform,State.Name,PrivateIpAddress,PublicIpAddress,InstanceType,PublicDnsName,KeyName]'
See: describe-instances — AWS CLI Command Reference
I did find a way to sort this out by using pipe. exp: | sort -k5
Please note, before I used {sort}, the report had {windows} and {None} allover the place under column PLATFORM. Please see attached, I uploaded a sample result of my report..
The new statement looks like this:
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0],InstanceId,Platform,State.Name,PrivateIpAddress,PublicIpAddress,InstanceType,KeyName]' --output table | sort -k5
Report shows that is sorted by PLATFORM IN Asc order
I do the following for a few reasons
it can be easily piped to grep
you don't need to query aws api's repeatedly if you want to improve your data
I have multiple regions that I'm usually iterating through, so this makes it easy to scan through all regions (and accounts)
instances=`aws ec2 describe-instances `
echo $instances | jq '.Reservations[].Instances[] | "[\(.InstanceId) \(.Platform) \(.State.Name) \(.PrivateIpAddress) \(.PublicIpAddress) \(.InstanceType) \(.PublicDnsName) \(.KeyName)]"'

AWS CLI command to list stopped instances

ec2-describe-instances --filter "instance-state-name=stopped"
This helps me list all stopped instances with all its details.
How should I modify the command that it only gives me the names of the stopped instances?
You are using old style commands. Use AWS CLI to get what you want.
aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped" --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value[]'
you can use aws cli combined with other tools like jq
aws ec2 describe-instances \
--filter Name=instance-state-name,Values=stopped \
--query 'Reservations[].Instances[].{ID: InstanceId,Hostname: PublicDnsName,Name: Tags[?Key==`Name`].Value }' \
| jq '.[] | .Name[]'
this will produce output in form:
"instance2"
"instance1"

Terminate a set on EC2 instances by tags using AWS CLI

Faily new to AWS however I am looking to terminate a set of ec2 instances using the AWS CLI by filtering by a Tag name.
If I use describe-instances, I can filter by tag:key=value . For terminate-instances I don't see a way of filtering. I assume this is possible since I can filter and terminate using the AWS console but I am looking to do this via CLI.
Latest AWS CLI allows you to avoid the need for any scripts or jq:
aws ec2 terminate-instances --instance-ids $(aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' --filters "Name=tag:tagkey,Values=tagvalue" --output text)
as long as the number of expected instances is not huge, the above can be used.
The terminate-instances command only takes a list of instance IDs. You would need to write a script to run the describe-instances command first and capture the instance IDs, then pass those IDs to the terminate-instances command.
I created the following script(.sh) and it worked for me:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --filters 'Name=tag-value,Values=MYTAG' --output text |
grep stopped |
awk '{print $2}' |
while read line;
do aws ec2 terminate-instances --instance-ids $line
done

How to get a list of all public IPs and their instance names on Amazon EC2

Anyone know how to get a list of all public IPs and their instance names on Amazon EC2 using aws CLI?
This got me the list of public IPs, but not their associated instance names.
aws ec2 describe-instances --query "Reservations[].Instances[].PublicIpAddress" --output text
Thanks in advance.
http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html
Update: The CLI now supports filters:
aws ec2 describe-instances --query "Reservations[].Instances[].[PublicIpAddress,InstanceId,Tags[?Key=='Name'].Value]"
Got it working using this:
aws ec2 describe-instances --output table --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, PublicIpAddress]'
Adding this for folks that will commonly find this post when searching for how to get your instance info.
In powershell you can use the following:
(Get-EC2Instance -ProfileName Profile).Instances | select InstanceId,PrivateIPAddress,PublicIpAddress #{Name="Servername";
Expression={$_.tags | where key -eq "Name" | select Value -expand Value}} | Format-Table.
With the Python AWS CLI you can use:
aws ec2 describe-instances --region=us-east-1 --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],PrivateIpAddress,PublicIpAddress]' --output text --profile ProfileName