Get AllocationID in aws cli for powershell - amazon-web-services

How to filter out AllocationId for Elastic IP address in AWS cli 2.0 in Powershell.
aws ec2 describe-addresses --query 'Addresses[?AssociationId!=`null`].{AllocId:AllocationId} | [0]' --output text --region ap-southeast-2 --filters "Name=tag:xxx,Values=xxx"
tried this, returns Null. Need help especially with TAG filter.
I am trying to automate the process of EIP allocation to my newly generated Instance from autoscaling group.

Try this as it working for me, I just reorder the command query after filter.
aws ec2 describe-addresses --region ap-southeast-2 --filters "Name=tag:Name,Values=xxx" --query 'Addresses[?AssociationId!=`null`] | [0]' --output table

Related

AWS CloudShell - List instances by ARN prefix

In AWS Backup, I have created a resource assignment to a backup-plan, which targets all EC2 instances.
The ARN prefix looks like this:
arn:aws:ec2:*:*:instance/*
How can I list all instances that match an ARN prefix? Either in AWS Cloudshell or with the aws cli?
I think you can try using ec2's describe-instances cli command and run it over all AWS regions :
for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region
done

How to Start AWS instance if it has Tag exists

I would like to run AWS CLI command to start the instance if it has relevant Tags exist for Eg "MigratedBy". If Tag doesnt exists, it shouldnt start the instance at all
I tried running below command but it didnt work out
aws ec2 start-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:MigratedBy,Values=my-super-tag" --query 'Reservations[].Instances[].InstanceId' --outpu t text`
It seems this command would run all the instance with the tag "MigratedBy" but this is wrong in my case. I would like to just start the particular instance if tag exists otherwise not
If you want to filter based only on the existence of the tag you can include it in the query and then pipe to show only the InstanceId:
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key=='MigratedBy'],InstanceId] | [*][1]' --output text
Finally, to start those instances the whole command will be:
aws ec2 start-instances --instance-ids `aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key=='MigratedBy'],InstanceId] | [*][1]' --output text`
Reference:
Filtering AWS CLI output

AWS CLI - JMESPath query to find vpc-id by tag

I want to list the VPC id's which have a particular tag (Name=MyVPC).
I am aware that I can use --filter and run:
aws ec2 describe-vpcs --filters Name=tag:Name,Values=MyVPC --query 'Vpcs[].VpcId'
This works completely fine.
Is there a way I can achieve this without using --filter and only use JMESPath?
aws ec2 describe-vpcs --query 'Vpcs[?Tags[?Key==`Name`]|[?Value==`MyVPC`]].VpcId' --output text
Try this command:
aws ec2 describe-vpcs --query 'Vpcs[?contains(Tags[?Key==`Name`].Value[], `MyVPC`) == `true`].[VpcId]' --output text

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

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