Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Showing posts with label amazon. Show all posts
Showing posts with label amazon. Show all posts

Amazon AWS: Calculate your monthly costs on Amazon S3, EC2 and SQS

"its' 0.3 cents per hour, this is 0.32 cents per request"
So how can we get the subtotal cost of an AWS Cloud application?
Simple, use the Amazon Cloud Services Cost Calculator.
note: i tried it with my own parameters, and it's showed a little above what i actually pay (5$ more).

Deploying Ruby on Rails on EC2 - Update

Tom Mornini, the Founder and CTO of EngineYard, introduced me their latest announcement, Solo.
Solo is an inexpensive, web-based platform for on-demand management of your Ruby on Rails web application on Amazon’s cloud computing infrastructure. Manage your web application from development to deployment. You get Engine Yard’s Ruby on Rails deployment expertise wrapped in an easy-to-use interface.
Solo removes a lot of the hassles and provides a battle tested Rails stack and many tools required to create reliable and manageable deployment atop Amazon's wonderful AWS offerings.

Tom also mentions that using my Deploying on Ruby on Rails on EC2 guide is not recommended for production stages and that users on AWS should always keep in mind that in case of a crash or reboot of the environment will result in lost data (without proper countermeasures, like EngineYard offers)

So your setup scenario as i suggested, should be used for development only, and carefully even in that case and keep in mind the non-persistence state of EC2 instance disks.

Rails and Amazon EC2 - Beginners guide

First of all, Amazon AWS rocks. it's a great, stable and not so expensive way to get your application up and running, but also ready for any disaster to come (Someone said Digg effect?).
Getting your application on to Amazon EC2 is not as complicated as you think, managing and controlling your instances may require a professional system administrator, but i would recommend that anyway (except if you go and host your application on EngineYard than you are worry-free, but you'll pay.. ohh you'll pay for that sense of security).

Get your application Ready

There is no real need to setup your EC2 instances in day 1, you can wait until the application is mature enough to be deployed onto a production infrastructure. Amazon EC2 costs per usage, so, it will cost you to have your application up and running on EC2, keep that in mind (although the prices are a joke).

Setting things up
Once your application is ready to be deployed to EC2, you'll need an image.
Image, in EC2 terms, is a reference to an OS + all required installations and configuration needed to run your application.
The best all-round image to use with rails is Pawl Dowman's Rails on EC2 bundle (or here). EC2onRails is great. Unfortunately, if you’ve never used EC2 before, you probably won’t be able to “Deploy a Ruby on Rails app on EC2 in Five Minutes” as the documentation claims, so this document will try to fill in the gaps for someone who has never worked with EC2.

note: it's never too early to sit down and read Amazon's documentations, they are not that bad


