Sunday, July 19, 2015

Auto sacalling

Auto Scaling

AWS resources, such as EC2 instances, are housed in highly-available data centers. To provide additional scalability and reliability, these data centers are in different physical locations. Regions are large and widely dispersed geographic locations. Each region contains multiple distinct locations, called Availability Zones, that are engineered to be isolated from failures in other Availability Zones and provide inexpensive, low-latency network connectivity to other Availability Zones in the same region. Auto Scaling enables you to take advantage of the safety and reliability of geographic redundancy by spanning Auto Scaling groups across multiple Availability Zones within a region. When one Availability Zone becomes unhealthy or unavailable, Auto Scaling launches new instances in an unaffected Availability Zone. When the unhealthy Availability Zone returns to a healthy state, Auto Scaling automatically redistributes the application instances evenly across all of the designated Availability Zones.
You can add new instances to the application only when necessary, and terminate them when they're no longer needed. And because Auto Scaling uses EC2 instances, you only have to pay for the instances you use, when you use them. You now have a cost-effective architecture that provides the best customer experience while minimizing expenses.
An Auto Scaling group can contain EC2 instances in one or more Availability Zones within the same region. However, Auto Scaling groups cannot span multiple regions.

Web App Architecture

In a common web app scenario, you run multiple copies of your app simultaneously to cover the volume of your customer traffic. These multiple copies of your application are hosted on identical EC2 instances (cloud servers), each handling customer requests.



You can create as many Auto Scaling groups as you need. For example, you can create an Auto Scaling group for each tier.Auto Scaling manages the launch and termination of these EC2 instances on your behalf. You define a set of criteria (such as an Amazon CloudWatch alarm) that determines when the Auto Scaling group launches or terminates EC2 instances. Adding Auto Scaling groups to your network architecture can help you make your application more highly available and fault tolerant.



To distribute traffic between the instances in your Auto Scaling groups, you can introduce a load balancer into your architecture.

Instance Distribution
Auto Scaling attempts to distribute instances evenly between the Availability Zones that are enabled for your Auto Scaling group. Auto Scaling does this by attempting to launch new instances in the Availability Zone with the fewest instances. If the attempt fails, however, Auto Scaling attempts to launch the instances in another Availability Zone until it succeeds. For each instance that Auto Scaling launches in a VPC, it selects a subnet from the Availability Zone at random.





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.
  • Better availability. You can 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 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.

Using DYNAMO DB LOCAL to Node project


To install DynamoDB locally on Windows 7


DynamoDB Local supports the Java Runtime Engine (JRE) version 6.x or newer;
it will not run on older JRE versions.You can download using following link.

http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html

Go to following link to download dynamo db jar file:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html


Once you have downloaded the archive to your computer, extract the contents and
copy the extracted directory to C:\ drive.


Open a command prompt window, navigate to the downloaded directory where you will find DynamoDBLocal.jar, and enter the following command:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

or

java -Djava.library.path=. -jar DynamoDBLocal.jar


Now we’re ready to use our local DynamoDB 

2013-09-29 12:11:38.530:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT<br/>
2013-09-29 12:11:38.618:INFO:oejs.AbstractConnector:Started <br/>
SelectChannelConnector@0.0.0.0:8000

JavaScript Shell for DynamoDB Local

The JavaScript Shell for DynamoDB Local can help jump-start your usage of DynamoDB, all within an interactive, hands-on environment. The JavaScript Shell is bundled with DynamoDB Local, and provides an easy-to-use environment for prototyping and application development.

  • Its supports latest version of DynamoDB Local, to run it on your computer while you up and running the Dynamo DB Local server open a web browser on your computer and go to the following URL:http://localhost:8000/shell






Install asw-sdk for node

The preferred way to install the AWS SDK for Node.js is to use the npm package manager for Node.js. Simply open the cmd window inside project folder and type the following 

npm install aws-sdk

You can also use Bower to install the SDK by typing the following into a terminal window

bower install aws-sdk-js


Create a js file and define configuration

  var AWS = require('aws-sdk');

For work with Local DynamoDB define endpoint will be "http://localhost:8000"

var databaseConfig = {"endpoint": new AWS.Endpoint("http://localhost:8000")};
var dynamoDB = new AWS.DynamoDB(databaseConfig);

To Connect with Local DynamoDB, we need provide dummy entries for required fields(accessKey, Secret and Region).

var dynamoDBConfiguration = {
    "accessKeyId": "xxxxxxxxxxxxx",
    "secretAccessKey": "xxxxxxxxxxxxxxxxx",
    "region": "eu-west-1"
  };

Later on to Connect with AWS DynamoDB Service place actual accessKey, Secret and Region.

provide your configurations.

AWS.config.update(dynamoDBConfiguration);

Now you can start use it locally.