Read secrets using AWS secret manager in Rails
Storing secrets in a version control system is not a good idea.
Rails provides a way to store
credentials
in an encrypted manner.
In such approach,
we need to just store secret key (called as master.key
)
separately which is used to decrypt credentials.
AWS has released a feature called AWS secret manager. It solves a few problems.
- Storing environment credentials in a secure manner
- Auto rotate secrets using AWS Lambda in an automated manner
- Storage and access of secrets in environment specific manner
Read more about AWS secrets here. To create secrets using AWS secrets manager follow this article
In this article, we will learn how to use secrets created using AWS secret manager and using those in the Rails application.
Step 1: Add aws-sdk-secretsmanager gem
To access services specific to AWS secret manager, add the gem in Rails application.
Step 2: Create secret manager initializer
Create an initializer file in config/initializers
directory.
Let’s name it as secret_manager.rb
Below are a few noteworthy things:
We have created secrets specific to environment name in AWS secret manager. e. g.
And the code below is used to define the secret name based on rails environment.
We receive the json response in lines given below.
secret_hash
is a Hash with
values we have stored in aws secret manager.
Now, we just make those values available in ENV
variable in lines given below.
3. Use secrets from ENV
Now, wherever we need those values in the codebase (e.g. config/database.yml
),
we can directly access them with ENV['DATABASE_HOST']
, ENV['DATABASE_USERNAME']
and so on.
Posting sample database.yml
file below.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox