Tuesday 6 August 2013

a nodes journey into the amazon


Node.js + Coffee + Amazon

Here I'm going to run through hosting your Node server on the Elastic Beanstalk

A super quick intro to Elastic Beanstalk: 
Amazon's Elastic Beanstalk is a deployment service that allows you to encapsulate different amazon services to provide a specific server and resource configuration based on your requirements. + There is no extra cost for this service. To find out more read Amazon's AWS Elastic Beanstalk Components

*Note: Beanstalk refers to each service collection as an "Application".

In this "Application", beanstalk will pull in 
Lets get started! ^_^


I am going to uses a directory called "aws" and I will use my Git basics as my server code. This is important as we will be using git to upload our code to beanstalk! In your command-line, go to this "awsdirectory. but we will also need a "package.json" file to tell our node server about our coffee source.

File: package.json
{
"name": "AmazonCoffee",
"version": "0.0.1",
"description": "An example of a nofe.js running CoffeeScript source",
"homepage": "http://www.codemeasandwich.com/2013/08/a-nodes-journey-into-amazon.html",
"scripts": {
   "start": "coffee index.coffee"
 },
"dependencies": {
    "coffee-script": "*"
  }
}

Console ~ Lets stage our files. 
 git add . 

Next we commit our staged file.
Console
 git commit -m "added configuration file "package.json" for Node to run index.coffee" 
You should get something like the following:
 [master (root-commit) 950b29badded configuration file "package.json" for Node to run index.coffee
 1 file changed, 19 insertions(+)

Next comes the real juicy bit! Deploying to AWS Elastic Beanstalk

You will now need to download & install the Elastic Beanstalk Command Line Tool 

Once you have downloaded the zip file. Extract it to your node directory.

Next you will need to add to your systems environmental variables.

Console - Linux: *Remember to match the python folder version with the version of python that you have installed
 export PATH=$PATH:node/AWS-ElasticBeanstalk-CLI-2.5.1/eb/linux/python2.7
On windows you will need to add ";c:\node\AWS-ElasticBeanstalk-CLI-2.5.1\eb\windows\" to your PATH in your Environment Variables. A good step by step can be found at How to set the path and environment variables in Windows

Back in our server AWS folder lets run.

Console
 eb init 
Next you will get:
 Enter your AWS Access Key ID: 
To get your key you can follow my Coffee and S3 tutorial.

With your ID and key in hand, enter your ID.
 Enter your AWS Secret Access Key: 
Now you can pick a region to setup you server
 Select an AWS Elastic Beanstalk service region. 
For me I picked 4) EU West.. just cos!

Next;
 Enter an AWS Elastic Beanstalk application name (auto-generated value is "aws"): 
and

 Enter an AWS Elastic Beanstalk environment name (auto-generated value is "aws-env"): 
Here you can just hit enter and it will use the defaults based on your working directory(highlighted in yellow). 

 Select a solution stack. 
 Available solution stacks are:  
 5) 32bit Amazon Linux running Node.js 
For this I went with option 5. You could pick 6, if you want a 64bit version
Next you will be asked what type of "environment" you want?
 Select an environment type.
 Available environment types are:
 1) LoadBalanced
 2) SingleInstance 

You're best off picking 2) 'Single Instance' as you will only need to 'Load Balanced' with a live site.
 Create an RDS DB Instance? [y/n]: 
We don't need a database right now, so "n"
Next pick a profile.
 Attach an instance profile
1) [Create a default instance profile]
2) [Other instance profile]
or hit enter and lets go with the default. 1

* You can change you Beanstalk configuration, by running the init command again.
For each setting you can just hit Enter to use the previous settings.

lets deploy our server ^_^

 eb start 
 Starting application "aws".
 Would you like to deploy the latest Git commit to your  environment? [y/n]: 
Lets go with "y".. This will take a while.. but you should be getting updates while(Really!) its deploying.

After it's done you'll be given a URL to access your node server. 
 Application is available at " ... .elasticbeanstalk.com". 
If you have any problems let me know ;)

... continue reading!

Sunday 4 August 2013

Git basics



Here I'm going to run through the very basics of getting started with Git. Simply Git is used to store our server code. It is a LOT more powerful than that, but every needs to start with baby steps.

First step is to download/install the latest version of Git on your machine. 

Now I am going to build on my  node.js/coffee example

Once you have the source running. I want you to point your terminal to your directory where you have the coffee source saved.

Terminal

 git init 
Your path should now have "(Master)" at the end, but we now need to add our server code into the newly created repository.

Terminal ~ This will stage all the files
 git add . 
 * You can think of staging like adding to a list of files that you are ready to commit.

Next we commit our staged file.

Terminal
 git commit -m "First commit" 
You should get something like the following:
 [master (root-commit) 950b29b] First commit
 1 file changed, 19 insertions(+)
 create mode 100644 index.coffee 
Let do a quick test to make sure all is good we our server 


So far so good! but there is one small thing bugging me.. that console message when the server starts. lets make 2 small tweaks. We are going to print out the port number and make the port selection more dynamic by adding have an optional argument when starting the script to specify the port. Else check if there is a predefined port of servers to start on.

First we will read in the port number from the command line. For this we will need process.argv which is an array containing the command line arguments. The first element will be 'coffee', the second element will be the path to our file and the last element will be the port number. The second part is process.env.PORT this will try and pull a port number from the global environment variable.

Add at the top of the script

port = process.argv[2]
port ?= process.env.PORT
port ?= 80

Replace line 15 & 17 with the below !! don't forget the indentation !!

 http.createServer(onRequest).listen (port)

 console.log ("Server on port #{port} has started.")


The changes above will read in a port value, If one can't be found 8888 will be set as a default. The second part sets the port number and will output the number when the server is started.


Terminal
 coffee index.coffee 8889 
You should get something like the following:
Serveon port 8889 has started.

Now lets commit our newly modified file with the following two commands

Terminal ~ This will stage just the index.coffee file
 git add index.coffee 
 git commit -m "the port number can be passed as a command line argument and the port number will be displayed on terminal"
And that is for now.
... continue reading!