033 AWS

16 Feb 2013

Amazon Web Services or AWS is a set of web based services by Amazon. We will use Elastic Cloud Compute to launch a simple html page as well as a hello world in NodeJS. Next, we will use Elastic Beanstalk to deploy a Sinatra hello world with GIT.

Download video: mp4

Sample code: Github

Similar episodes: 022 SSH

##Background on AWS

  1. Amazon Web Services and its products & docs
  2. Developer Tools
  3. Cloud Formation

##Things to learn with AWS

####1. Elastic Cloud Compute (EC2) for a simple index.html

  1. Go to the Amazon Management Console for EC2
  2. Click Launch Instance
  3. Create a New Instance > Classic Wizard
    • Choose an AMI > Amazon Linus AMI 2012.09 Free tier or any other Free Tier
    • Create Key Pair > Download *.pem and change its permission level on our local machine

    cd ~/Desktop chmod 600 mykey.pem - Configure Firewall > Create a new Security Group > Add SSH, HTTP, HTTPS

  4. Instance status should be running
  5. Copy the public DNS which should have the format of xxxxxxxxxxx.amazonaws.com
  6. login to the EC2 instance through ssh ssh -i mykey.pem ec2-user@[public-dns]
  7. connect as root user and install httpd

    sudo su - yum install httpd

  8. copy the simple html page to the folder var/www/html

    ``` cd ../../var/www/html wget [dropbox index.html-public-url]


  9. connect as root user

    service httpd start service httpd status

  10. visit the public DNS address to view the index.html
  11. Terminate or stop the EC2 instance as you wish

####2. Elastic Cloud Compute (EC2) for node.js 1. Setup another Free Tier Micro EC2 Instance 2. connect to the instance with ssh:

ssh -i mykey.pem ec2-user@[public-dns]
``` 3. Update EC2 Linux. Install Git, Node

sudo yum update
sudo yum install gcc-c++ make
sudo yum install git
git clone git://github.com/joyent/node.git
cd node
git checkout v0.8.19
sudo make install
``` 4. Add the path to file `/etc/sudoers`

sudo su
vi /etc/sudoers

- Press `i` to go into Insert Mode
- find line `Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin` and add `:/usr/local/bin` to it
- Press `ESC` and then `:wq!` to save and exit    2. 5. Install `npm`

git clone https://github.com/isaacs/npm.git
cd npm
sudo make install
``` 6. Check all are installed

git --version
node --version
npm --version
``` 7. Create a simple hello world in node and put it in the public folder of dropbox:

var sys = require( "sys" );
var http = require( "http" );

var server = http.createServer(
  function( request, response ){
    response.writeHead( 200, {"content-type": "text/plain"} );
    response.write( "Hellow world from AWS!\n" );

server.listen( 8080 );

``` 8. Make a new folder

cd ~/var
sudo mkdir site
cd site
sudo wget https://[dropbox-public-url]/server.js
``` 9. Add port forwarding

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
``` 11. Start the node server and visit the public dns

node server.js
``` 10. Install [Forever](https://github.com/nodejitsu/forever) to keep our Node.js application running even after we logout of our EC2 instance. If npm throws an error without sudo, [change the ownership](http://stackoverflow.com/questions/16151018/npm-throws-error-without-sudo) of the `.npm` directory with `chown`

npm install forever -g
  1. Start the forever server and log out of EC2 and the server will still run!

    forever start server.js

####3. Deploy Sinatra with Beanstalk

  1. download aws elastic beanstalk command line tools

    unzip AWS-ElasticBeanstalk-CLI-2.3.zip

  2. copy the directory AWS-ElasticBeanstalk-CLI-2.3/eb to /usr/local or any other folder that you wish.
  3. add export path to your bash profile or zsh profile file

    ``` export PATH=$PATH:/usr/local/eb/macosx/python2.7:$PATH


  4. restart command line or source bash profile or zsh profile
  5. eb --version in the command line to see that it has installed
  6. create a new folder for a hello world sinatra application
    • config.ru

    require './hello' run Sinatra::Application - hello.rb

    require 'sinatra' get '/' do "Hello World!" end - Gemfile

    source 'http://rubygems.org' gem 'sinatra'

  7. Initiate a git repository

    git init git add . git commit -m "initial commit"

  8. initiate elastic beastalk with command line code eb init
  9. commit the newly create .gitignore
  10. start the elastic beanstalk with eb start and git aws.push
  11. eb stop to stop the application and eb delete to delete the application

##More Resources on AWS

  1. Videos: AWS as an overview, launching a website, overview of EC2
  2. Developer Tools
  3. Starting with EC2, installing command line tools
  4. nodejs with EC2 complete guide and another blog post
  5. nodejs aws sdk
  6. Wordpress with ec2, rds, beastalk
  7. Deploy Sinatra with Elastic Beanstalk
  8. aws cli


  1. David Strada for pointing out why npm should not be run as root

##Build Link of this Episode

A Practical Guide to HTML & CSS beginner’s html and css