How to auto scale my instance in Amazon EC2? - amazon-ec2

I just have created Amazon EC2 image and I'm new to this environment.
I'm interested in "auto scale" part of Amazon EC2.
But I could not find clear guide to find whether I'm using "auto scale" or not and how to auto scale my instance.
How can I setup "auto scale" properly and easily?

Here are some links that might help you setup auto-scaling:
http://kkpradeeban.blogspot.com/2011/01/auto-scaling-with-amazon-ec2.html
http://www.codebelay.com/blog/2009/08/02/how-to-load-balance-and-auto-scale-with-amazons-ec2/

You will need to download, unzip and setup the Auto Scaling Command Line Tool
You will need an AMI e.g. ami-xxxxxx
and a security group e.g. my-securitygroup-sg
and a key e.g. myKey
Now create a Launch Configuration, in this case called: my-launch-config-1
as-create-launch-config my-launch-config-1 --image-id ami-xxxxxx --region eu-west-1 --instance-type m1.small --group my-securitygroup-sg --key myKey
Then you can create the Auto Scaling Group
as-create-auto-scaling-group my-auto-scaling-group --region eu-west-1 --launch-configuration my-launch-config-1 --availability-zones eu-west-1a eu-west-1b eu-west-1c --min-size 3 --max-size 3 --desired-capacity 3 --default-cooldown 5 --grace-period 5 --tag "k=Name, v=my-servers, p=true" --tag "k=enabled, v=true, p=true"
This will create 3 instances base on the AMI, one in each zone
You can check on the progress of the creation of the Auto Scaling group using this command
as-describe-scaling-activities --auto-scaling-group my-auto-scaling-group --region eu-west-1
You can find more useful commands for things like deleting or updating Auto Scaling Group in my blog post:
How to use Amazon’s Auto Scaling Groups
Or there is the Amazon Web Services getting started documentation

You also have a look into this blog,
http://geekospace.com/installing-aws-command-line-tools-from-amazon-downloads/

Auto Scaling is a tool that uses the results from Amazon CloudWatch to define the scaling policies on various instances.
Auto Scaling gives you power to decide the scaling, Schedule it and also define the resource to be scaled. These configurations are stored under an Auto Scaling Group and can be used to track applications working over various instances.

Related

ec2 instances and AWS auto scaling group

I use the module, https://github.com/terraform-aws-modules/terraform-aws-ec2-instance to provision the ec2 instances on AWS. I would like to have AWS auto scaling group feature implemented, i.e. in case one of the ec2 instances is not healthy, AWS auto replaces it. But, I do not see the code, resource "aws_autoscaling_group" inside the above ec2 instance module.
Question:
Is it reasonable to implement AWS auto scaling group with the ec2 module? I understand we can implement AWS auto scaling group with eks. But, I am not sure about ec2 instances.
If it is possible, how to do it?
Yes, it is not only reasonable, but actually considered a best practice to use an auto scaling group for EC2 instances.
you can do so using the terraform autoscaling module: https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws/latest

How to create ECS cluster via CLI with custom AMI image?

I'm aware that there are similar question posted here, but none of them address this specific issue.
How to create an ECS cluster via CLI tools, aws-cli and/or ecs-cli, with using your own custom AMI stored in EC2 as an AMI image?
Is this even possible currently or do I need to turn to CloudFormation?
EDIT: I had a major confusion about the service architecture of ECS in terms of distributing containers on container instances (EC2 instances registered to the cluster).
So the container instance runs on an AMI. Which can be a custom built AMI, but such would needed only in some special circumstances. It's recommended to use an ECS-optimized Amazon Linux AMI by as maintained by AWS and is updated about once a month.
Another confusion point was with Docker Compose approach and the ECS CLI tool. Some experts think that while the ECS CLI is released and maintained by AWS, and being already 3 years old at this point in 2019, it is not suitable for production usage. So am using AWS CLI instead, and dealing with clusters, task definitions, and (task scheduling) services directly.
I would rather run the custom stuff within the container defined by dockerfile on ECS optimized AMI
But to run custom AMI
1) Pre-requisite is to have AMI image with ECS Agent installed
2) Then I would try ecs-cli up command with defined --image-id
ecs-cli up
--keypair <my-keypair>
--launch-type EC2
--size 2
--instance-type t2.micro
--instance-role ECSMediumRole
--vpc <my-vpc>
--image-id ami-XXXXXXXX # Custom Image ID with ECS Agent installed
--cluster-config <my-cluster-config>
--ecs-profile <my-ecs-profile>
--security-group <my-security-group-id>
--region us-east-1
--subnets <my-public-vpc-subnet>

AWS ECS: Will Service Auto Scaling create EC2 instance automately?

