Search code examples
chef-infraaws-cloudformationaws-opsworks

Data bag 'aws_opsworks_instance' not available


Does anyone have any idea why

instance = search('aws_opsworks_instance', 'self:true').first

# ...

    instance_host: instance['hostname']

would intermittently result in

undefined method `[]' for nil:NilClass

on OpsWorks?

I've been searching for hours now and didn't find anything, so I am starting to think this is a byproduct, not the actual issue.

Since it happens every once in a while, I am thinking it may be a "race condition". Maybe OpsWorks takes a while to inject this, so Chef needs to be told to wait? If so, I couldn't find anything of the sort in the docs though.

Any ideas why this happens and how it can be fixed/avoided?


Solution

  • OpsWorks seemingly relies on instance names to identify these data bags, so since our script was requesting to create several new instances at the same time, there were some conflicts when assigning names (i.e. names were duplicated).

    The issue was avoided by changing OpsWorks naming theme from Greek_Deities_and_Titans (e.g. hercules, zeus, poseidon) to Layer_Dependent (e.g. api1, api2, notifications1, notification2).

    Although it may not be a definitive fix for the issue of Opsworks naming two instances with the same name, I have yet to encounter the issue using Layer_Dependent naming, so at least the chances are greatly reduced.