Today I spent a frustrating amount of time trying to figure out why a data conversion task was failing. The task I was running takes about 15 minutes, and at about 10 minutes in it would choke and the error I received included the following message:

Excon::Errors::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code>
                    <Message>The authorization header is malformed; the region 'us-east-1' is wrong;
                    expecting 'us-west-2'</Message>
                    <Region>us-west-2</Region>
                    ... "

We are using AWS for multiple services, so immediately I dove into environment variables and config files to see if a region might have been set improperly. I found a couple places where we had explicity defaulted to ‘us-east-1’ if an environment variable was not set- but in all of those cases the proper variable did exist. After a couple more failed tries (fortunately I had other things to work on while the conversion was running), I managed to track down a meaningful stacktrace. It turned out that when we tried to save a Model after setting it’s uploaded_file (it’s a Rails app), then we would receive the aforementioned error.

A quick Google search and dive into the Carrierwave::Storage::Fog ruby docs revealed the culprit:

You need to setup some options to configure your usage:
...
:region (optional) defaults to 'us-east-1'

So, if you’re using CarrierWave and Fog for attaching files to models- be sure that you have the proper region set in your config/initializers/carrierwave.rb file. All I had to do was add a region key-value pair and the conversion went smooth as silk (well almost, but that’s another post…). After the update our carrierwave.rb file includes this for our fog credentials:

config.fog_credentials = {
    provider: 'AWS',
    aws_access_key_id: 'SOMEKEY',
    aws_secret_access_key: 'SomeSuperSecretKey',
    host: 's3.amazonaws.com',
    region: 'us-west-2'
  }