Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
Heroku Environment Variables

Heroku Environment Variables

Posted on September 30, 2021September 30, 2021 by Rails Hero

What Are Environment Variables ?

Storing credentials like access keys, secret keys, SMTP passwords, etc in the source is never a good idea, especially in production; Because it may be vulnerable to hacks or you might want to share the source code with other developers. So, there are Environment Variables for that. We can use environment variables to store various credentials as well as configuration parameters for our app.

Environment Vars(or ENV Vars for short), are stored in the operating system of the application server or the container or wherever your app is running. In the example below; you can see the ENV Vars of my windows machine, same goes for other operating systems or containers as well. ENV vars can be accessed globally by any application running on the server or the container. ENV vars can be added, removed as well as modified by the user.

Windows ENV Vars Example
Windows ENV Vars Example

More On Environment Variables: https://docs.ruby-lang.org/en/master/ENV.html

Ruby Guides On Environment Variables: https://www.rubyguides.com/2019/01/ruby-environment-variables

Using Environment Variables In Heroku

If you want to use Environment Variables on heroku, you can do it right away from the dashboard or you can use heroku CLI. If you wan to do it from the dashboard, you can navigate to you app’s settings tab, and then click on ‘Reveal Config Vars’ in the config vars section. You can now add, remove or modify any config vars from here.

heroku config vars
heroku config vars

Using the CLI

And if you wanted to do it from the CLI, make sure to login at first, with heroku login command. After logging in, you can run heroku config to view the config/ENV vars associated with you app on heroku.

heroku env config
heroku env config

Adding New Vars

To add any new config/ENV vars, you can use heroku config:set with your desired variables. For example;

heroku config:set YOUR_VAR_KEY_NAME:YOUR_VAR_VALUE

heroku config:set S3_BUCKET_NAME:my_bucket123
heroku config-set
heroku config-set

Removing Vars

To remove any existing config/ENV vars, you can use heroku config:unset. For example;

heroku config:unset YOUR_VAR_KEY_NAME

heroku config:unset S3_BUCKET_NAME
heroku config unset
heroku config unset

 

Using ENV Vars in Rails

After adding config/ENV vars on heroku, you’ll have to configure rails to use those vars. Let me show you a few examples.

Database Credentials

To use database credentials; you’ll have to change a few lines in your database.yml config file, located in config/database.yml. You can add your database credentials like the one you can see below. Make sure there’s no typo both in your heroku config vars as well as in your config file. If you’re using heroku postgres; user DATABASE_URL.

Heroku Postgres Guide: https://devcenter.heroku.com/articles/heroku-postgresql

#File: config/database.yml
##...Other Configs
production:
adapter: postgresql
host: <%= ENV['DB_HOST'] %>
port: <%= ENV['DB_PORT'] %>
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASS'] %>

##...Other Configs

Storage Credentials

Same goes for storage too, here i’m using active storage, so this is how my storage config looks like. I have already added my config vars in heroku ACCESS_KEY ACCESS_KEY_ID BUCKET_NAMEand so on.

Active Storage Guide: https://edgeguides.rubyonrails.org/active_storage_overview.html

#File: config/storage.yml
##...Other Configs

digitalocean:
service: S3
access_key_id: <%= ENV['ACCESS_KEY'] %>
secret_access_key: <%= ENV['SECRET_KEY'] %>
region: <%= ENV['REGION'] %>
bucket: <%= ENV['BUCKET_NAME'] %>
endpoint: <%= ENV['ENDPOINT'] %>

##...Other Configs

SMTP Credentials

And for sending SMTP mails, i use sendgrid, if you’d also like to use sendgrid you can add it from heroku elements marketplace. After adding sendgrid from heroku elements, those ENV vars will automatically be added to your app, SMTP_SERVER, SMTP_PORT, etc. Please note: there may be slight changes in the config names, as i modified a few of them, please make sure to add the correct ones; check them in your heroku config vars, from the dashboard.

Action Mailer Guide: https://guides.rubyonrails.org/action_mailer_basics.html

Sendgrid Guide: https://devcenter.heroku.com/articles/sendgrid

#File: config/environments/production.rb
##...Other Configs
config.action_mailer.smtp_settings = {
:address => ENV['SMTP_SERVER'],
:port => ENV['SMTP_PORT'],
:user_name => ENV['SMTP_USERNAME'],
:password => ENV['SMTP_PASSWORD'],
:authentication => :login,
:tls => :true,
:enable_starttls_auto => true
}

##...Other Configs

Final Thoughts

So, that was all about Environment Variables on Heroku. While adding new Environment Variables, please make sure that there’s no typo and also make sure that your rails app is configured properly to use those Environment Variables. And, If you face any issues, please make sure to leave a comment below. So that’s all for today, have a nice day.

Leave a Reply Cancel reply

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

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT