TheDizzle
TheDizzle

Reputation: 1574

How can I pull opsworks variables into a .env file with chef?

I am very new to chef and am on the last piece of the puzzle. I am trying to create a .env file in my project root using variables set in AWS Opsworks. I can't for the life of me figure it out.

Does anyone have any suggestions or a working chef recipe for this? I've googled to my wits end.

Upvotes: 1

Views: 588

Answers (1)

edcs
edcs

Reputation: 3879

You don't usually use a .env file if you're using AWS*. The .env file mimics environment variables which are actually set at an operating system level, this is useful for when you're developing locally or you're on a hosting platform which doesn't allow you to set these yourself.

So, all you need to do is set the environment variables for your app in Opsworks and you're done. These will be loaded from the operating system by PHP and are then available to your Laravel application.

*The reason for this is you usually have sensitive data such as database and API credentials stored in here. You absolutely want to keep these separate from your code and out of source control.

Edit:

You can write a .env file during deployment a bit like this if you need. This would be what your Chef script would look like:

node[:deploy].each do |application, deploy|
  # write out .env file
  template "#{deploy[:deploy_to]}/current/.env" do
    source 'env.erb'
    mode '0660'
    owner deploy[:user]
    group deploy[:group]
    variables(
      :env => deploy[:environment_variables]
    )
  end
end

This is what your .env file template would look like:

<% @env.each do |key, value| %>
<%= key %>=<%= value %>
<% end %>

Upvotes: 5

Related Questions