Local machine
First thing is first, your development machine, the one from which you will be deploying to EC2 should undoubtably hold:

  • Ruby on Rails (do yourself a favor, at least version 2.1)
  • MySQL (Well, we are going to work with RailsonEC2, which includes it, if you need another db.. you'll need another image).
  • Java Development Kit 1.5 or later installed.
    Mac users should be ready-to-go (Hah! Hah!), Windows/Linux users if you don’t have it, download it from Sun's website.
    Make sure to download a version labeled “JDK”.
    Java is required for the tools that Amazon provides to manage EC2 instances. (rumor says that there are ruby management tools also, but i prefer the ones that Amazon gives).
Signing up for the services
Once you are set with these issues, you'll need to sign up for the Amazon EC2 service, do that right here.
Now, sign up for the S3 service.
Why? becaous althougn your image (later: 'instance') includes storage, images/instances can be brought down, up, dropped and even deleted, that will ultimatly, kill your data.

S3 is Amazon’s “Simple Storage Service”. S3 is a super-inexpensive service to store files into “buckets in the cloud”. S3 will be used for database backups of your Rails Application.
Sign up for S3 here.

What you'll need to keep somewhere
After signing up, you will need to collect four pieces of information from your AWS account by visiting the Access Identifiers page.
  • Your account number. The account number can be found at the upper right of the Access Idenfiers page and should be three four-digit numbers separated by dashes.
  • Your Access Key ID. This is a 20 or so character key found in a beige box a little below your account number.
  • Your Secret Access Key. This is a 40 or so character key found in another beige box just below your Access Key ID. If this is your first time on this page, you may have to generate your key. Click the “Show” button to display your 40 character key.
  • Your X.509 Certificate Create and download the X.509 certificate below the Secret Access Key section. Place the public and private keys into a folder called “.ec2″ in your home directory.
double check it! it's very very very very important.

Download and Install the EC2 Command Line Tools

The EC2 command-line tools are a Java-based set of tools that allow you to create and manage machine instances on EC2.

Download the Command Line Tools
Download here Amazon EC2 Command-Line Tools and extract the zip file, remember where you put it, i always keep it in my home folder as a hidden folder named .ec2 (/Users/eizesus/.ec2/ on OSX).



Set Appropriate Environment Variables
export EC2_PRIVATE_KEY=/Users/eizesus/.ec2/pk-5xxxxxxxx7.pem
export EC2_CERT=/Users/eizesus/.ec2/cert-5xxxxxxxxxxxxx7.pem
export EC2_HOME=/Applications/java/ec2-api-tools-1.3-24159
export PATH=$EC2_HOME/bin:$PATH
Remember to replace my library with yours.

I suggest adding these lines to your startup profile, whatever it maybe on your OS. (OSX, it's in /etc/profile).
After you do that, reload your session (close the console/command line and open it again).

Create a Key Pair the Deployment machine


ec2-add-keypair my-secret-code
The string my-secret-code can and should be anything that you like, try to pick something that makes sense for your setup (remember that i will keep on writing my-secret-code, use your phrase instead).

Save the output from that into a file named id_rsa-my-secert-code and paste everything between (and including) the “—–BEGIN RSA PRIVATE KEY—–” and “—–END RSA PRIVATE KEY—–” lines into it. Confirm that the file contents looks exactly like this, then save the file into the file:
~/.ssh/id_rsa-my-secret-code

Set permissions on the new key file:
chmod 600 ~/.ssh/id_rsa-my-secret-code

Start Up an Amazon EC2 Instance

An Amazon Machine Image (AMI) is a named configuration of an EC2 image.
The current AMI id’s for EC2onRails are:
  • ami-c9bc58a0 (32-bit)
  • ami-cbbc58a2 (64-bit)
Start up an instance of the 32-bit EC2onRails image:

ec2-run-instances ami-c9bc58a0 -k my-secret-code
The second line of the results returned will look like:
INSTANCE        i-XXXXXXXX      ami-c9bc58a0                    pending my-secret-code       0
The pending my-secret-code means that the image is pending.
To check the status of the instance build type:

ec2-describe-instances i-XXXXXXXX
Replace the i-XXXXXXXX above with the string that comes after INSTANCE in the second line of the results from the ec2-run-instances command.
Run it again until it says running my-secret-code, than you have an Amazon EC2 instance running! (yippie!).
Take note of the string that looks like
ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com
that is your machine’s address.

Authorize SSH and HTTP

ec2-authorize default -p 22
should result in:
PERMISSION default ALLOWS tcp 22 22 FROM CIDR 0.0.0.0/0
ec2-authorize default -p 80
This should return something like:
PERMISSION default ALLOWS tcp 80 80 FROM CIDR 0.0.0.0/0
you are up and ready to go!
You should be able to ssh into the your new machine, replace
ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com
with your own machine’s address:
ssh -i ~/.ssh/id_rsa-ec2-rails-keypair root@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com
You may have to type “yes” to accept the authenticity of the host.

Prepare Your Application for EC2

EC2 for Rails requires Capistrano:
sudo gem install capistrano -v 2.4.3
And then install the EC2 for Rails gem:
sudo gem install ec2onrails
Okay, you need to add three configuration files to your Rails application.
  1. Capfile - save this file at the root of your application.
  2. deploy.rb - save this file in the /config folder.
  3. s3.yml - also save this file in the /config folder.

Customize the EC2 Configurations
capfile can an be left as-downloaded.
config/s3.yml - This configuration is pretty simple, under the production section, put your AWS information that you noted above.

  aws_access_key: (you got me when you signed up)
  aws_secret_access_key: (me too)

  bucket_base_name: production.yourname.com
config/deploy.rb - There are a lot of definitions to be made, read all the comments and setup the deploy process as your applicaiton requires.

config/database.yml - For the production section, basically add any good password that you like and add hostname: db_primary. It should look something like:
production:
  adapter: mysql
  encoding: utf8
  database: appname_production
  username: user (not ROOT!)
  password: password (no, no empty passwords)
  hostname: db_primary

Run the EC2 on Rails Capistrano Tasks

cap ec2onrails:get_public_key_from_server cap ec2onrails:server:set_roles

Configure the db and stuff:

cap ec2onrails:setup
And launch your app:
cap deploy:cold
And wooooohoooo, check your application url.

That's it basically, you are up.

why i hate Adsense

As it should be clear already, i am the owner of Pdfetch.com. just until the recent facelift, i used Google's Adsense as my ad operator.

and i did an honest mistake.

i copy-pasted a snippet right above the adsense script, which contained the words "click here".

Google didn't like it so much, and sent me an email which says that i violated the usage terms of Adsense by forcing users to click on ads and they will block my site from using Adsense if i will not remove this phrase in 3 days.

WHHHATTT?

lucky me, i saw this message 2 days after my ultimatum was over (they give you 3 days to complay.... mmmm... some counter-terrorist are consulting Google? :) ). anyway by the time i saw this message and begged them to let me remove this error and get back my ads and explaining them what really happaned,
they replied by saying that the will not allow my to use it anymore on Pdfetch.com

real thanks.

that's why i like amazon now :)


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels