How to catch AWS EC2 Instance IPs dynamically? - amazon-web-services

How to catch few AWS EC2 Instances IPs and put them to a script variable if its generates every time randomly and automatically?
I was trying to make it with
echo "$(curl http://169.254.169.254/latest/meta-data/public-ipv4/) master" >> /etc/hosts
but it is just the IP of one of them.
Also was trying with
aws ec2 describe-instances ... but don't know how to separate clear IP with other information. Any suggestions with awk \ sed?

Use the AWS Command-Line Interface (CLI) with a --query parameter:
aws ec2 describe-instances --query 'Reservations[*].Instances[*].{ID:InstanceId,Public:PublicIpAddress,Private:PrivateIpAddress}' --output text
i-2da518a2 172.31.15.3 None
i-6d261640 172.31.27.232 56.64.218.82
i-b3aa3476 172.31.5.0 None
i-6c57c951 172.31.20.243 56.79.129.118
i-192b95c1 172.31.28.76 56.253.207.57
i-af413c91 172.31.27.17 None
You can also output as JSON, which is easier to parse.

End command is
echo "$(aws ec2 describe-instances --filters Name="tag-value",Values="nagios" |grep PrivateIpAddress | awk '{gsub(",","",$2); gsub("\"","",$2); print $2}' | head -n 1) master" >> /file
To catch a dynamic ip address from your aws instance with tag and put it to any file

For example if you want to get all the private IP's which are behind a load balancer and pass it to a file.
/usr/bin/aws --output text --query "Reservations[].Instances[].PrivateIpAddress" ec2 describe-instances --instance-ids aws --output text --query "LoadBalancerDescriptions[0].Instances[*].InstanceId" elb describe-load-balancers --load-balancer-name <loadbalancer name> > hosts.txt
hope it helps....

Related

AWS CLI Instance ID in variable

Hi can someone help with this
I am using the Amazon AWS CLI command in a bash script and have the following line and the output it gives.
aws ec2 describe-instances --instance-ids $Ins --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text
' does not existd (InvalidInstanceID.NotFound) when calling the DescribeInstances operation:
The instance ID 'i-0c7bf4181bdfxxxxx Will be backed up
If I echo the value of $ins and hard code it in the command like
$ echo $Ins
i-0c7bf4181bdfxxxxx
$ aws ec2 describe-instances --instance-ids i-0c7bf4181bdfxxxxx --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text
lon-prod-xxxx-xxxx
I don't understand why it works in the command when copied and pasted but not when used as a variable?
Additional Code, sure there are neater ways to do this but just need something quick. Just grabbing all the instance ids from a single VPC and then attempting to take an image of each in turn.
Instances=$(aws ec2 describe-instances --filter "Name=vpc-id,Values=$VPCID" --query 'Reservations[*].Instances[*].[InstanceId]' --output text)
for Ins in $Instances; do
echo $Ins
name=$(aws ec2 describe-instances --instance-ids $Ins --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text)
echo $name Will be backed up
echo $Ins
aws ec2 create-image --instance-id $Ins --name "$name" --description "Auto backed up on $(date)" --no-reboot --$dryrun
echo "***"
done
enter code here
error is below, the first id is where i am echoing $Ins so it seems to know the ID, but i think it has a /r /n after it
i-0c7bf4181bdfxxxxx
' does not existd (InvalidInstanceID.NotFound) when calling the DescribeInstances operation:
The instance ID 'i-0c7bf4181bdfxxxxx Will be backed up
OK I fixed it, the variable did have a new line after it "/r"
Added this line
Ins=${Ins/$'\r'/} to strip it out and works OK now.

How do I find all Linux EC2 instances

I can use the following to list all Amazon Windows EC2 instances. How do I list Linux instances?
aws ec2 describe-images --owners self amazon --filters "Name=root-device-type,Values=ebs" "Name=platform,Values!=windows"
I think you can use the below to list all instances with platform type, then filter by platform type.
for region in `aws ec2 describe-regions --output text | cut -f2|awk -F. '{print $2}'`; do echo -e "\nInstances in: '$region':"; aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text --region $region; done;
Platform type "None" indicates the Linux.

aws cli describe-instances without a VpcId

I want to run aws ec2 describe-instances looking for any instances without a VpcId property (those in ec2-classic)
How can I return ec2-classic instances using either the --query flag or JMESPath expression to get results without a VpcId?
This cli command will list all the instances which doesn't have VpcId.
aws ec2 describe-instances --region us-east-1 --query 'Reservations[*].Instances[?!not_null(VpcId)] | [].[InstanceId]' --output text
You can tweak the same to list all instances which has VpcId.
aws ec2 describe-instances --region us-east-1 --query 'Reservations[*].Instances[?not_null(VpcId)] | [].[InstanceId]' --output text
One approach is to query all instances and look for entries that do not have a SubnetId. The following CLI lists the EC2 classic instances. You can change the --query option to get the attributes you want.
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value | [0], State.Name, SubnetId]' --output text | grep -v subnet
Output
i-123456789abcdef01 MyClassicRunning running None
i-123456789abcdef23 MyClassicStopped stopped None

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