The ability to scale horizontally is very important in building Cloud Native applications. In this studio, you will be extending your Airwaze App to scale horizontally as traffic on the server increases.
You can do this studio in any US Region that has not reached it’s VPC limit. N.Virginia and Oregon have the highest VPC limit.
IF you are in a new Region, you will need to create a new KeyPair.
yourname-region-name-key
~/.ssh
folder$chmod 400 yourname-useast-key.pem
We will be using the AWS CLI tool for some parts of the studio. The AWS CLI tool allows you to create and change infrastructure on the cloud via the command line.
To install the AWS CLI tool run the following commands in your terminal:
(on local computer)
$ brew install awscli
$ aws --version
Create AWS CLI credentials
Screenshot of IAM credentails
Screenshot of Create Access Key
Note
It is very important that you keep the AWS Secret Access Key
private. Keep it secret, Keep it safe!
Access to that key allows anyone to programmatically create infrastructure(servers, rds, etc) on the AWS account.
Next run the below command and configure your the AWS CLI tool. Use the “Default region name” or us-east-1
:
(on local computer)
$ aws configure
AWS Access Key ID [None]: AK-------------------
AWS Secret Access Key [None]: r4------------------
Default region name [None]: (just hit enter)
Default output format [None]: (just hit enter)
You should now be able to run commands against AWS. For example, you should now be able to list all of the buckets in S3::
$ aws s3 ls
Take a look around by looking at the help pages for a couple of commands::
$ aws help
$ aws s3 help
$ aws s3 sync help
The aws help
command is a quick alternative to looking up information about the tool on line.
You are going to use Amazon CloudFormation to create your VPC. CloudFormation can create infrastructure on AWS based on a JSON template. CloudFormation allows you to create consistent, reproducible AWS environments.
You’ll be using a CloudFormation template that adds:
Finally AWS CloudFormation will pull the template from an S3 bucket.
aws-cli
tool (command shown below)airwaze_cloudformation.json
in your favorite editor$ mkdir ~/s3-sync/cloud
$ aws s3 sync s3://launchcode-gisdevops-cloudformation ~/s3-sync/cloud
$ cd ~/s3-sync/cloud
(then open the airwaze_cloudformation.json file)
Screenshot of CloudFormation Screen 1
Next we need to give your stack a name and pass along a few parameters to customize the VPC.
Screenshot of Stack parameters
It will take CloudFormations about 15 minutes to create and run your VPC. The “Events” tab will give you continuous updates on the progress of the job. Be sure to note the name and VPC ID of the VPC that is created.
Since you will be scaling machines horizontally, you WON’T be able to manually scp
a jar to each machine. Instead, the machines will reach out and grab a copy of the jar when they start. The servers will download a copy of your application jar from S3.
First create a new bucket in S3. Remember EVERY bucket name for S3 in the whole wide world has to be unique. Use the pattern below to get a unique name.:
$ aws s3 mb s3://launchcode-gisdevops-c1-yourname/
Run aws s3 ls
to make sure that the bucket was created properly.
Locate a .jar
for Airwaze that you deployed for Day 2 Studio. Rename it to app.jar
and upload the jar to S3 using the following command::
$ aws s3 cp build/libs/app.jar s3://launchcode-gisdevops-c1-yourname/
$ aws s3 ls s3://launchcode-gisdevops-c1-yourname/ # check to make sure it uploaded
You are going to create an EC2 do some initial database setup. This EC2 will not be used for anything else. Please name it your-name-day3-db-setup
{yourname}-airwaze-SubnetWebAppPublic
as the subnet(on remote server)
ubuntu$ sudo apt-get update
ubuntu$ sudo apt-get install postgresql
ubuntu$ psql -h airwaze-example.cew68jaqkoek.us-east-1.rds.amazonaws.com -p 5432 -U masterUser airwaze
(paste this sql into psql shell)
CREATE USER airwaze_user WITH PASSWORD 'verysecurepassword';
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION postgis_tiger_geocoder;
CREATE TABLE airport
(
id serial primary key,
airport_id integer,
airport_lat_long geometry,
altitude integer,
city character varying(255),
country character varying(255),
faa_code character varying(255),
icao character varying(255),
name character varying(255),
time_zone character varying(255)
);
CREATE TABLE route
(
id serial primary key,
airline character varying(255),
airline_id integer,
dst character varying(255),
dst_id integer,
route_geom geometry,
src character varying(255),
src_id integer
);
ALTER TABLE airport OWNER to airwaze_user;
ALTER TABLE route OWNER to airwaze_user;
Also, send up the routes.csv
file and the Airports.csv
file and get those in the database.:
(on local computer)
$ scp -i ~/.ssh/mikes-keys.pem routes.csv [email protected]:/home/ubuntu
$ scp -i ~/.ssh/mikes-keys.pem Airports.csv [email protected]:/home/ubuntu
Then after the csv files have been copied to the server you can populate the database by running these commands.
(remote server)
ubuntu$ psql -h airwaze-example.cew68jaqkoek.us-east-1.rds.amazonaws.com -d airwaze -U airwaze_user -c "\copy route(src, src_id, dst, dst_id, airline, route_geom) from STDIN DELIMITER ',' CSV HEADER" < /home/ubuntu/routes.csv
ubuntu$ psql -h airwaze-example.cew68jaqkoek.us-east-1.rds.amazonaws.com -d airwaze -U airwaze_user -c "\copy airport(airport_id, name, city, country, faa_code, icao, altitude, time_zone, airport_lat_long) from STDIN DELIMITER ',' CSV HEADER" < /home/ubuntu/Airports.csv
You now have all of the pieces set up to begin Auto Scaling EC2 machines.
Screenshot of AutoScale Start
A LaunchConfiguration is essentially creating a template for all of the EC2 instances that will be created automatically via Auto Scale.
Screenshot of AutoScale Step 1
The Launch Configuration is going to be very similar to setting up a normal EC2 instance.
Screenshot of Auto Scale AMI
Screenshot of Auto Scale instance size
There are several important configurations that have to be made on the Configure Details screen.
The most important configuration is the User data in Advanced Details. The User data is the script that runs as the server starts up. This script creates the proper directories, configures systemd, and launches the app. Additionally, the app pulls down a copy of the jar file from S3.
There are two pieces of data to change in the User data script:
APP_DB_HOST
to the endpoint of your RDS database.aws s3 c s3://launchcode-gisdevops-c1-yourbucket/app.jar /opt/airwaze/app.jar
command to point to the bucket that you created earlier in the studio.airwaze-{your name}-config
.Assign a public IP address to every instance
.Screenshot of Auto Scale configuration
WebAppSecurityGroup
from your VPC. The key is that you want to have ports 22 and 8080 open on the machines that you are running.#!/bin/bash
# Install Java
apt-get update -y && apt-get install -y openjdk-8-jdk awscli
# Create airwaze user
useradd -M airwaze
mkdir /opt/airwaze
mkdir /etc/opt/airwaze
aws s3 cp s3://launchcode-gisdevops-c1-traineemike/app.jar /opt/airwaze/app.jar
chown -R airwaze:airwaze /opt/airwaze /etc/opt/airwaze
chmod 777 /opt/airwaze
# Write Airwaze config file
cat << EOF > /etc/opt/airwaze/airwaze.config
APP_DB_HOST=airwaze-example.cew68jaqkoek.us-east-1.rds.amazonaws.com
APP_DB_PORT=5432
APP_DB_NAME=airwaze
APP_DB_USER=airwaze_user
APP_DB_PASS=verysecurepassword
EOF
# Write systemd unit file
cat << EOF > /etc/systemd/system/airwaze.service
[Unit]
Description=Airwaze Studio
After=syslog.target
[Service]
User=airwaze
EnvironmentFile=/etc/opt/airwaze/airwaze.config
ExecStart=/usr/bin/java -jar /opt/airwaze/app.jar SuccessExitStatus=143
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl enable airwaze.service
systemctl start airwaze.service
Screenshot of Auto Scale security groups
The Auto Scale Group is the piece of configuration responsible for how and when new machines are spun up (and spun down). Spun up = created and started. Spun down = stopped and possibly deleted.
The first step is configuring where the machines will be spun up.
airwaze-{your name}
(replace {your name} of course…)SubnetWebAppPublic
.Screenshot of Auto Scale configuration
The next screen configures how an app scales up.
Use scaling policies to adjust the capacity of this group
.Scale Fast!
.Screenshot of Auto Scale configuration
This will create you Auto Scaling Group. At first, the summary page will say 0 instances; it typically takes a couple of minutes to initialize.
Screenshot of Auto Scaling Group Dash
The “Instances” tab will show you how many machines you currently have running in your Auto Scaling Group.
Screenshot of the Instances tab
Next you need to hook a load balancer up to your Auto Scaling Group.
Screenshot of Target Groups Edit
WebAppTargets
from the “Target Groups” drop down.Screenshot of Target Groups select target
Next, you want to test that your autoscaling is working properly.
Go to the public DNS of your ELB and hit refresh many times. You can even go to that address in multiple browsers at the same time. You are trying to send as many requests as possible to your ELB.
You can optionally use a Node library called loadtest. Loadtest measures the average latency time of a concurrent requests to a server.
Note
Note: Tools like loadtest and Apache AB are like guns. You don’t point them live things unless you want to kill it. It’s fine to point a load test tool at your non production apps on AWS; in fact you need to make sure that it can handle load. You would never want to point a load testing tool at a live site because: * It’s your live site (your staging environment should be similar enough to production to replicate the error). * Your production site will be sitting behind one or more layers like a CDN. Your load test is going to look a lot like a denial of service attack. Services like your CDN are designed to recognize and block attacks at the fringe of your network. Running a load test against a live site is a good way to get your IP address blocked.
To install loadtest install the following npm package globally (-g
):
$ sudo npm install -g loadtest
Next, you can run a command to put load on the server. The following command runs 3 requests per second sending 3 concurrent results to the server at a time.:
(this starts 3 threads and sends 3 requests a second, don't run this for very long!)
loadtest -c 3 --rps 3 http://internal-airwa-WebAp-1CT34V4AX36U0-774969334.us-east-1.elb.amazonaws.com