Startup hacks and engineering miracles from your exhausted friends at Faraday

Antipattern: Using Ruby's Hash#[]

Seamus Abshere on

This is part of our antipatterns series. Ouch! Updated for 2017!

Ask yourself why you're using Hash#[]. It is is a great way to introduce silent bugs into your app.

Use Hash#fetch if you expect the value to exist

That way you get sensible error messages.

#> params = {}

#> params.fetch('really').fetch('important')
KeyError: key not found: "really"  

Use Hash#dig if you don't care

Because you don't get idiotic, non-semantic NoMethodError: undefined method '[]' for nil:NilClass errors.

#> params.dig('really', 'important')
=> nil

Avoid Hash#[] because... just... why?

#> params['really']['important']
NoMethodError: undefined method `[]' for nil:NilClass  

Special case: ENV

The Twelve-Factor App has us all using environment variables. But most of us default to ENV#[] to look stuff up... even if it's critical. Bad idea! Use fetch!

#> ENV['REALLY_IMPORTANT'] == 'thing'
=> false # well i hope you didn't need that

#> ENV.fetch('REALLY_IMPORTANT') == 'thing'
KeyError: key not found: "REALLY_IMPORTANT"  

Antipattern: ECS + yum update

Seamus Abshere on

This is part of our antipatterns series. Ouch!

With the recent bugs in ecs-agent 1.8.0, you may be trying to roll back to amzn-ami-2015.09.e or earlier to get a last-known-good ecs agent.

If you have yum update in your userdata, however, it updates ecs-init and that, in turn, will auto-upgrade you to 1.8.0—rolling back to an older image won't help!

But... you don't want to get rid of yum update it from your userdata because of fun CVEs in glibc and openssh.

Solution: yum update --exclude=ecs-init

Best of both worlds: you get the latest security patches and you can roll back to whatever agent you want!

Confirmed to work with Julien of AWS Support. Thanks Julien!