I am trying to get launch configuration details by aws-cli, I know there have a command aws autoscaling describe-launch-configurations --launch-configuration-names my-launch-config, but I don't know what is the launch config name, I only have the ASG(auto-scaling group)name. I know in AWS console I can find the launch configuration name in the ASG detail page, but how to do this by aws-cli?
In another word, I want to get launch configuration details but I only have ASG name as input, what should be the command/commands in aws-cli.
You can get LC name using describe-auto-scaling-groups and then use describe-launch-configurations to get its details:
asg_name="dddd"
launch_config_name=$(aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-names ${asg_name} \
--query "AutoScalingGroups[0].LaunchConfigurationName" \
--output text)
echo ${launch_config_name}
aws autoscaling describe-launch-configurations \
--launch-configuration-names ${launch_config_name}
Related
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
I am running an AWS CLI command to get the latest Auto-scaling Group name in a given account, now I want to use the output (i.e. ASG Name) as an input to other AWS command to update that ASG.
Command to get the ASG name:
aws autoscaling describe-auto-scaling-groups --query "sort_by(AutoScalingGroups,&CreatedTime)[-1].{id:AutoScalingGroupName}" --output text
Command to update the ASG returned by above command:
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $NameReturnedByAboveCommand
I tried these solutions but I am not able to get it to work:
https://stackoverflow.com/a/38153936/2858235
https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output.html#controlling-output-format
I am using windows command but also tried using PS but no luck.
Is it possible to create and Auto Scaling Group Launch config with the CLI and define the instance tags in one command?
Maybe I am missing something but right now it looks like have to do it in two steps.
i.e.
aws autoscaling create-launch-configuration ...
and then
aws autoscaling create-or-update-tags --tags ...
Since you need to have asg LC created first to tag it, it is two step process as you mentioned.
https://docs.aws.amazon.com/cli/latest/reference/autoscaling/create-launch-configuration.html
This example creates a launch configuration based on an existing instance. In addition, it also specifies launch configuration attributes such as a security group, tenancy, Amazon EBS optimization, and a bootstrapping script:
https://docs.aws.amazon.com/autoscaling/ec2/userguide/autoscaling-tagging.html
aws autoscaling create-launch-configuration --launch-configuration-name my-launch-config --key-name my-key-pair --instance-id i-7e13c876 --security-groups sg-eb2af88e --instance-type m1.small --user-data file://myuserdata.txt --instance-monitoring Enabled=true --no-ebs-optimized --no-associate-public-ip-address --placement-tenancy dedicated --iam-instance-profile my-autoscaling-role
aws autoscaling create-or-update-tags --tags "ResourceId=my-asg,ResourceType=auto-scaling-group,Key=environment,Value=test,PropagateAtLaunch=true"
I've created an ASG with a min size and desired capacity set to 1. The EC2 instance is bind to an Application Load Balancer. I use ignition to define the user data of the Launch Configuration. I run defined in Ignition a script which execute these two commands:
# Set the ASG Desired Capacity - get CoreOS metadata
ASG_NAME=$(/usr/bin/docker run --rm --net=host \
"$AWSCLI_IMAGE" aws autoscaling describe-auto-scaling-instances \
--region="$COREOS_EC2_REGION" --instance-ids="$COREOS_EC2_INSTANCE_ID" \
--query 'AutoScalingInstances[].AutoScalingGroupName' --output text)
echo "Check desired capacity of Auto Scaling group..."
# shellcheck disable=SC2154,SC2086
/usr/bin/docker run --rm --net=host \
$AWSCLI_IMAGE aws autoscaling set-desired-capacity \
--region="$COREOS_EC2_REGION" --auto-scaling-group-name "$ASG_NAME" \
--desired-capacity 3 \
--honor-cooldown
The problem is that I get as error ScalingActivityInProgress so I can't change the desired capacity.
First I'd like to understand the root cause. Is it maybe because the ALB is not healthy when I run the above commands?
Solved removing honor-cooldown param from the request
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.