Search This Blog

Saturday, December 10, 2016

Part - 10 - AWS - Autoscaling

Autoscaling

o   Based on custom policies, schedules, or metric monitoring
§  Can use any metric CloudWatch measures
o   Scaling out ensures performance to handle demand
§  Adding instances
o   Scaling in minimizes cost when resources are no longer needed
§  Removing instances
Ø  Auto Scaling is enabled by Amazon CloudWatch
o   No additional fees other than CloudWatch fees
o   All instances launched using Auto Scaling will have detailed monitoring automatically enabled
Ø  Can configure Auto Scaling with web console, command-line tools, or APIs
o   This section will focus on the command-line tools
o   You can explore the web console in the exercise bonus

Why Use Auto Scaling?

Ø  Ensure that a minimum number of instances is always running
o   Auto Scaling will make sure that number of instances is always running
o   If an instance terminates for any reason, a new one will be started
o   Can also be used to keep instance count at a fixed size
§  Delete “extra” instances started by mistake
Ø  Automatically scale instances to meet demand
o   Can specify quantity of instances to launch or terminate as demand requires
§  For example, add or remove five instances at a time
o   Can specify a “cool-down” period to wait before taking action

Two Steps to Get Started With Auto Scaling

Ø  Create a launch configuration, specifying
o   A name for the launch configuration
o   The image (AMI) to use when creating instances
o   The instance type

# aws autoscaling create-launch-configuration --launch-configuration-name user01LC--image-id ami-1235564a --instance-type m1.small –-key-name Crs1205EC2Key--security-groups sg-7a91c610

Ø  Create an Auto Scaling group, specifying
o   A name for the group
o   The launch configuration to use
o   One or more subnets (separated by commas) to run the instances in
o   Minimum instance count
o   Maximum instance count

# aws autoscaling create-auto-scaling-group --auto-scaling-group-name user01ASGrp --launch-configuration-name user01LC --vpc-zone-identifier subnet-41767929c --min-size 2 --max-size 4

Ø  After creating an Auto Scaling group
o   The minimum number of instances will automatically be started
o   No other action is needed for this to happen
Ø  The Auto Scaling group ensures that the minimum number of instances are always running
o   New instances will automatically be started if necessary

Checks, Policies, and Actions

Ø  Health check
o   A check of the health of an instance within an Auto Scaling group
o   Uses standard EC2 health checks by default
o   Can also use custom Elastic Load Balancing (ELB) health checks
§  Will be seen shortly
o   If an instance is marked not healthy, it is immediately scheduled for replacement
§  By default, it is not allowed a chance to recover
Ø  Policy
o   Instructions on how to change the instance count size
o   Can specify the size as an absolute size, increment, decrement, or percentage of current size
Ø  Triggers
o   Combination of an Auto Scaling policy and CloudWatch alarm

Three Types of Scaling

Ø  Manual scaling
o   Change the Auto Scaling group instance counts

# aws autoscaling update-auto-scaling-group --auto-scaling-group-name user01ASGrp --min-size 4 --max-size 10

Ø  Scaling on a schedule
o   Useful when you have a predictable schedule of required demand
o   For example: Run additional servers between 10:00 a.m. and 4:00 p.m.
Ø  Scaling based on triggers and policies
o   Utilize CloudWatch alarm to execute a policy
o   Must define separate policies to scale up and scale down; e.g.:
§  Scale up when bandwidth exceeds a threshold
§  Scale down when average bandwidth falls back down
o   This is the truly “automatic” part of Auto Scaling

Creating a Policy

Ø  Create a new policy

#aws autoscaling put-scaling-policy –-policy-name <PolicyName> --adjustment-type <type> --auto-scaling-group-name <as-group> --scaling-adjustment <adjustment>

o   Type can be ExactCapacity, ChangeInCapacity, PercentChangeInCapacity
Ø  Examples:
o   Scale up by two instances

# aws autoscaling put-scaling-policy –-policy-name IncrBy2Pol --adjustment-type ChangeInCapacity --auto-scaling-group-name user01ASGrp --scaling-adjustment 2

o   Scale down by two instances

# aws autoscaling put-scaling-policy –-policy-name DecrBy2Pol --adjustment-type ChangeInCapacity --auto-scaling-group-name user01ASGrp --scaling-adjustment -2

o   Scale up by 50 percent

--adjustment-type PercentChangeInCapacity –-scaling-adjustment 50

Testing a Policy

Ø  Policies can be tested by executing them manually
o   Good idea to test before utilizing it from a trigger

