Scott's Recipes Logo

Adding an Environment Variable to Kamal


Please note that all opinions are that of the author.


Last Updated On: 2025-10-13 07:27:39 -0400

This documents the process for adding an environment variable to the Polly / PollitifyHub application.

Step 1: Set the Value of the Environment Variable in .env.production

This requires adding a value in the root directory of the app like this:

export AWS_ACCESS_KEY_ID=not_a_real_value_but_would_be_real_if_this_was_not_a_blog_post_

Step 2: Add the Environment Var to .kamal/secrets

This requires adding a line like this:

AWS_REGION=$AWS_REGION

Step 3

this requires editing deploy.yml and adding the environment variables to the registry so they are copied up to the server at deploy time:

env:
  secret:
    - AWS_ACCESS_KEY_ID
    - AWS_SECRET_ACCESS_KEY
    - AWS_REGION
    - (any additional vars here)

Step 4: Deploy

Run a deploy. This can be done with the native Kamal commands or our application level shell script:

./deploy

Step 5: Verify

Log into the server and verify that the new environment variable is accessible within the Rails console:

bin/ssh
bin/rails c 
polly(prod)> ENV['AWS_SECRET_ACCESS_KEY']
=> "an_actual_env_value_is_here_not_nil"

Thank You

As with most of my deploy land adventures, my good friend Nick Janetakis provided inspiration and assistance along the way. Thanks Nick!