Getting Heroku Cedar and Rails 3.1 Asset Pipeline to Play Nicely Together

I recently migrated a Ruby on Rails 3.1 app from the Heroku Bamboo stack to Heroku Cedar. If you’re doing the same, here are a few notes to help avoid snags on getting the Rails Asset Pipeline working efficiently.

Unlike Bamboo, Cedar does not offer Varnish as a reverse proxy cache, nor does it automatically gzip content. You need to do it yourself. Heroku recommends:

  1. Use memcached, with Dalli as the memcached client. Make sure to follow the Rails 3 section.
  2. Use Rack::Cache as a substitute for Varnish. Heroku links to this article which explains how to integrate Heroku with Rack::Cache. I couldn’t get it to work, so I hunted around and pieced together the folllowing riff.
In your runtime environment file (e.g. production.rb), add this:
require 'rack-cache'
My::Application.configure do
...
  # Enable Rack::Cache
  config.middleware.use Rack::Cache,
   :metastore => "memcached://#{ENV['MEMCACHE_SERVERS']}/meta",
   :entitystore => "memcached://#{ENV['MEMCACHE_SERVERS']}/body"

You can also set HTTP headers in production.rb as follows. (3600 is an example value, make this whatever you want.)

# Add HTTP headers to cache static assets for an hour
config.static_cache_control = "public, max-age=3600"

And you may want to add this to your config.ru to get gzip working:

use Rack::Deflater

References:

Heroku, MongoHQ, and Amazon S3 Backups

I spent a few hours yesterday convincing MongoHQ to back up a Mongo database to an Amazon S3 bucket via an Amazon “IAM” account. Here’s the secret recipe. Hopefully it saves someone time.

Context: MongoHQ, which I’m using with one of my work projects right now, is a service that hosts MongoDB databases. It integrates with Heroku, allowing you to get started on a Heroku app using MongoDB very quickly. MongoHQ supports hourly and daily backup of their databases to Amazon S3 buckets, but alas, not yet for databases provisioned automatically via Heroku. So if you want to step up to a backed-up database you’ll have to set up your own. Here are the steps I followed to do it.

Secret decoder ring:
S3 = Amazon Web Services Management Console, S3 tab.
IAM = Amazon Web Services Management Console, IAM tab.
MHQ = Mongo HQ console.
HKU = Heroku.

Recipe:

  1. S3 : Create a bucket for your app’s backups, e.g. “my-app-backups”.
  2. IAM: Create an IAM User account for MongoHQ to use, e.g. “mongohq”. Download the credentials and store them safely.
  3. IAM: Create an IAM Group to contain all users with backup permission, e.g. “myapp-backup-writers”.
  4. IAM: Add the mongohq User account to the newly created Group.
  5. IAM: Using the Permissions tab of the Group’s properties attach a new security policy, granting permission to write to the myapp-backups bucket. See below for a sample policy.
  6. MHQ: Sign up for your own MongoHQ account.
  7. MHQ: Create the database you wish to use with Heroku.
  8. MHQ: Select the “Backups” tab and enter the IAM User credentials from step 2.
  9. MHQ: Click “Save Settings”. If all is well you’ll see a message, “Setting Updated”.*
  10. MHQ: Create a new user on the Database Users tab.
  11. MHQ: Copy down the Mongo URI string from the Database Info tab, substituting your user name and password from step 10.
  12. HKU: In the root directory of your Heroku app do “heroku config:add MONGOHQ_URL=’URI’”. URI is the Mongo URI from step 11. (Normally Heroku sets this variable for you when it provisions a MongoHQ database. You’re overriding it with a link to your own database.)

* Step 9 is where I got stuck. It worked fine with my own personal security credentials, but not with the IAM account I created specifically for MongoHQ to use. (I wanted more security… giving your main account credentials to a 3rd party site isn’t smart.) I fiddled around with the security policy for quite a while, and eventually discovered I needed the “ListAllMyBuckets” permission. Here’s the security policy that worked for me:

    {
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [ "s3:ListAllMyBuckets" ],
          "Resource": "arn:aws:s3:::*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "s3:PutObject",
            "s3:PutObjectAcl"
          ],
          "Resource": [
            "arn:aws:s3:::MY-APP-BACKUPS/*"
          ]
        }
      ]
    }

Notes from Howard Lindzon’s DemoCamp Toronto 29 Talk

Howard Lindzon spoke last night at DemoCamp 29 (on twitter; democamp site). It was a fun ramblin’ jamblin’ talk. Here are a few takeaways that stood out for me:

Treat the world as flat, and find a defensible niche to start in. E.g. think of the world as the Risk game board, and pick an initial territory where your chances of getting established are high. If you start in Europe, you have lots of borders and the battle is much harder than, say, starting in Australia. (Or Kamchatka. Remember Kamchatka? I think I learned more geography in Risk than in school.)

Follow your own rules. In response to an audience question about mistakes he has made in his career, Howard explained that many of them stemmed from breaking his own rules when investing, e.g. (1) investing in the market when all signs still clearly pointed to down, (2) investing in companies whose CEOs did not have deep domain experience. (Howard likes such companies best, as a rule, for his investments.)

Have a sense of humor, especially about yourself. Howard’s self-deprecating humor made him a real pleasure to listen to, and he explained that his habit of poking fun at himself is part of why people like to follow him in social media. “I make fun of my own mistakes first, before anyone else has a chance to.”  He talked openly about passing on a few investments that would have been incredibly profitable, e.g. a chance to invest $25K in Twitter when it was valued at only $20M.

Spot and leverage the big waves. He spoke to only one slide during the 30-or-so minutes, and it showed two graphs depicting the meteoric rise in Twitter and Facebook usage. His main point was that when you can get in front of a huge market-changing trend (like social media) you are maximizing your business opportunities to do well. “You don’t need to catch every wave. Just one amazingly great wave in your lifetime is all you really need”. Also, fighting a paradigm-changing trend is futile. So, in this specific example, which Howard calls “the social leverage trend”, figure out which of Facebook and Twitter it makes best sense to align your business with first, then integrate and try to ride the wave.

Use your dashboards. He talked about dashboards and his view that they’re essential to business success. From memory, “What’s your dashboard? It’s never too early to start building your dashboard. … Figure out the one or two pages you need to look at every day to understand both the macro market picture and the micro [i.e. the key signals/metrics from your own business]. For me it’s http://techmeme.com, http://angellist.com, and market all time highs.” These dashboards give him “the very early market signals [from AngelList] and the loudest public market signals [from all-time highs]“.

Thanks Howard for a fun and frank talk.

Follow

Get every new post delivered to your Inbox.

Join 348 other followers