How can I use the AWS CLI to stop every EC2 instance in a region (1 VPC)?
I did see an old thread saying that this should work:
aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=pending,running" --query "Reservations[].Instances[].[InstanceId]" --output text | tr '\n' ' ')
However, I get the following error when using the above command:
Unknown options: --filters, Name=instance-state-name,Values=pending,running
Related
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
I need a single Windows CMD command that terminate all instances from Ohio region. I found this commands but its not working.
aws ec2 terminate-instances \
--region us-east-2 \
--instance-ids (aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId]" --region us-east-2)
Try this out in powershell:
foreach ($id in (aws ec2 describe-instances --filters --query "Reservations[].Instances[].[Instance
Id]" --output text --region us-east-2)) { aws ec2 terminate-instances --instance-ids $id }
You can pass the --dry-run flag with terminate instances to confirm first if you'd like.
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.
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
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