Remove default SSH host keys before publishing an AMI

AMIs that have the same ssh host key pairs as other public amis will be made private by amazon to prevent man-in-the-middle attacks, so always remove SSH Host Key Pairs (they will be regenerated with new unique keys automatically)

rm /etc/ssh/ssh_host_dsa_key
rm /etc/ssh/ssh_host_dsa_key.pub
rm /etc/ssh/ssh_host_key
rm /etc/ssh/ssh_host_key.pub
rm /etc/ssh/ssh_host_rsa_key
rm /etc/ssh/ssh_host_rsa_key.pub

Installing MySql Handlersocket in Ubuntu Natty for Ruby

Install

sudo apt-get install mysql-server -y;
sudo apt-get install handlersocket-doc -y;
sudo apt-get install handlersocket-mysql-5.1 -y;
sudo apt-get install libhsclient-dev -y;

Configure

#/etc/mysql/my.cnf -- under mysqld section
loose_handlersocket_port = 9998
loose_handlersocket_port_wr = 9999
loose_handlersocket_threads = 16
loose_handlersocket_threads_wr = 1
open_files_limit = 65535 

Start

mysql -e "install plugin handlersocket soname 'handlersocket.so';"
sudo /etc/init.d/mysql restart

# should show lots of handlersocket processes
mysql -e "show processlist;"

Use

gem install handlersocket

require 'handlersocket'
h = HandlerSocket.new(:host => '127.0.0.1', :port => '9998')

# open PRIMARY index on widgets.user table, assign it ID #1
h.open_index(1, 'widgets', 'user', 'PRIMARY', 'user_name,user_email,created')
...

more of this can be found at ruby handlersocket introduction and ActiveRecord Handlersocket gem

Config files for heroku or duostack

To get config variables to herkou / duostack simply base64 encode them and store them into the ENV.
The cfg.rb is kept very simple, so you can also load it where rails is not yet loaded.
This scales to up to 3900 characters for duostack and 10000+ for heroku. Add gzip to get even more…

# lib/cfg.rb
require 'active_support/core_ext/hash/indifferent_access'

env = defined?(Rails.env) ? Rails.env : (ENV['RAILS_ENV'] || 'development')
config = if encoded = ENV['CONFIG_YML']
  require 'base64'
  Base64.decode64(encoded)
else
  File.read('config/config.yml')
end
CFG = YAML.load(config)[env].with_indifferent_access.freeze

# config/application.rb
require File.expand_path('../../lib/cfg', __FILE__)

# script/configure_heroku.rb
#! /usr/bin/env ruby
require 'rubygems'
require 'rake'
require 'base64'

config = Base64.encode64(File.read('config/config.heroku.yml')).gsub("\n","")
sh "heroku config:add CONFIG_YML=#{config}"

Creating a EC2 Micro instance via Fog

0.03c/hour (14€/month) is pretty cheap for a small server so lets try this 🙂

Generate and download a key-pair, so you can login to the instance via ssh.

AMI-id used is a micro ebs ubuntu 10.04 instance on eu-west-1, choose your own at http://uec-images.ubuntu.com/releases/lucid/release

Be sure to add ssh port to your security group or you will get a nice “port 22: Connection timed out”

Start the servers!

require 'fog'
fog = Fog::Compute.new(
  :provider => 'AWS',
  :region=>'eu-west-1',
  :aws_access_key_id => 'yyy',
  :aws_secret_access_key => 'xxxx'
)

# start a server
server = fog.servers.create(
  :image_id=>'ami-311f2b45',
  :flavor_id=>'t1.micro',
  :key_name => 'pey-pair-name'
)

# wait for it to get online
server.wait_for { print "."; ready? }

# public address -> ec2-79-125-45-252.eu- west-1.compute.amazonaws.com -> ssh into it
server.dns_name

# instance id -> find it again
fog.servers.get(server.id)

# shutdown
server.destroy

Connect via ssh

ssh -i KEY-PAIR.pem ubuntu@SERVER-DNS-NAME