I am confused about how Service AutoScaling automately works. Will it create EC2 instance automately?
I create it and add it to a Cluster's service, but it does no create EC2 for placing my required number of tasks. Is any thing wrong with my settings? I check the [Events] and see "service s2 was unable to place a task because no container instance met all of its requirements. ", but shouldn't it create a EC2 if no instance met? Please give me some advice, thanks in advance.
but shouldn't it create a EC2 if no instance met
Not really. There are two types of scaling policies: scaling policies on an ECS service and scaling policies on the ECS cluster. Instances are added based on cluster scaling policies, and that's what you should set up in addition to your service scaling policy.
AWS has a couple of detailed tutorials on scaling ECS clusters:
https://aws.amazon.com/blogs/compute/automatic-scaling-with-amazon-ecs/
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cloudwatch_alarm_autoscaling.html
AWS Elastic Container Services has two methods to deploy containers over aws environment
Where you no need to worry about orchestration of containers (task in aws)
Fargate (Available in few regions like N.Virginia)
Using EC2 in ECS
I guess you are using 2nd option to deploy application over ECS where you can provide details of scaling tasks/containers not ec2 instances.
For Auto-scaling of ec2 instances you should look into ASG of AWS.
As far as AWS ECS is concerned you need some building blocks which are as follows-
Cluster
Task definition (Memory, Network and Storage configs of tasks)
Service contains EC2 instance configuration
Auto scaling policies if you want to auto-scale tasks

How can I connect my autoscaling group to my ecs cluster?

In all tutorials for ECS you need to create a cluster and after that an autoscaling group, that will spawn instances. Somehow in all these tutorials the instances magically show up in the cluster, but noone gives a hint what's connecting the autoscaling group and the cluster.
my autoscaling group spawns instances as expected, but they just dont show up on my ecs cluster, who holds my docker definitions.
Where is the connection I'm missing?
I was struggling with this for a while. The key to getting the instances in the autoscaling group associated with your ECS cluster is in the user data. When you are creating your launch config when you get to step 3 "Configure Details" hit the advanced tab and enter a simple bash script like the following for your user data.
#!/usr/bin/env bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
All the available parameters for agent configuration can be found here http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html
An autoscaling group is not strictly associated to a cluster. However, an autoscaling group can be configured such that each instance launched registers itself into a particular cluster.
Registering an instance into a cluster is the responsibility of the ECS Agent running on the instance. If you're using the Amazon ECS-optimized AMI, the ECS Agent will launch when the instance boots and register itself into the configured cluster. However, you can also use the ECS Agent on other Linux AMIs by following the installation instructions.
Well, i found out.
Its all about the ecs-agent and its config file /etc/ecs/ecs.config
(This file will be created through the Userdata field, when creating EC2 instances, even from an autoscaling configuration.)
Read about its configuration options here: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html
But you can even copy a ecs.config stored on Amazon S3, do it like this (following lines go into Userdata field):
#!/bin/bash
yum install -y aws-cli
aws configure set default.s3.signature_version s3v4
aws configure set default.s3.addressing_style path
aws configure set default.region eu-central-1
aws s3 cp s3://<bucketname>/ecs.config /etc/ecs/ecs.config
note: Signature_version v4 is specific for some regions, like eu-central-1.
This ofc only works, if your IAM role for the instance (in my case its ecsInstanceRole) has the right AmazonS3ReadOnlyAccess
The AWS GUI console way for that would be:
Use the cluster wizard at https://console.aws.amazon.com/ecs/home#/firstRun .
It will create an autoscaling grou for your cluster, a loadbalancer in front of it, and connect it all nicely.
This question is old but the answer is not complete. There are 2 parts to getting your own auto-scaling group to show up in your cluster (as of Jan 2022).
You need to ensure your cluster name is set for ECS_CLUSTER variable in /etc/ecs/ecs.config as mentioned in this answer: https://stackoverflow.com/a/35324937/583875
You need to create a new capacity provider for the cluster and attach this auto scaling group. To do this, go to Cluster -> Capacity Provider -> Create -> Select your auto scaling group under Auto Scaling group.
Another tricky part is getting your service to use the instances (if you have a service running). You need to edit the Service, and change the Capacity provider strategy. Click on Add another provider and choose the new capacity provider you created in (2) above.
That's all! To ensure things are working properly: you should see your capacity provider under Graph -> Capacity Providers and you should see instances from your auto scaling group under Graph -> ECS Instances.

Cloud Formation in auto scaling using aws sdk command line

I am using aws sdk for auto scaling command line tool. I want to know that how to use cloud formation with this command line tool? i.e. can we give template id instead of instance/ ami id?
Example:
as-create-launch-config MyLC --image-id <CF tamplete id> --instance-type m1.small
can we give template id instead of instance/ ami id?
No, using Auto Scaling with AWS CloudFormation currently works the other way round only:
AWS CloudFormation fully supports all Auto Scaling resources like AWS::AutoScaling::AutoScalingGroup, AWS::AutoScaling::LaunchConfiguration etc., see the AWS Resource Types Reference for details on all available resources; there are a couple of Auto Scaling related AWS CloudFormation Sample Templates to get you started as well:
AutoScalingKeepAtNSample.template - An example of using Auto Scaling groups to manage a set of EC2 instances.
AutoScalingMultiAZSample.template - An example of using Load Balanced, Auto Scaling groups spanning multiple EC2 availability zones. This template has been updated to use Auto Scaling Policies.
AutoScalingMultiAZWithNotifications.template - An example of using Load Balanced Auto Scaling groups spanning multiple EC2 availability zones. This template uses Auto Scaling Policies and configures Auto Scaling notifications so that you can get emails when scaling events occur.
You can then use the AWS CloudFormation Command Line Tools to use your template(s) with said Auto Scaling resources, e.g. via cfn-create-stack, cfn-update-stack etc., see the Command Line Tools Reference for details on all available commands.

Resources