Date post: | 26-Dec-2015 |
Category: |
Documents |
Upload: | rafe-hudson |
View: | 216 times |
Download: | 1 times |
Amazon AWS – brief intro
By “PJ” (JP on meetup.com)
iOS and PHP developer, and occasional lawyer
Contact me via: [email protected]
AWS - Objectives
•AWS is a set of products on the cloud that helps 3rd party business' host their web services without having to buy their own infrastructure (i.e. web servers etc)
What I will cover:
•S3, EC2, Route 53, RDS, ELB, Elastic BeanstalkHow all the products fit together
•Secret Bonus Topic•The AWS documentation is REALLY REALLY BAD•Register and you can get basic use for free (1 year)
AWS
S3 – Simple Static Storage
Online hard drive for files (any files)
Your files can be private or public
Great for storing static content– Javascript, html, css, images
•In fact it is the industry standard to store static content in a separate server
Static Server for meetup.com
Very real possibility that meetupstatic.com points to S3 address
S3 – Create a bucketYou must create a BUCKET (like a drive) to store your files
Store Files in Bucket
• All files are identified by its BUCKET and KEY• The “Key” is the folders + filename + file extension• All “Folders” are actually illusions.
• eg.• BUCKET = “samplebucket123321”• KEY = “favicon.ico”
S3 – Accessing files using URL
All files in S3 can be accessed using its individual URL.
If you make the files “public” or make the “folder” public, you can use those URL's in your website.
S3 – accessing files using URL
You can also make the bucket into “static website hosting” mode.
This link is optimised for static delivery.
The previous link (https) is your only choice if your website is using a https connection
S3 – accessing files using PHP SDK
S3 - Summary
Great for storing•Static data (images, css, javascript, html)•Instead of storing BLOB data in a database, you
can reduce pressure on database server by storing it on S3•Create a space for your users to store files (that's
what dropbox does)•Very very simple and straightforward•They provide HTTPS access for no extra cost•Unbelievably cheap product•NOTE: You can't delete a bucket unless you delete
all files inside first
Elastic Beanstalk
Provides PaaS interface (like Google App Engine and Heroku)Connects EC2, RDS, ELB togetherYou don't need to use Elastic Beanstalk but it is
freeYou only pay for the underlying infrastructure that
is used (i.e. EC2 instances)Provides really convenient way to upload your
software
Elastic Beanstalk
ELBELBLoad BalancerLoad Balancer
Incoming Traffic EC2EC2
EC2EC2
EC2EC2
EC2EC2
Makes scaling easy
RDSRDS(DB (DB
Server)Server)
Elastic Beanstalk
You create a new “Environment” with your choice in OS (i.e. Linux (different variants) or Windows) and your preferred configuration (i.e. PHP/Apache)Create a unique name for the EnvironmentThe website will be X.elasticbeanstalk.comChose option to also make a separate database
server using RDS productChose your instance type (t1.micro is free for 1st
year)Make sure you set up a key_pair so you can SSH
into your EC2 instanceChose your RDS database type (i.e. mysql) and
root database name and password and size of database
EC2 instance typeshttps://aws.amazon.com/ec2/instance-types/http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts_micro_instances.htmlhttp://haydenjames.io/when-to-use-amazon-ec2-t1-micro-instances/
Elastic Beanstalk•After it creates the EC2 instance etc. you will be able to upload your website using its Deployment Service.
• You need to zip up your PHP files AND not the folder containing your PHP files
•This is much more convenient than accessing your EC2 instance using SSH, setting up Apache and then uploading your PHP files.
•You can also set up ELB (Load balancer) and configure it so you set limits on how many EC2 instances are created and when to kick-start them (i.e. when traffic is peaking, CPU will be close to 100%)
Elastic Beanstalk
•RDS will provide you with an endpoint to use in your PHP programs to connect to your database.
•If you want, you can host database yourself in same instance OR create a new instance specifically for MySQL server
•Be careful not to store files on EC2 server because it could get shut down anytime. Always store permanent data in S3, RDS or EBS (Elastic Block Store)
Elastic Beanstalk – Optimising Costs
•If you have a lot of traffic, you must optimise the way scaling is done.
•You could save $100-1000 per month if you do it right.
•There are many ways to scale: Increase capacity of EC2 instance (more memory/CPU) or use smaller capacity instances but create more of them when traffic increases.
•There are tools they provide to help you calculate optimum configuration
ELB (Load Balancer)•You can upload your SSL certificate for HTTPS
connection•If you have an intermediate certificate from SSL
provider, open up the file and swap the two certificates around
•Make sure you don't use HTTPS between Load Balancer and EC2 instance
•ELB will set up a header: •HTTP:X-Forwarded-Proto = https
•You can set up apache to detect it and do redirections or do it via PHP code
Route 53Domain Name Service (DNS) server for use with
your custom domain.
You need it to link custom domains to your EC2 instance or Load Balancer (since they have variable IP addresses)First you create a “Hosted Zone” with your domain
Route 53•There will be 2 records already (Type NS and SOA)•Go to your domain name registrar (i.e. godaddy)
and change your DNS records to point to the 4 addresses under NS•Click “Create Record Set” and register a Type A.•Link it to your EC2 instance or ELB (they'll be on
list)•Wait some time and then your domain will link
BONUS Topic: HHVM and Hack
http://www.youtube.com/watch?v=p5S1K60mhQU
Haiping Zhao, the Senior Server Engineer at Facebook
•One of the biggest bottlenecks at FB was the PHP language.
•They had 2 choices:
• Change to a faster compiled language
• OR make PHP faster
HHVM and Hack
•Another language wasn't practical because they had thousands of the best PHP programmers working for
them and the code base was already millions of lines.
•They chose a two-fold strategy
• Make PHP faster
• As code needed to be updated, change it to C++ (so percentage of overall PHP codebase gets smaller and smaller over time)
HHVM and Hack
Why PHP was chosen:
•Easy to read/Easy to Learn
•Object Orientated
•Weakly typed (don't need to worry about var-type)
•Operators overloaded ->Convenient
•Core PHP library is small (200-500 basic functions)
•Easy to Debug → Just refresh browser
Easy to Read/Write/Debug!
HHVM and Hack
To achieve these objectives, certain design decisions were made in PHP Language and Zend Engine.
PHP is one of the slowest languages (using a framework will slow it down further)
HHVM and Hack
•Ordinary when you refer to PHP, you actually mean the Zend Engine which interprets the PHP code line by line.
•FB's original solution was PHP->C++ conversion called HipHop
•Then they created HHVM which uses same principles as JVM (Just in time compilation)
•FB no longer uses Zend Engine. All PHP is run on HHVM which they released publicly for free
HHVM and Hack
HHVM is designed to run 100% of PHP without any changes. To date almost all of PHP runs 'out of the box'
To date: Laravel and Codeigniter work 100% with no changes. Symfony works 99%.
http://hhvm.com/frameworks/
Instead of waiting for PHP 6 to come around, they also created an extension to PHP language called Hack.
HHVM and Hack
Hack helps you help the compiler by telling the compiler what the type of the arguments are to a method, determining what type of array you want to use more specifically etc.
By giving more hints to the compiler on what you are trying to achieve, the compiler can optimise your code better.
Undo's the features of PHP that make it slow
FB claims you can get 5-20x speed increase. Average wordpress site can get 9x increase
HHVM and Hack
HHVM and Hack
How does this connect to AWS?
On the official github page:
https://github.com/facebook/hhvm/wiki/Building-and-installing-HHVM-on-Amazon-Linux-2014.03
There are instructions on how to install it onto an EC2 instance (including a t1.micro)