EC2 instance from AWS command line - amazon-web-services

I am very new to AWS. I've got a Windows instance running and have my aws command line configured. I've read through the AWS docs but can't seem to find exactly what I'm looking for.
How do I view my current instances from the command line?

If by view your current instances, you mean list all running instances from the command line, you can call the describe-instances command:
aws ec2 describe-instances
This will list all of your current instances.
See describe-instances

As noted in the answer by Rodrigo M, you should use describe-instances to view your EC2 instances. In general, the help command is the best way to explore the CLI. Start with aws ec2 help and try the various options. You can get more details on subcommands with aws ec2 describe-instances help as well.
The output is a bit verbose and by default in JSON. This can be a bit overwhelming and hard to read without additional processing. I'd recommend getting familiar with the --query aws CLI parameter if you intend to use the CLI interactively.
In particular, I use this for a quick overview of my EC2 instances:
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`] | [0].Value, State.Name, PublicDnsName]' --output table
To check one particular attribute on an instance:
aws ec2 describe-instances --query Reservations[0].Instances[0].InstanceType --output text --instance-ids <my-instance-id>
The CLI is very powerful once you get comfortable with learning the commands and managing the output. It's also helpful for learning the porgramming APIs as well, since aws CLI commands generally map one-to-one with an API call.

Related

You do not have any instances in this region

I have created EC2 instance in the my office PC. It was successfully and I used it well. But when I logged to the AWS console from my home laptom - no one instance exist there and I have the error You do not have any instances in this region. I try to search instance information in other regions but do not found any.
How I can found my created instance or list all instances independently of region?
Quick way would be to use combination of AWS CLI, jq and a simple Bash for loop to iterate through each region and list the instances. Be sure to set your credentials before running
for region in `aws ec2 describe-regions | jq .Regions\[\].RegionName -r`
do
echo -e "\tRegion: ${region}"
aws ec2 describe-instances --query "Reservations[*].Instances[*].{InstanceID:InstanceId}" --output=table --region ${region}
done
You can copy post the code in your Linux shell, or run them in AWS CloudShell which gives you an authenticated shell with aws cli preinstalled

AWS Command Line querying for EC2 Describe Network Interfaces that are available

Been searching around and playing around with the aws ec2 describe-network-interfaces command...
Can't seem to get a specific query to work.
Wanted to retrieve a list of ENI's that are currently with the status: available.
aws ec2 describe-network-interfaces --filters Name=Status,Values=available --query 'length(NetworkInterfaces)'
Tried the command above, and output an error. Probably due to no structure of Name=Status.

EC2 Instance Status using cygwin terminal

I'm trying to get into the reporting of AWS instances within my environments, and I am trying to create a script using AWS cli to generate a report of the status of EC2 instances. I'm still a beginner, so I'm learning how all this works. I was wondering if it was possible to do with a line similar to this:
AWS EC2 describe-instances --region $REGION --query 'Reservations[].Instances[?LaunchTime>=`2015-03-01`][].{id: InstanceId, type: InstanceType, launched: LaunchTime}'
I am using the Cygwin Terminal to perform this query, and any with this would be awesome thanks!
If it's possible I am also trying to see if I could get the CPU usage at that time of the report. I am still trying to understand how all this works since I am new to the AWS API.
Firstly, you will need to install the aws-sdk. Checkout the following answer for doing this in cygwin.
Then you can configure your account using aws configure and following the prompts.
After that you should be able to run aws ec2 describe-instances.
Thanks for everyone's input! I was able to figure it out here. I'm starting to understand how the population output all comes together, but right now I'm trying to put this information into at TSV file. To get the status and launch time of the instances I used the following:
aws ec2 describe-instances --region $REGION --query "Reservations[].Instances[].[InstanceId, LaunchTime, State.Name] --output text >> Instances_In_AWS.tsv
Thank you guys for your help!

Aws commands are not responding

When I was trying to type "aws ec2 describe-instances" it will gives a blink cursor (AWS Command line). No results are showing.I have tried with setting the configurations for the user as well.
You have to provide instance details to get a description. like
aws ec2 describe-instances --instance-ids i-5xxxxbx

Get information about new instances spawned by auto scaling using CLI

I am working on creating a monitor dashboard for monitoring status of ec2 instances.
I am searching for a method to get information (especially instances ID) of newly spawned instances using auto scaling.
Can anyone point me in the right direction. Thanks
If you know your instance type then you can use describe-instances command to get details about instances and use query command to get the details what you need (in your case Instance-id)
aws ec2 describe-instances --filters "Name=instance-type,Values=t1.micro" --query 'Reservations[*].{InstanceId:Instances[0].InstanceId}'
## Enter your instance type in the 'Values' field of '--filters' command
I was able to get instance id by using combination of following commands
aws elb describe-load-balancers --load-balancer-name "LoadBalanceID" --region "region" --output text | grep INSTANCES
Using the AWS CLI you can get a list of scaling activities for an auto scaling group.
aws autoscaling describe-scaling-activities --auto-scaling-group-name my-group-name
See AWS CLI
This is the newer Python CLI, so you would need to install that if you have not already done so. It will return a JSON block with all of the scale up and down activities in the group, including the reason an the date and time.