Search code examples
amazon-s3chef-infraexecutechef-recipe

Chef scripts, execute resource and execution order


I have the following Chef recipe:

Chef::Log.info "Downloading files"
execute "aws s3 sync s3://mybucket/prod/ . --delete --region=ap-southeast-2" do
  user 'vagrant'
  environment 'HOME' => "/home/vagrant"
  cwd '/var/www/public/lib/files'
end
Chef::Log.info "Finished downloading files"

The thing is that "Finished downloading files" is being output before the s3 sync command finishes. I can tell because the script takes a long time to finish afterwards and I can see the files progressively being created.

My understanding is that Chef recipes are executed sequentially, but does execute also wait for the shell process it creates to return? Or is there something else I'm missing?


Solution

  • Chef runs in two steps. First it gathers all of the resources together, and then it executes them. In your case, the execute resource actually runs during the second phase, but the log statement runs during the first phase. If you change your log statement to be a resource, it will behave as you expected:

    log "Finished downloading files" do
      level :info
    end