How to Securely Store RDS Instance Passwords in AWS Secret Manager and Access Them in a Ruby on Rails Application
AWS Secret Manager is a service offered by Amazon Web Services (AWS) that enables you to manage secrets such as database passwords, API keys, and other sensitive data. This blog will discuss the steps to set up AWS Secret Manager.
Step 1: Create a Secret
To create a secret in AWS Secret Manager, follow the steps below:
- Sign in to the AWS Management Console
- Go to the AWS Secret Manager dashboard
- Click “Store a new secret”
- the secret type will be
Credentials for Amazon RDS database
- Enter the secret value
- Add a key-value pair to tag the secret (optional)
- Click “Next”
- Enter a name for the secret
- Choose the encryption key
- Click “Next”
- Review the details and click “Store secret”
Step 2: Access the Secret in ruby on rails application
To access the secret in a Ruby on Rails application, you can use the AWS SDK for Ruby. First, install the SDK by adding the aws-sdk-secretsmanager
gem to your Gemfile and running bundle install
.
In your Rails app, create an initializer file to load the AWS credentials and region from environment variables, and then use the AWS SDK to retrieve the RDS instance password from Secret Manager.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# config/initializers/aws.rb
Aws.config.update({
region: ENV['AWS_REGION'],
credentials: Aws::Credentials.new(
ENV['AWS_ACCESS_KEY_ID'],
ENV['AWS_SECRET_ACCESS_KEY']
)
})
# Retrieve RDS password from Secret Manager
secrets_manager = Aws::SecretsManager::Client.new
secret_value = secrets_manager.get_secret_value({ secret_id: 'my-rds-password' })
RDS_PASSWORD = secret_value.secret_string
or you can fetch the passwords and save it in your environment variables
code is as follows :-
1
2
3
4
5
6
7
8
#decalre region_name and secret_name statically or dynamically
client = Aws::SecretsManager::Client.new(region: region_name)
get_secret_value_response = client.get_secret_value(secret_id: secret_name)
secret_json = get_secret_value_response.secret_string
secret_hash = JSON.parse(secret_json)
ENV['DATABASE_HOST'] = secret_hash['host']
ENV['DATABASE_USERNAME'] = secret_hash['username']
ENV['DATABASE_PASSWORD'] = secret_hash['password']
Conclusion
AWS Secret Manager is a powerful tool for managing sensitive data in your applications. By following the steps outlined in this document, you can easily set up, access, and use secrets in your applications.