033 AWS
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
##Things to learn with AWS
####1. Elastic Cloud Compute (EC2) for a simple index.html
- Go to the Amazon Management Console for EC2
- Click Launch Instance
- 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 - Instance status should be running
- Copy the public DNS which should have the format of
xxxxxxxxxxx.amazonaws.com
- login to the EC2 instance through ssh
ssh -i mykey.pem [email protected][public-dns]
-
connect as root user and install httpd
sudo su - yum install httpd
-
copy the simple html page to the folder
var/www/html
``` cd ../../var/www/html wget [dropbox index.html-public-url]
```
-
connect as root user
service httpd start service httpd status
- visit the public DNS address to view the
index.html
- 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 [email protected][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
./configure
make
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" );
response.end();
});
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
```
-
Start the forever server and log out of EC2 and the server will still run!
forever start server.js
####3. Deploy Sinatra with Beanstalk
-
download aws elastic beanstalk command line tools
unzip AWS-ElasticBeanstalk-CLI-2.3.zip
- copy the directory
AWS-ElasticBeanstalk-CLI-2.3/eb
to/usr/local
or any other folder that you wish. -
add export path to your bash profile or zsh profile file
``` export PATH=$PATH:/usr/local/eb/macosx/python2.7:$PATH
```
- restart command line or source bash profile or zsh profile
eb --version
in the command line to see that it has installed- 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'
-
Initiate a git repository
git init git add . git commit -m "initial commit"
- initiate elastic beastalk with command line code
eb init
- commit the newly create
.gitignore
- start the elastic beanstalk with
eb start
andgit aws.push
eb stop
to stop the application andeb delete
to delete the application
##More Resources on AWS
- Videos: AWS as an overview, launching a website, overview of EC2
- Developer Tools
- Starting with EC2, installing command line tools
- nodejs with EC2 complete guide and another blog post
- nodejs aws sdk
- Wordpress with ec2, rds, beastalk
- Deploy Sinatra with Elastic Beanstalk
- aws cli
##Thanks
- 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