# aws autoscaling execute-policy --auto-scaling-group-name user01ASGrp --policy-name team02IncrBy2Pol

Ø  Note: An Auto Scaling group always ensures that the running instance count adheres to the minimum and maximum sizes specified in the group
o   Executing a policy will not be allowed to violate this
o   For example, if a maximum size is eight, there will never be more than eight instances
§  Even if the policy says to add 10 instances

Creating a Trigger



Cool-Down Period

The Auto Scaling cooldown period is a configurable setting for your Auto Scaling group that helps to ensure that Auto Scaling doesn't launch or terminate additional instances before the previous scaling activity takes effect. After the Auto Scaling group dynamically scales using a simple scaling policy, Auto Scaling waits for the cooldown period to complete before resuming scaling activities. When you manually scale your Auto Scaling group, the default is not to wait for the cooldown period, but you can override the default and honor the cooldown period. Note that if an instance becomes unhealthy, Auto Scaling does not wait for the cooldown period to complete before replacing the unhealthy instance




Ø  A time period to suspended Auto Scaling
o   Goal is to prevent Auto Scaling from reacting too quickly
§  For example, before a previous scaling activity has finished
Ø  Can specify a cool-down period for an Auto Scaling group
o   Provide the following option to the create-auto-scaling-group command when creating the group:

--default-cooldown <seconds>
--default-cooldown <300s>

Ø  Can also specify a cool-down period for each policy
o   Overrides the group’s cool-down period for that policy
o   Provide the following option to the put-scaling-policy when creating a policy:

--default-cooldown <seconds>

Managing Auto Scaling Resources

Ø  All launch configurations defined can be displayed:

aws autoscaling describe-launch-configurations

Ø  All Auto Scaling groups defined can be displayed:

aws autoscaling describe-auto-scaling-groups

Ø  All policies defined can be displayed:

aws autoscaling describe-policies

Benefits of Auto Scaling

Adding Auto Scaling to your application architecture is one way to maximize the benefits of the AWS cloud. When you use Auto Scaling, your applications gain the following benefits:
·        Better fault tolerance. Auto Scaling can detect when an instance is unhealthy, terminate it, and launch an instance to replace it. You can also configure Auto Scaling to use multiple Availability Zones. If one Availability Zone becomes unavailable, Auto Scaling can launch instances in another one to compensate.
·        Better availability. Auto Scaling can help you ensure that your application always has the right amount of capacity to handle the current traffic demands.

·        Better cost management. Auto Scaling can dynamically increase and decrease capacity as needed. Because you pay for the EC2 instances you use, you save money by launching instances when they are actually needed and terminating them when they aren't needed.

Part - 9 - AWS - Lambda (Stateless computing)

Lambda (Stateless computing)








·        AWS Lambda service allows you to run code without having to worry about provisioning any underlying resources (such as virtual machines, databases etc.)
·        AWS Lambda is a server-less compute service that runs your code in response to events and automatically manages the underlying compute resources for you.
·        You can use AWS Lambda to extend other AWS services with custom logic, or create your own back-end services that operate at AWS scale, performance, and security.

What is Lambda?

AWS Lambda is a compute service where you can upload your code and create a Lambda function. AWS Lambda takes care of provisioning and managing the servers that you use to run the code. You don't have to worry about operating systems, patching, scaling, etc. You can use Lambda in the following ways:
·        As an event-drive compute service where AWS Lambda runs your code in response to events. These events could be changes to data in an Amazon S3 bucket or an Amazon DynamoDB table.
·        As a compute service to run your code in response to HTTP requests using Amazon API Gateway or API Calls made using AWS SDKs. This is what we use at A Cloud Guru.

What Actually Is It?

·       Data Centers
·        Hardware
·        Assembly Code/Protocols
·        High level Languages
·        Operating Systems
·        AWS API's
·        AWS Lambda
Lambda runs your code on high-availability compute infrastructure and performs all the administration of the compute resources, including server and operating systems maintenance, capacity provisioning and automatic scaling, code and security patch deployment, and code monitoring and logging.
All you need to do is supply the code.

What Events Trigger Lambda?

You can use AWS Lambda to respond to table updates in Amazon DyanmoDB, modifications to objects in Amazon S3 buckets, messages arriving in an Amazon Kinesis stream, AWS API call logs created by AWS CloudTrail, and customer events from mobile applications, web applications, or other web services.
·        Supported Programming Languages -  Javascript, Python, Node.js
·        AWS Lambda is designed to provide 99.99% availability for both the service itself and for the functions its operates.

