How to set up Rails secrets

by Jason Swett,

This is part 4 of my series on how to deploy a Ruby on Rails application to AWS. If you found this page via search, I recommend starting from the beginning.

Overview of this step

We need to set up the Rails secrets security feature. It’s a relatively simple step although it does require us to jump through a few hoops.

1. Run rails credentials:edit

First we need to give permission to the current user, ubuntu, so we can make changes.

cd /var/www/hello_world
sudo chown -R ubuntu:ubuntu .

When we run the rails credentials:edit command, it will have us edit a credential file. We need to specify the editor that should be used for this action. In this case I’ll specify Vim.

export EDITOR=vim

Now we need to delete the existing config/credentials.yml.enc or else there will be a conflict.

rm config/credentials.yml.enc

With all these things out of the way, we can finally edit our credential file. No changes to the file are necessary. Just save and exit.

rails credentials:edit

Lastly, we need to give permissions back to the nginx user, www-data. Restart nginx afterward.

sudo chown -R www-data:www-data .
sudo service nginx restart

2. Verify success

Now, if you visit your EC2 instance’s URL in the browser, you should get this error:

The significant thing about this error is that it’s coming from Rails, not nginx. So we’ve made it all the way “to Rails”.

If you run tail -f log/production.log before refreshing the page, you should be able to see the exact error that’s occurring. It should be something like this:

This is telling us there’s no PostgreSQL server running, which is true. We can fix this problem in the next step: setting up our RDS database.

2 thoughts on “How to set up Rails secrets

  1. Oliver

    Just for those who follow me! I had an issue where I could not see the Ruby error, but remained on the nginx welcome page. This was because I had not updated the root directory in the stage before – i.e. the following line needs to be updated in your nginx server information:

    root /var/www/hello_world/public;

    The line is given in the example file on the step before, but you aren’t explicitly told to change it so I originally missed it. This will then fix the issue!

    Great tutorial

    Reply
  2. Youssef

    Thank you for this extensive and clear tutorial. I had one issue that I worked around but I’d like to know how to fix it. After I configured everything until the secret_key error which was also fixed by writing the creds, the server returned a rails error of 404 – the production logs were showing given I have a root page, every other route than this was giving an nginx 404 which made me wonder whether nginx was running my application in the first place. My nginx config file was pointing to my app successfully, esp since before the secret_key issue I fixed issues resulting from my codebase.

    How I worked around that is by adding a to my nginx config file in the location attribute and running the command in my root. This worked as expected, showing me an SQL error. Why was nginx responding with 404 errors? Are there other checks for me to follow? Appreciate it

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *