How to get the InstanceID using the PublicDnsName? - amazon-web-services

With the AWS CLI, given a PublicDnsName, how can I get the InstanceID? I tried
aws ec2 describe-instances --filters 'Name=publicdnsname,Values=ec2....
but it complains that
publicdnsname is not a valid filter

It is dns-name
aws ec2 describe-instances --filters "Name=dns-name,Values=ec2-xxxxx.compute-1.amazonaws.com" --query 'Reservations[*].Instances[*].InstanceId' --output text
Output
i-00123458ca3fa2c4f

The valid filters are listed in the docs.
To filter by public DNS name the filter name you should use is: dns-name

Related

Delete EBS Snapshot From Shell Script with Name and Date FIlter

Hi I want to delete EBS snapshot which is older then 30 days and want to filter out with pharse of it's name. Let's say the name contain word 'Manish'.
The name can be like that 'Manish-Ebs-snapshot' or EBS-Manish-snapshot or EBS-ManishFinal-Snapshot' like that.
I found out one command but it's collecting all the snapshot not a particular group of snapshot.
snapshots_to_delete=$(aws ec2 describe-snapshots --owner-ids xxxxxxxxxxxx --query 'Snapshots[?StartTime<=`2020-08-23`].SnapshotId' --output text)
echo "List of snapshots to delete: $snapshots_to_delete"
for snap in $snapshots_to_delete; do
aws ec2 delete-snapshot --snapshot-id $snap
done
can anyone help me with this above script how I can add name filter as well.
The field I want:
You can filter by description using *Manish*:
aws ec2 describe-snapshots --owner-ids xxxxxxxxx --filter Name=description,Values=*Manish* --query 'Snapshots[?StartTime<=`2020-08-23`].SnapshotId'
And by tag Name:
aws ec2 describe-snapshots --owner-ids xxxxxxxx --filter Name=tag:Name,Values=*Manish* --query 'Snapshots[?StartTime<=`2020-08-23`].SnapshotId'

Retrieve EC2 instance attributes and flatten the output using AWS EC2 CLI

I am using the command below aws ec2 describe-instances to return list of running EC2s. The output is a TSV in 2 lines, first line being Account ID (OwnerId), and the rest of the query is displayed at the second line. I would like to make all the attributes to be flattened in one line, but due to the nature of the json output of the API calls, is there a way to manipulate the output to be in just one line?
aws ec2 describe-instances --output text --profile $account --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]' --filter --filters Name=instance-state-name,Values=running >> $outfile
1234567890
i-03cxxxxxxxdab t2.medium running windows ap-southeast-1a 10.0.0.0 10.10.0.10 api-abc-prod-01 PROD
desired output in one line
1234567890 i-03cxxxxxxxdab t2.medium running windows ap-southeast-1a 10.0.0.0 10.10.0.10 api-abc-prod-01 PROD
First thing, AWS cli offer to specify the output formate, so you can set the output to text then replace the new line with space. you can try
aws ec2 describe-instances --output text --profile test --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]' --filter --filters Name=instance-state-name,Values=running --output text | tr '\r\n' ' '
Text Output Format
The text format organizes the AWS CLI's output into tab-delimited
lines. It works well with traditional Unix text tools such as grep,
sed, and awk, as well as the text processing performed by PowerShell.
The text output format follows the basic structure shown below. The
columns are sorted alphabetically by the corresponding key names of
the underlying JSON object.
IDENTIFIER sorted-column1 sorted-column2
IDENTIFIER2 sorted-column1 sorted-column2
so you are good to go with pipe using tr '\r\n' ' '
Or you can use awk
aws ec2 describe-instances --output text --profile test --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]' --filter --filters Name=instance-state-name,Values=running | awk 1 ORS=' '
update:
If you want to append ownerID with each instance details then use this.
aws ec2 describe-instances --output text --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]' --filter --filters Name=instance-state-name,Values=running | paste -d" " - -
As recommended in the top answer in Parsing JSON with Unix tools --> you can use jq to do this https://stedolan.github.io/jq/, check the tutorial here on extracting the relevent fields... https://stedolan.github.io/jq/tutorial/

How can I call file using AWS CLI

I am looking forward to call \File.txt using below AWS CLI , if you have exp please share it.
aws ec2 describe-instances --filters Name=network-interface.group-name,Values='\file.txt' --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,Tags[?Key==Name].Value|[0]]'
File.txt includes Security Group Names.
Here is a way you can do it, I'm assuming Security Group Name is on a separate line.
Command:
$(cat file.txt|sed 'N;s/\n/,/')
Input: file.txt:
cat file.txt
security-group-name-1
security-group-name-2
security-group-name-3
Output:
security-group-name-1,security-group-name-2
Full command:
aws ec2 describe-instances --filters Name=network-interface.group-name,Values=$(cat file.txt|sed 'N;s/\n/,/') --query 'Reservations[].Instances[].[Placement.AvailabilityZone, State.Name, InstanceId,Tags[?Key==Name].Value|[0]]'

How to list only the instance id and related tags

I using the CLI tools and I want to list only the instance ID and related tags of an instance. The command that I am using is ec2-describe instances. I have tried certain filters but nothing is working out.
Can anybody help ??
error :
./Instance_Audit.sh: line 24: $: command not found
./Instance_Audit.sh: line 25: syntax error near unexpected token `do'
./Instance_Audit.sh: line 25: ` do echo $ID ; echo $(aws ec2 describe-instances --query "Reservations[].Instances[?InstanceId==\`$ID\`].Tags[]") done'
Using the aws cli, I can produce a few different alternatives of the tag output:
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,Tags[]]'
Result: i-xxxxx [{u'Value': 'key1value', u'Key': 'key1name'}, {u'Value': 'key2value', u'Key': 'key2name'}]
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,Tags[].*]'
Result: i-xxxxx [['key1value', 'key1name'], ['key2value', 'key2name']]
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,Tags[].Key,Tags[].Value]'
Result: i-xxxx ['key1name', 'key2name'] ['key1value' 'key2value']
Is any of those formats what you're looking for?
The AWS CLI command
aws ec2 describe-instances --region us-west-2 --query 'Reservations[].Instances[].[InstanceId,Tags[]]' --output text
produces these results:
i-789c55c3
Name Instance1
aws:cloudformation:logical-id Instance1
aws:cloudformation:stack-id arn:aws:cloudformation:us-west-2:012345678901:stack/test10-test10-foobarfoobar/a6e545a0-af52-11e4-a0be-50d5020578e0
aws:cloudformation:stack-name test10-test10-foobarfoobar
i-77c108cc
Name Instance2
aws:cloudformation:logical-id Instance2
aws:cloudformation:stack-id arn:aws:cloudformation:us-west-2:012345678901:stack/test10-test10-foobarfoobar/a6e545a0-af52-11e4-a0be-50d5020578e0
aws:cloudformation:stack-name test10-test10-foobarfoobar
If that is not in the format you are looking for, can you provide an example of they format you are expecting?
If you want to only display the instance id and the tags associated to that instance, you can use something like :
$ for ID in $(aws ec2 describe-instances --region eu-west-1 --query "Reservations[].Instances[].InstanceId" --output text); do echo $ID ; echo $(aws ec2 describe-instances --region eu-west-1 --query "Reservations[].Instances[?InstanceId==\`$ID\`].Tags[]") done
i-60****2a
[ [ { "Value": "SB", "Key": "Name" } ] ]
i-1a****56
[ [ { "Value": "Course and Schedule on Apache 2.4", "Key": "Name" } ] ]
i-88eff3cb
[ [ { "Value": "secret.com", "Key": "Name" } ] ]
i-e7****a5
[ [ { "Value": "2014.03 + Docker", "Key": "Name" } ] ]
I could not find a way to do that with only one AWS CLI call. Should someone come up with a shorter solution, I would love to hear from you.
If you want to filter to certain tag key/value only, you can edit the aws ec2 describe-instances to add a --filter option.
For example :
aws ec2 describe-instances --region eu-west-1 --filter "Name=tag:Name,Values=SB"
--Seb
simple answer:
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[InstanceId,Tags[*]]'
not so simple answer .. the same principle could be applied to both instances and vpc's
# how-to get all the tags per instance-id with aws ec2 for prd env
while read -r i ; do \
aws ec2 describe-tags --filters "Name=resource-id,Values=$i" \
--profile prd; done < <(aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].InstanceId' --profile prd)
# how-to get all the tags per virtual-private-cloud-id
while read -r v; do aws ec2 describe-tags \
--filters "Name=resource-id,Values=$v" ; \
done < <(aws ec2 describe-vpcs \
--query 'Vpcs[*].VpcId'|perl -ne 's/\t/\n/g;print')

aws cli: How can I query list values?

The aws cli has a --query option, which allows you to select only some information.
For an example, I am interested in getting just the Security group name from ec2 describe-instances.
If I run:
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,InstanceType,SecurityGroups]
my output looks like:
i-xxxxxxx m1.type [{u'GroupName': 'groupName', u'GroupId': 'sg-xxxxx'}]
I can also access elements of the list using an index:
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,InstanceType,Tags[0].Value,Tags[0].Name]
Is it possible to query tags so that instead of Tag[0] I search for a Tag where the name is specified?
As of 1.3.0, you can now query that information like this:
--query 'Reservations[*].Instances[*].Tags[?Key==`<keyname>`].Value[]'
So where you have this:
"Tags" : [
{
"Value" : "webserver01",
"Key" : "InstanceName"
},
you'd want to do this:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].Tags[?Key==`InstanceName`].Value[]'
What you probably want to use is the --filters option:
aws ec2 describe-instances --output text --filters "Name=tag-key, Values=SecurityGroups, Name=tag-value, Values=Foo" --region us-east-1
You can change the filters around to "query" for the exact field you're looking for.
checkout this slideshare from the Atlanta AWS meetup group's talk on the new AWS CLI for more examples
This way works for me: (this only works in version 1.3.0 and above)
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Tags[?Key==`Name`].Value[*]]'
select security_groups from aws.aws_ec2_instance;
> select security_groups from aws.aws_ec2_instance limit 1;
+---------------------------------------------------------------------------------------------------------------------------------+
| security_groups |
+---------------------------------------------------------------------------------------------------------------------------------+
| [{"GroupId":"sg-xxxx","GroupName":"xxxx"},{"GroupId":"sg-xxxxxx","GroupName":"xxxx"}] |
+---------------------------------------------------------------------------------------------------------------------------------+
This will just list out the security groups for your instances.
You can also use
select security_groups from aws.aws_ec2_instance where instance_id = 'i-xxxxxx';