How to know EC2 instances by region from aws-cli?
Desired output:
Region name name
us-west-1 instance1
us-west-1 instance2
us-west-2 instance1
us-east-1 instance1
You can only list instances via the CLI from one region at a time. So you would write a script that loops through each region, getting the instances in each region.
Here's a good starting point for a script:
#!/bin/bash
all_regions="us-east-1 us-east-2 us-west-1 us-west-2"
echo "Region Name Instance ID"
for region in ${all_regions}; do
aws ec2 describe-instances --region ${region} | \
grep '"InstanceId":' | \
perl -pe "s/.*: \"(i-.*?)\".*/${region} \1/"
done
The aws command above is the AWS command line interface:
https://aws.amazon.com/cli/
describe-instances is one of the commands for the AWS CLI:
http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html
grep and perl are standard utilities.
Related
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
I need to get information such as VPCs, subnets, security groups, etc for many AWS accounts at once. How can I go about this?
One solution is to use a for loop with the AWS CLI. Check out the CLI Documentation for the service that you're wanting to gather information for and find the appropriate commands then use a for loop to loop over the profiles in your ~/.aws/credentials file.
For example, if you're wanting to get the VPCs, subnets, and security groups, those are all described in the EC2 CLI docs.
Here is an example of getting information about those resources and outputting it into the current directory as .json (this assumes you didn't change the default output format when using aws configure
#!/usr/bin/env bash
region=us-east-1
for profile in `grep [[] ~/.aws/credentials | tr -d '[]'`
do
echo "getting vpcs, subnets, and security groups for $profile"
aws ec2 describe-vpcs --region $region --profile $profile > "$profile"_vpcs.json
aws ec2 describe-subnets --region $region --profile $profile > "$profile"_subnets.json
aws ec2 describe-security-groups --region $region --profile $profile > "$profile"_security_groups.json
done
How do I find the name of "this" Lightsail instance. "This" being the instance that the aws command is being executed. My below script isn't working, since I thought Lightsail is just another EC2 instance.
#!/bin/bash
InstanceId=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
echo $InstanceId
Region=`aws configure get region`
echo $Region
InstanceName=$(aws ec2 describe-tags --region $Region --filters "Name=resource-id,Values=$InstanceId" "Name=key,Values=Name" --output text | cut -f5)
echo $InstanceName
The name of a Lightsail instance can be obtained with:
aws lightsail get-instances --query instances[].name
In my case, this was the auto-assigned name when I started the instance via the Lightsail management console. I couldn't see a way to change the name during launch.
Interestingly, I could not find a way to use the AWS CLI to list tags associated with a Lightsail instance. For example, I could not retrieve the Name tag that I manually added to an instance, and which appears in the Lightsail console.
Update:
After discussion in comments, I got this working:
aws lightsail get-instances --query "instances[?contains(supportCode,'`curl -s http://169.254.169.254/latest/meta-data/instance-id`')].name" --output text
I am trying to get instance-id, availability zone, instance status, instance name(from tags) of ec2 instances hosted in us-east-1 and us-west-1 region in a single AWS CLI command so that I can export the output in an excel file.
I can get the output from one region at a time using the below commands but couldn't find a way to get the output from two regions using one single command.
aws ec2 describe-instances --region us-east-1 --query Reservations[].Instances[].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value} --output text>C:\Users\PiyushVermaVerma\Desktop\testfile.xls
and:
aws ec2 describe-instances --region us-west-1 --query Reservations[].Instances[].[InstanceId,Tags[0].Value,Placement.AvailabilityZone,State.Name] --output text>C:\Users\PiyushVermaVerma\Desktop\testfile.xls
In Bash, it is not possible to specify more than one region but you can always do this:
for region in us-east-1 us-west-1 ; do
aws ec2 describe-instances --query \
'Reservations[*].Instances[*].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value}' \
--output text --region $region
done > C:\Users\PiyushVermaVerma\Desktop\testfile.xls
For Windows Batch, you are probably best off just running the two commands in sequence, and using the append >> operator:
aws ec2 describe-instances --region us-east-1 --query Reservations[].Instances[].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value} --output text > C:\Users\PiyushVermaVerma\Desktop\testfile.xls
aws ec2 describe-instances --region us-east-1 --query Reservations[].Instances[].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value} --output text >> C:\Users\PiyushVermaVerma\Desktop\testfile.xls
You can only list instances in one region at a time.
Each region is a collection of zones. You are connecting to each region to list instances in the zones within that region. These zones are data centers and most are fairly large.
You will need to iterate thru each region.
I have found a script for starting/stopping a dynamically created ec2 instance, but how do I start any instances in my inventory?
Seems you are talking about scripting, not SDK. So there are two tools to do the job.
1 AWS CLI tools
download aws cli tool and set the API Key in $HOME/.aws/credentials
list all instances on region us-east-1
Confirm which instances you are targeting.
aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' --region us-east-1 --output text
2 Amazon EC2 Command Line Interface Tools
download and setup instruction
list all instances on region us-east-1
You should get same output as WAY #1.
ec2-describe-instances --region us-west-2 |awk '/INSTANCE/{print $2}'
With the instance ID list, you can use your command to start them one by one.
for example, the instance name are saved in file instance.list
while read instance
do
echo "Starting instance $instance ..."
ec2-start-instances "$linstance"
done < instance.list
BMW, give you an excellent startup, but you can even summarise the thing like this:
1) First get the id of all the instances and save them into a file
aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' --region us-east-1 --output text >> id.txt
2) Then simply run this command to start all the instances
for id in $(awk '{print $1}' id.txt); do echo "starting the following instance $id"; aws ec2 start-instances --instance-ids --region us-east-1 $id; done
Please change the region, I am considering that you have installed and setup the AWS CLI tools properly. Thanks