Steps for using the Amazon Command Line Tools:
- Pre-requisites
- All of the steps below can be skipped if you use EZAutoScaling.
- Download the command line tools for Auto Scaling
- Download the command line tools for CloudWatch
- Set up Auto Scaling command line
- Set up CloudWatch command line
- Create new Launch Configuration
- Create new Auto Scaling Group
- Create per region
- Create CloudWatch monitor to increase servers
- Create CloudWatch monitor to decrease servers
- Set Desired Capacity after creation
- Monitoring your Auto Scaling Froups
- Monitoring your Instances' health
- Delete Auto Scaling Groups and Launch Configurations
Pre-requisites
- You need to know which Amazon region (e.g. 'US East (N. Virginia)' or 'us-east-1') you want to set up your Auto Scaling in.
- You need to have an AMI set up in that region. The AMI will be the image that will be used for each new instance in your Auto Scaling group.
- You need to have a load balancer created in that region. Each new instance in the Auto Scaling group will be added to that load balancer.
The region where your EC2 instances will be created can be viewed in the top-right corner of your AWS Management Console.
Having an AMI
Under the Services tab select EC2 and then click on AMIs in the left panel. In the right panel you need to have an AMI. If you don't have one then you need to create one. Every new Auto Scaling EC2 instance that is created will be an exact copy of this AMI.
Having a load balancer
Similarly to AMIs, the left panel has a link for Load Balancers. You need to create at least 1 load balancer. Every new Auto Scaling EC2 instance will be added to this load balancer. When you first create the load balancer, you don't need to attach any instances to it.
FYI: All of the steps below are converted to a web-based interface in EZAutoScaling's online tool. If you use EZAutoScaling you will not need to execute any of the steps below.
Download the command line tools for Auto Scaling
This is probably one of the easier steps in this guide. Click on this link and download the command line tools for Auto Scaling.
Download the command line tools for CloudWatch
Similarly to the step above, click on this link and download the command line tools for CloudWatch.
Set up Auto Scaling command line
After you unzip the command line tools to a directory, go that that directory. Setting up the command line is described in the file README.TXT.
There are quite a few pre-requisites to make this work, but the assumption for this guide is that you have the JAVA pre-requisites; you just need to set up the security keys to get the command line to work.
Here are the steps as per the README file:
1. Ensure that JAVA version 1.5 or higher is installed on your system: (java -version) [Assumption is that this is set up]
2. Unzip the deployment zip file [Assumption is that this is done]
3. Set the following environment variables:
3.1 AWS_AUTO_SCALING_HOME - The directory where the deployment files were copied to.
- For Windows users to go: Start > right-click on My Computer > Properties
- Click on Change Settings
- Click on the Advanced tab and then Environment Variables
- Add a new system variable with name "AWS_AUTO_SCALING_HOME" and the value where you unzipped the directory
4. Add "%AWS_AUTO_SCALING_HOME%\bin" to your path (Windows)
- Open the variable "Path" and add "%AWS_AUTO_SCALING_HOME%\bin" at the end of the value. Make sure you add ";" between values.
Configuration of security keys; this guide will demonstrate how to set up the AWS keys with a credentials file:
1. Create your credentials file
- Copy and paste the file "credential-file-path.template" that is in the unzipped directory.
- Rename it to "credentials-yourname.template"
- Open the copied file and write in it: AWSAccessKeyId=youaccesskeySecretKey=yoursecuritykey. Note that there are no spaces.
- These two keys can be found in your AWS Management Console...
- Click on your username in the top-right corner and then click on My Account
- In the left navigation select "Security Credentials"
- You will find the AWS Access Key and Secret Key on this page.
- Similarly to the steps above, add a new system environment variable with name "AWS_CREDENTIAL_FILE" and the value should be the path to the credentials file you created.
3. Finally, to check you have set up everything properly:
- Open command prompt and go to the directory where you unzipped the Auto Scaling tools.
- Go to the bin directory
- Type as-describe-auto-scaling-groups --headers and you should see a result be returned that is not an error.
Set up CloudWatch command line
Setting this up is extremely similar to setting up Auto Scaling.
After you unzip the command line tools to a directory, go that that directory. Setting up the command line is described in the file README.TXT.
There are quite a few pre-requisites to make this work, but the assumption for this guide is that you have the JAVA pre-requisites; you just need to set up the security keys to get the command line to work.
Here are the steps as per the README file:
1. Ensure that JAVA version 1.5 or higher is installed on your system: (java -version) [Assumption is that this is set up]
2. Unzip the deployment zip file [Assumption is that this is done]
3. Set the following environment variables:
3.1 AWS_CLOUDWATCH_HOME - The directory where the deployment files were copied to.
- For Windows users to go: Start > right-click on My Computer > Properties
- Click on Change Settings
- Click on the Advanced tab and then Environment Variables
- Add a new system variable with name "AWS_CLOUDWATCH_HOME" and the value where you unzipped the directory (see screenshots from Auto Scaling steps).
4. Add "%AWS_CLOUDWATCH_HOME%\bin" to your path (Windows)
- Open the variable "Path" and add "%AWS_CLOUDWATCH_HOME%\bin" at the end of the value. Make sure you add ";" between values.
Configuration of security keys; this guide will demonstrate how to set up the AWS keys with a credentials file:
1. Create your credentials file
- Copy and paste the file "credential-file-path.template" that is in the unzipped directory.
- Rename it to "credentials-yourname.template"
- Open the copied file and write in it: AWSAccessKeyId=youaccesskeySecretKey=yoursecuritykey. Note that there are no spaces.
- These two keys can be found in your AWS Management Console...
- Click on your username in the top-right corner and then click on My Account
- In the left navigation select "Security Credentials"
- You will find the AWS Access Key and Secret Key on this page.
2. Set up the environment variable to the credentials file:
- If you set up the Auto Scaling tools then you have already done this step.... Add a new system environment variable with name "AWS_CREDENTIAL_FILE" and the value should be the path to the credentials file you created (see screenshots from Auto Scaling steps).
3. Finally, to check you have set up everything properly:
- Open command prompt and go to the directory where you unzipped the CloudWatch tools.
- Go to the bin directory
- Type mon-list-metrics --headers and you should see a result be returned that is not an error.
Create new Launch Configuration
The first step in creating an Auto Scaling group is to create a launch configuration for each new instance in the Auto Scaling Group.
To create a launch configuration:
- Go to the Auto Scaling Command Line Tools BIN directory.
- Use the command:as-create-launch-config Launch Config Name --image-id AMI ID --instance-type Instance Size --key Security Key
- Launch Config Name: This is the name that you want to give to the launch configuration.
- AMI ID: This is the ID of the existing AMI (which was a pre-requisite).
- Instance Size: The size of each new instance, for example t1.micro. Sizes are listed here (see the API names).
- Security Key: The security key that should be applied to each new instance.
- Example: as-create-launch-config lc1 --image-id ami-12345678 --instance-type c1.xlarge --key securitykey1
The command can get much more detailed than the above, however as a start the above is sufficient.
Create new Auto Scaling Group
The auto scaling group says which launch configuration to use, what region, and which load balancer.
To create an auto scaling group:
- Go to the Auto Scaling Command Line Tools BIN directory.
- Use the command:as-create-auto-scaling-group Auto Scaling Group Name --launch-configuration Launch Configuration Name --availability-zones Availability Zones --min-size Minimum size of group --max-size Maximum size of group --desired-capacity Starting desired capacity --load-balancers Load Balancers to add each new instance to
- Auto Scaling Group Name: Name of the new auto scaling group.
- Launch Configuration Name: Name of the launch configuration created previously.
- Availability Zones: The availability zones of the new EC2 instances that will be created. The zones are here. The zones you select for your new EC2 instances should be allowed by the load balancer that will be used.
- Minimum size of group: The minimum size of this group. If triggers get fired to reduce the size of the group, it will never go below this number.
- Maximum size of group: The maximum size of this group. If triggers get fired to increase the size of the group, it will never go above this number.
- Starting desired capacity: The number of EC2 instances to be started immediately.
- Load Balancers to add each new instance to: The load balancers to which each new EC2 instance should be added.
- Example: as-create-auto-scaling-group as-group1 --launch-configuration lc1 --availability-zones us-east-1a --min-size 1 --max-size 50 --desired-capacity 1 --load-balancers lb1
The command can get much more detailed than the above, however as a start the above is sufficient. After the above command runs, wait a few seconds and you should see new EC2 instances being created in your AWS Management Console. In the screenshot above the desired starting capacity was 1, therefore 1 new EC2 instance should start getting created.
Also make sure when you set this up that you test it - you need to make sure that each EC2 instance that is added to the Load Balancer is added and becomes "InService" in the load balancer. If it is "OutOfService" in the load balancer then the Health Check that the load balancer does is failing. This means that no requests will be routed to that EC2 instance.
Create per region
Auto Scaling is region specific. Below is a screenshot of regions in which you can create EC2 instances, and consequently independent Auto Scaling Groups:
Therefore, if you want to have EC2 instances in 2 different regions, you need to create an Auto Scaling Group for each region (i.e. 2 Auto Scaling Groups).
To create Auto Scaling groups in different regions you need to add an extra parameter to the commands that you write. Every single command needs to have --region Region API Name, for example --region us-west-1.
The Launch Configuration creation command would be:
as-create-launch-config lc1 --image-id ami-12345678 --instance-type c1.xlarge --key securitykey1 --region us-west-1
The Auto Scaling Group creation command would be:
as-create-auto-scaling-group as-group1 --launch-configuration lc1 --availability-zones us-east-1a --min-size 1 --max-size 50 --desired-capacity 1 --load-balancers lb1 LoadBalancer --region us-west-1
Create CloudWatch monitor to increase servers
The next important steps is to add a CloudWatch monitor so that if something occurs (such as CPU usage is too high on an EC2 instance) then a new EC2 instance will spawn in an Auto Scaling Group automatically.
Thanks to CardinalPath for the easy examples.
The steps are:
- Go to the CloudWatch Command Line Tools BIN directory.
- Create a policy:as-put-scaling-policy --auto-scaling-group Auto Scaling Group Name --name Policy Name --adjustment Number of Servers --type ChangeInCapacity --cooldown 600
- This command will increase the auto scaling group by "Number of Servers" and will not run this policy more than once in 600 seconds.
- Auto Scaling Group Name: The name of the auto scaling group that will have another instance added.
- Policy Name: The name of this new policy that you are creating.
- Number of Servers: The number of servers that should be spawned when this policy executes.
- The result of the above command will be an ID that looks like this: arn:aws:autoscaling:us-east-1:123123123123:scalingPolicy:aaa11aa2-11aa-1a11-1115-a111a111a111:autoScalingGroupName/AutoScaling:policyName/policy. Remember this.
- Create the alarm that should trigger the policy:mon-put-metric-alarm "Policy Name" --comparison-operator "GreaterThanThreshold" --evaluation-periods 3 --metric-name CPUUtilization --namespace AWS/EC2 --period 60 --statistic Average --threshold 70 --alarm-actions Policy Result --dimensions "InstanceId=Instance ID to monitor"
- This command will create a monitor which when "alarmed" will trigger the policy specified. The parameters say the this should monitor the Average CPU Utilization of the EC2 instance for every 60 seconds. If in 3 such measurements it is greater than 70% then trigger the policy specified in the previous command.
- Policy Name: The name of policy that you created with the previous command.
- Policy Result: The ID that resulted from the previous command.
- Instance ID to monitor: The command needs the ID of the EC2 instance for which it will monitor the CPU. This is usually the first instance in the auto scaling group.
Create CloudWatch monitor to decrease servers
In the previous step you set up EC2 instances to automatically get created. This step sets up CloudWatch monitors to terminate EC2 instances in an Auto Scaling group.
Thanks to CardinalPath for the easy examples.
The steps are:
- Go to the CloudWatch Command Line Tools BIN directory.
- Create a policy:as-put-scaling-policy --auto-scaling-group Auto Scaling Group Name --name Policy Name "--adjustment=-1" --type ChangeInCapacity --cooldown 600
- This command will decrease the auto scaling group by -1 and will not run this policy more than once in 600 seconds.
- Auto Scaling Group Name: The name of the auto scaling group that will have an instance removed.
- Policy Name: The name of this new policy that you are creating.
- The result of the above command will be an ID that looks like this: arn:aws:autoscaling:us-east-1:123123123123:scalingPolicy:aaa11aa2-11aa-1a11-1115-a111a111a111:autoScalingGroupName/AutoScaling:policyName/policy. Remember this.
- Create the alarm that should trigger the policy:mon-put-metric-alarm "Policy Name" --comparison-operator "LessThanThreshold" --evaluation-periods 3 --metric-name CPUUtilization --namespace AWS/EC2 --period 60 --statistic Average --threshold 20 --alarm-actions Policy Result --dimensions "InstanceId=Instance ID to monitor"
- This command will create a monitor which when "alarmed" will trigger the policy specified. The parameters say the this should monitor the Average CPU Utilization of the EC2 instance for every 60 seconds. If in 3 such measurements it is less than 20% then trigger the policy specified in the previous command.
- Policy Name: The name of policy that you created with the previous command.
- Policy Result: The ID that resulted from the previous command.
- Instance ID to monitor: The command needs the ID of the EC2 instance for which it will monitor the CPU. This is usually the first instance in the auto scaling group.
Set Desired Capacity after creation
There are many commands in the Auto Scaling command line tool package that allow you to edit an Auto Scaling Group. One most common usage is editing the number of EC2 instances that should be live currently. This is called "setting the desired capacity". To change the desired capacity do the following:
- Go to the Auto Scaling Command Line Tools BIN directory.
- Use the command:as-set-desired-capacity Auto Scaling Group Name --desired-capacity Number of EC2 servers currently desired
- Auto Scaling Group Name: The name of the auto scaling group that you wish to edit.
- Number of EC2 servers currently desired: A number specifying the number of EC2 servers you would like to be currently live
- Example: as-set-desired-capacity as-group1 --desired-capacity 2
You should see the changes take effect a few seconds later. With the above command my auto scaling group had 1 instance and the command just added a 2nd.
Monitoring your Auto Scaling Groups
There are a few ways to see how the Auto Scaling groups are doing in terms of health and status. The easiest is the following command:
- Go to the Auto Scaling Command Line Tools BIN directory.
- Use the command:as-describe-auto-scaling-groups --headers
Monitoring your Instances' health
There is no command line method to map Auto Scaling Groups and their instances to health information (such as CPU%) about the instances. You will need to remember or find out which EC2 instances make up your Auto Scaling Group and then check the health either through the AWS Management Console or using CloudWatch Command Line tools for those particular instances.
For example, you will need to find each EC2 instance that makes up your Auto Scaling Group and then use AWS Management Console to check up on its health as in the screenshot below:
On a side note, this is an area where EZAutoScaling is useful because it adds this mapping of EC2 Instances to Auto Scaling Groups and provides EC2 health information on one screen.
Delete Auto Scaling Groups and Launch Configurations
Deleting an Auto Scaling Group means first deleting the group (which terminates the EC2 instances of that group) and then deleting the Launch Configuration. If you have CloudWatch monitors attached, you can delete those separately through the AWS Management Console - they will simply stop monitoring and alerting. The steps to delete are:
- Go to the Auto Scaling Command Line Tools BIN directory.
- Use the command:as-delete-auto-scaling-group Name of Auto Scaling Group --force-delete
- This deletes the group and forces the termination of EC2 instances.
- Use the command:as-delete-launch-config Launch Configuration Name –force
You should soon see the EC2 instances being terminated.
Summary
Let's quickly summarize the steps and commands needed to auto scale:- Make sure you have pre-requisites ready
- Install and setup Auto Scaling Command Line Tools
- Install and setup CloudWatch Command Line Tools
- Create new launch configuration:
- as-create-launch-config Launch Config Name --image-id AMI ID --instance-type Instance Size --key Security Key
- Create new auto scaling group:
- as-create-auto-scaling-group Auto Scaling Group Name --launch-configuration Launch Configuration Name --availability-zones Availability Zones --min-size Minimum size of group --max-size Maximum size of group --desired-capacity Starting desired capacity --load-balancers Load Balancers to add each new instance to
- Create new CloudWatch monitor to scale up:
- as-put-scaling-policy --auto-scaling-group Auto Scaling Group Name --name Policy Name --adjustment Number of Servers --type ChangeInCapacity --cooldown 600
- mon-put-metric-alarm "Policy Name" --comparison-operator "GreaterThanThreshold" --evaluation-periods 3 --metric-name CPUUtilization --namespace AWS/EC2 --period 60 --statistic Average --threshold 70 --alarm-actions Policy Result --dimensions "InstanceId=Instance ID to monitor"
- Create new CloudWatch monitor to scale down:
- as-put-scaling-policy --auto-scaling-group Auto Scaling Group Name --name Policy Name "--adjustment=-1" --type ChangeInCapacity --cooldown 600
- mon-put-metric-alarm "Policy Name" --comparison-operator "LessThanThreshold" --evaluation-periods 3 --metric-name CPUUtilization --namespace AWS/EC2 --period 60 --statistic Average --threshold 20 --alarm-actions Policy Result --dimensions "InstanceId=Instance ID to monitor"
- Change desired capacity:
- as-set-desired-capacity Auto Scaling Group Name --desired-capacity Number of EC2 servers currently desired
- Delete auto scaling group:
- as-delete-auto-scaling-group Name of Auto Scaling Group --force-delete
- as-delete-launch-config Launch Configuration Name –force
Or, don't bother with all of these commands and instead sign up for EZAutoScaling.
Resource: https://www.ezautoscaling.com/AutoScalingGuide.aspx
No comments:
Post a Comment