Search code examples
amazon-web-serviceschef-infrachef-recipeaws-opsworks

AWS Opsworks using "environment_variables" on Deploy recipes


I want to use the environment_variables of an App to set some properties, but my instance keep failing at setup, reading the documentation (http://docs.aws.amazon.com/opsworks/latest/userguide/workingcookbook-events.html) I found this:

"Setup includes Deploy; it runs the Deploy recipes after setup is complete.".

This is part of my Deploy script

if node['deploy']['ws']['application'] && node['deploy']['ws']['environment_variables']['is_root_app'] == 'true'
  web_app node['deploy']['ws']['application'] do
    server_name node['fqdn']
    server_aliases node['deploy']['ws']['domains']
    template 'web_app.conf.erb'
    docroot node.default['apache']['docroot_dir']
    ssl_enabled node['deploy']['ws']['ssl_support']
    enable true
  end
end

This is the Setup log

NoMethodError
-------------
undefined method `[]' for nil:NilClass

Cookbook Trace:
---------------
/var/chef/runs/254b8caa-cd70-4525-a2fd-71044afdf62d/local-mode-cache/cache/cookbooks/WebService/recipes/deploy_app.rb:8:in `from_file'

Relevant File Content:
----------------------
/var/chef/runs/254b8caa-cd70-4525-a2fd-71044afdf62d/local-mode-cache/cache/cookbooks/WebService/recipes/deploy_app.rb:

1:  # 
2:  # Cookbook Name:: WebService 
3:  # Recipe:: default 
4:  # 
5:  # Copyright (c) 2016 The Authors, All Rights Reserved. 
6:   
7:   
8>> if node['deploy']['ws']['application'] && node['deploy']['ws']['environment_variables']['is_root_app'] == 'true' 
9:    web_app node['deploy']['ws']['application'] do 
10:      server_name node['fqdn'] 
11:      server_aliases node['deploy']['ws']['domains'] 
12:      template 'web_app.conf.erb' 
13:      docroot node.default['apache']['docroot_dir'] 
14:      ssl_enabled node['deploy']['ws']['ssl_support'] 
15:      enable true 
16:    end 
17:  else 

Whats the purpose of running the Deploy recipes after Setup, if I didnt deployed any App yet??? Where the environment_variables can be used if not on the Deploy recipes???


Solution

  • Imagine you have everything set up and you just start a new machine. It will run setup, configure then deploy otherwise you would end up with an instance that's not ready for production.

    deployment fails when you're trying to access attributes that aren't set

    You can use http://ruby-doc.org/core-1.9.3/Hash.html#method-i-has_key-3F to check this

    e.g.

    if node[:deploy].has_key?("ws")
    
    end
    

    Same for your env variables