Get information about new instances spawned by auto scaling using CLI - amazon-web-services

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.

Related

How do I upgrade my AWS EC2 instance using the AWS CLI?

I would like to increase the number of CPU cores in my EC2 instance, using the AWS CLI.
My instance is currently a C5.4 Large.
I don't know the command for this, and I don't know if I have to know the instance type I want to switch to beforehand, or if I can browse different instance types from the AWS CLI.
aws ec2 help lists the commands describe-instances, stop-instances, modify-instance-attribute, and start-instances.
Calling aws ec2describe-instances may provide too much information. You can select the fields you would like with the --query option. --query takes a "JMESPath", which stands for "JSON Matching Expression paths" - a set of special syntaxes for getting values out of complicated JSON.1
You need enough information to identify the instance, for example, its name and type. Start the JMESPath with Reservations[*].Instances[*], followed by [InstanceId, InstanceType, KeyName].2
Example:
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[InstanceId, InstanceType, KeyName]' \
--region us-east-1
Copy the ID.
Stop the instance before upgrading it:
aws ec2 stop-instances \
--instance-ids "$INSTANCE_ID"
It can take 5-10 minutes for it to stop.
Change the instance type with modify-instance-attribute:
aws ec2 modify-instance-attribute \
--instance-id "$INSTANCE_ID" \
--instance-type "{\"Value\":\"$REQUESTED_TYPE\"}
Replace $REQUESTED_TYPE with the name of a type, for example, t2.small.3
There may be limitations on what kinds of instances are allowed for your account, so make sure it's an instance type you have access to.
Now, restart the instance and you're done:
aws ec2 start-instances \
--instance-ids "$INSTANCE_ID"
You'll have to wait a few minutes before the instance has finished booting up.
1 Here is a tutorial on using JMESPath.
2 Every time you call an ec2 command, it is important to specify the region with the option --region. If you don't query a specific region, you won't see your instances.
3 (The quotes are escaped with a backslash so your shell doesn't misinterpret them.)

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

How to know EC2 instance stopped time?

I really need to know about the stopped time of AWS EC2 instances. I have checked with AWS cloudtrail, but its not easy to find the exact stopped EC2 instance. Is possible to see exact time of stopped EC2 instances by aws-cli commands or any boto3 script?
You can get this info from StateTransitionReason in describe-instances AWS CLI when you search for stopped instances:
aws ec2 describe-instances --filter Name=instance-state-name,Values=stopped --query 'Reservations[].Instances[*].StateTransitionReason' --output text
Example output:
User initiated (2020-12-03 07:16:35 GMT)
AWS Config keeps track of the state of resources as they change over time.
From What Is AWS Config? - AWS Config:
AWS Config provides a detailed view of the configuration of AWS resources in your AWS account. This includes how the resources are related to one another and how they were configured in the past so that you can see how the configurations and relationships change over time.
Thus, you could look back through the configuration history of the Amazon EC2 instance and extract times for when the instance changed to a Stopped state.
Sometimes time is missing from StateTransitionReason, you can use CloudTrail and search for Resource Name = instance ID to find out StopInstance(s) API calls.
By default you can track back 90 days, or indefinitely if you create your own trail.

How can I set existing EIPs to Auto scaled instances in AWS when they launch automatically?

I have cloud formation template which creates auto-scaling group with desired state 2. I need instances to be attached to existing eips when they get launched. How can I do this?
You need to write a custom user data script that assigns the elastic IP to the instance. You can not do this using CloudFormation templates yet. The AWS CLI to be used is: aws ec2 associate-address. For this, the best practice would be to assign and IAM role with ec2:AssociateAddress permission.
The command will look like this: aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOCATION_ID --allow-reassociation
While the allocation id will need to be hardcoded in the template, you can get the instance id within the instance using the command: curl -s http://169.254.169.254/latest/meta-data/instance-id. Refer this thread
for more details.

EC2 instance from AWS command line

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.