How to check the aws ec2 instance creation date using command line? - amazon-web-services

To determine how old the aws ec2 instances are, and to destroy those nodes if they are older than 90 days, I need to check the creation date of the node.
What is the correct way to do that using COMMAND LINE?
What I tried?
I tried the command ec2metadata, but the output doesn't contain creation date.

You can get this information with
aws configservice get-resource-config-history --resource-type AWS::EC2::Instance --resource-id i-xxxxxxxx
The last element in this json is what you need.
You can check get-resource-config-history and find resources between specific dates.
You can also get creation date of your root volume with
aws ec2 describe-volumes --volume-ids vol-xxxxx
This can also give you age of your root volume (if not changed).

Related

How can I get the name of the most recent snapshot for an RDS DB instance using the AWS CLI?

Using the AWS CLI, how can I get the most recent snapshot for a particular DB instance?
I can get them through the GUI easily but I'd like to automate it.
You can use the aws rds describe-db-snapshots CLI command to get a list of DB snapshots, and then run a local query using --query to get the latest DB snapshot using the SnapshotCreateTime field.
SnapshotCreateTime -> (timestamp)
Specifies when the snapshot was taken in Coordinated Universal Time (UTC). Changes for the copy when the snapshot is copied.
Something like this:
aws rds describe-db-snapshots \
--db-instance-identifier your-id \
--query "sort_by(DBSnapshots, &SnapshotCreateTime)[-1].{id:DBSnapshotIdentifier,time:SnapshotCreateTime}"
Note that this query sorts snapshots by their ascending SnapshotCreateTime and then simply takes the last one in the list (as dictated by [-1]), which will be the one that was last created.
[Added] if you're looking for snapshots of Aurora DB clusters then you'd have to use describe-db-cluster-snapshots in place of describe-db-snapshots, but otherwise the process is similar: use DBClusterSnapshots and DBClusterSnapshotIdentifier (in place of DBSnapshots and DBSnapshotIdentifier).

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 to return less information from aws ec2 describe-instances

When running aws ec2 describe-instances a LOT of information is returned per instance. Is there a way to easily review a table of instances and their states? (i.e. without much of the other information)
Note: some other IAAS/PAAS tools typically display less information unless you explicitly ask for more with --verbose or similar. Whereas aws seems to give a lot by default
This will return a few fields likely to be of interest, namely:
Availability Zone
State Name (e.g. running, stopped etc)
Launch Time
Instance Type (e.g. t2.medium)
Instance ID
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone, State.Name, LaunchTime, InstanceType, InstanceId]' --output text
The above can easily be edited to include (or remove fields) as necessary
This comes from here thanks #Dusan Bajic

Error trying to create my first AWS Data Pipeline (Unable to construct an endpoint for ec2 in region None Error????)

I'm trying to create my first data pipeline using the console and the CLI template. The job contains a single activity, a CLIActivity that I want to start an AMI instance. But, the job fails with the error:
Unable to construct an endpoint for ec2 in region None
I'm not sure where to set the region. I have it in the myAWSCLICmd parameter:
aws ec2 start-instances --instance-ids ami-xxxxxxx --region us-east-1
But that obviously isn't were it needs setting. Can someone identify what needs to be set and where?
I hardcoded the command I previously had in the myAWSCLICmd parameter and it worked.

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.