Chef::Exceptions::ChildConvergeError While Deploying to OpsWorks

By richardtylee

I attempted to deploy my Rails app to AWS OpsWorks and get the following error:


[2016-01-01T08:39:36+00:00] ERROR: Running exception handlers
[2016-01-01T08:39:36+00:00] ERROR: Exception handlers complete
[2016-01-01T08:39:36+00:00] FATAL: Stacktrace dumped to /var/lib/aws/opsworks/cache.stage2/chef-stacktrace.out
[2016-01-01T08:39:36+00:00] ERROR: deploy[/srv/www/myapp] (deploy::rails line 65) had an error: Chef::Exceptions::Exec: if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi returned 1, expected 0
[2016-01-01T08:39:36+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

OpsWorks doesn’t give the best logging from the AWS Console. All we know from this is that the deployment failed on the following line:


if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi

This means that it failed during a migration, but several possible issues could have caused that call to fail. As John C. Bland suggests, the best option to SSH into the EC2 instance and get the stacktrace.

If you haven’t set up ssh access to your instances, you can read: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html

When I ssh’ed into my instance and tried to run /usr/local/bin/rake db:migrate, I found a handful of problems. These are just a few that I encountered:

Problem 1


/usr/local/bin/bundle exec /usr/local/bin/rake db:migrate
rake aborted!
Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file - ["config/database.yml"]


In OpsWorks, Chef will in my database.yml file. Unfortunately, I had forgotten to connection my database instance to my app. Under Opsworks -> Apps -> Data Sources -> Database Instance, I can set which database instance I want.

Problem 2


$ /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate 
rake aborted!
ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

Even though my app ran fine locally, I needed to add two gems to my Gemfile for production:


gem 'execjs'
gem 'therubyracer', :platforms => :ruby

Then I ran: bundle update

Problem 3


$ /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate 
rake aborted! database configuration does not specify adapter

Here, I have forgotten to add a mysql adapter. To fix this, I modified my Gemfile like this:


# Use as the database for Active Record
group :development, :test do
  gem 'sqlite3'
end
group :production do 
  gem 'mysql2', '~> 0.3.13'
end

After all this, I finally got my app to deploy successfully.