Lambda Pricing:

·        Requests:
o   First 1 million requests are free
o   $0.20 per 1 million requests there after
·        Duration
o   Duration is calculated from the time your code begins executing until it returns or otherwise terminates, rounded up to the nearest 100ms. You allocate to your function. You are charges $0.00001667 for every GB-second used.
·        Free Tier
o   1M free requests per month and 400,000 GB-seconds of compute time per month. The memory size you choose for your Lambda functions determines how long they can run in the free tier. The Lambda free tier does not automatically expire at the end of your 12 months AWS Free Tier term, but is available to both existing and new AWS customer indefinitely. 



If you allocated 512MB of memory to your function, executed it 3 million times in one month, and it ran for 1 second each time, your charges would be calculated as follows:
·        The Monthly compute price is $0.00001667 per GB-s and the free tier provides 400,000 GB-s.
·        Total compute (seconds)=3M*(1s)-3,000,000 second
·        Total compute (GB-s)=3,000,000*512MB/1024=1,500,00 GB-s
·        Total Compute - Free tier compute = Monthly billable compute GB-s
·        1,500,000 GB-s - 400,000 free tier compute = Monthly billable compute GB-s
·        Monthly compute charges = 1,100,000 * $0.00001667 = $18.34 $18.34 

Why is Lambda Cool?

·        NO Servers!
·        Continuous Scaling

·        Super Super Super Che

Part - 8 - AWS - AWS Elastic BeanStalk

AWS Elastic BeanStalk







·        Platform to execute and manage applications
o   Extends AWS to Platform as a Service (PaaS)
·        Automatically manages many AWS resources to provide everything needed to execute an application
·        AWS service is specifically designed for developers to upload their code to and then it will automatically handle the provisioning of those resources that are required to host that code.
·        AWS Elastic Beanstalk makes it even easier for developers to quickly deploy and manage applications in the AWS cloud.
Ø  Applications are uploaded and execution details can be handled automatically by Elastic BeanStalk
o   Capacity provisioning
o   Load balancing
o   Auto scaling
o   Health monitoring
o   Etc.
Ø  Supports the following platforms
o   .NET on Windows
o   Java on Linux
o   PHP on Linux
o   Node.js on Linux
o   Python on Linux
o   Ruby on Linux
o   Docker
Ø  An Elastic Beanstalk platform is just a collection of standard AWS resources
o   EC2, Auto Scaling, load balancing, S3, SNS, CloudWatch, etc.
o   Provisioned automatically
o   There is no extra cost for this
§  Only pay for the resources used

Creating an Application for Beanstalk

Ø  No special requirement to host an application on Beanstalk
o   For example: supports virtually any Java web application
§  Package the application in a standard Java Web Archive (WAR) file
§  Existing JEE applications should be able to be deployed with no problem
·        Without even recompiling
o   Same is true for the other environments

Hosting an Application on Beanstalk

Ø  Steps
o   From the Elastic Beanstalk service, click Create New Application
o   Type a name and description; click Create
o   Select the environment tier, configuration, and environment type
o   Click Next



o   Select the application package to deploy
o   Set update deployment limits



o   Request a unique URL that will be used to access your application
§  Can be mapped to your own domain name later



o   Select an instance type (standard EC2 instance types)
o   Provide a key pair if you want to be able to SSH into the instances
o   Provide an e-mail address to be notified of any changes or issues
o   Several additional configuration details



·        Once deployed, the application environment can be administered
o   Can control many aspects of the environment



o   Change button under Configuration allows environment to be changed/updated

Controlling Environment Configuration

Ø  Configuration screen has several sections



Ø  The software configuration allows control over the framework
o   This example is for Java (Tomcat)



Uploading New Application Versions

Ø  Uploading new versions of an existing application is very easy



Updating the Environment

Ø  Beanstalk differs from other PaaS offerings
o   Provides full control over the underlying infrastructure
§  Can manually control all resources for maximum flexibility
§  For example, direct login access to instances
Ø  Once launched, AWS does not update the environment
Ø  Amazon will periodically update the provided environments
o   Must change to a new environment to take advantage of the updates



Deleting a Beanstalk Application

Ø  Perform the following steps to delete the application just deployed
o   From the AWS Management Console, select the Elastic Beanstalk service
o   Click the application you just deployed
o   Click the Actions button and select “Terminate this environment”
§  Read the information in the dialog

o   Click the Terminate button