Display security groups for a specific instance using aws cli - amazon-web-services

I am trying to find out which security groups a specific aws ec2 instance is in. I know I can do aws ec2 describe-instances
and then filter this result and do various things to it by piping the result to grep, but what has frustrated me is that I cannot use aws ec2 describe-instance-attribute --instance-id [instance-id] --attribute securityGroups or aws ec2 describe-instance-attribute --instance-id [instance-id] --attribute Groups , despite the documentation at: describe-instance-attribute suggesting that you can. Any ideas how to do this?

There is no such attribute called Groups. Refer: http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-attribute.html
from above link:
Same is mentioned in EC2 API Reference Guide: http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstanceAttribute.html
From Above link:
Probably groupSet attribute is what you are looking for:
e,g:
aws ec2 describe-instance-attribute --instance-id [instance-id] --attribute groupSet

Related

How to list AWS RDS instances that are stopped

I'm trying to use the AWS CLI to list all the AWS RDS instances that I have that are in a Stopped status.
It's possible with EC2 with aws ec2 describe-instances and adding a filter --filters "Name=instance-state-name,Values=stopped".
However, with aws rds describe-db-instances, I do not find an equivalent. There is a --filter option, but only had the following options as filters: db-cluster-id, db-instance-id, dbi-resource-id, domain, engine.
So what AWS CLI command can I use to list all the RDS instances that are currently Stopped (Status=stopped)?
Use --query instead of --filters:
something like:
aws rds describe-db-instances --query '...'
https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html
You can do something like this:
aws rds describe-db-instances --query 'DBInstances[].DBInstanceStatus[]'
output:
[
"available"
]

How to delete autoscaling groups with aws cli?

I am trying to write a bash script that will delete my EC2 instances and the auto scaling group that launched them:
EC2s=$(aws ec2 describe-instances --region=eu-west-3 \
--filters "Name=tag:Name,Values=*-my-dev-eu-west-3" \
--query "Reservations[].Instances[].InstanceId" \
--output text)
for id in $EC2s
do
aws ec2 terminate-instances --region=eu-west-3 --instance-ids $id
done
aws autoscaling delete-auto-scaling-group --region eu-west-3 \
--auto-scaling-group-name my-asg-dev-eu-west-3
But it fails with this error:
An error occurred (ResourceInUse) when calling the DeleteAutoScalingGroup operation:
You cannot delete an AutoScalingGroup while there are instances or pending Spot
instance request(s) still in the group.
There is no issue if I use the AWS console to do the same thing. Why does the aws cli prevent me from deleting the ASG if I have terminated all the instances?
if you really want to do this with CLI, you may first want to use aws autoscaling suspend-processes command to prevent ASG from creating new instances. Then use aws ec2 terminate-instances like you are doing. Then use aws ec2 wait instance-terminated command and pass instance ids. Once all that is done, you should be able use aws autoscaling delete-auto-scaling-group
aws ec2 terminate-instances will return before the instances have finished terminating (which could take several minutes).
I highly recommend using something like CloudFormation or Terraform for this sort of thing instead of the AWS CLI tool.
You can force delete the ASG with active spot instance requests with AWS cli:
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name Your-ASG-Name --force-delete

EC2 CLI. Help creating VPCs with name

I am trying to streamline the process for creating VPC/EC2 environments without using the gui. I also want to automate it by telling a script what I want created with what properties.
I decided that the best place to start is to create a VPC and create an EC2 instance with in it.
I am using
aws ec2 create-vpc --cidr-block 10.0.0.0/16
But I wanted to name it something like myVPC. Is there a way to do things like this? I am very new to this so if you have any documentation regarding this please send it my way.
Thank you!
Generally, AWS resources don't have names. Instead they have IDs. What passes for a 'name' of Production is actually a tag with the key/value pair Name=Production.
To set a name tag for a VPC, use the CLI's ec2 create-tags command. For example:
aws ec2 create-tags --resources vpc-1a2b3c4d --tags Key=Name,Value=Production
If you really want a one liner:
aws ec2 create-vpc --cidr-block 10.0.0.0/16 --output text | awk '{print $NF}' | xargs aws ec2 create-tags --tags Key=Name,Value=MyVPC --resources
It is a concatenation of two commands explained below.
Adding a tag while creating a VPC is not supported yet. Create a VPC like the following. The last value is VPC ID.
aws ec2 create-vpc --cidr-block 10.3.0.0/16 --output text
VPC 10.3.0.0/16 dopt-a54153c7 default False pending vpc-f13d7295
Use create-tags to add a tag to the created VPC
aws ec2 create-tags --resources vpc-f13d7295 --tags Key=Name,Value=MyVPC
You could use --tag-specifications:
aws ec2 create-vpc --cidr-block 10.0.0.0/24 --tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC}]"
https://docs.aws.amazon.com/cli/latest/reference/ec2/create-vpc.html#options
Unrelated to your specific question, but allow me to highly recommend AWS CloudFormation for managing these resources. It's a nicer method of definition that just the CLI, allows you to group resources or delete a stack. I use the CLI to call the Cloudformation, specifying a template.
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html

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.

Finding EC2 status using EC2 API

Is there any way to find out status of AWS EC2 instances, which are running on various different regions, from one EC2 instance which is present in any one of region by using EC2 API tool ?
How this is possible ?
I got the answer :-
ec2-describe-instances instance-ID --region region
Example :-
ec2-describe-instances i-f82d5ca0 --region eu-west-1
Where instance ID is EC2 instance ID which is located in region eu-west-1
Thats all .
Or in the new unified AWS CLI, this is slightly different:
aws ec2 describe-instances --instance-id i-f82d5ca0
You can also change the --output into json, text, or a table