Skip to main content

Database & VPCs

Any database supported by Ruby or Rails can be used with Lambda assuming your VPC/Network allows those connections. This guide will not get into the details on how to setup or use various databases options within AWS itself like RDS, Aurora, or DynamoDB. However, we will address a few high level topics along with some conventions in our cookiecutter project.

Our Cookiecutter

Our Quick Start project does not create a database but it does have a MySQL service attached to the dev container to faciliate quickly iterating toward using one. The two key files' snippets are below. If you decide to use switch to a different database like PostgreSQL, make adjustments to these files and your Gemfile as needed.

default: &default
adapter: mysql2
username: root
password: <%= ENV["MYSQL_ROOT_PASSWORD"] %>
host: <%= ENV.fetch("MYSQL_HOST") { "localhost" } %>
- MYSQL_HOST=mysql

VPC Configuration

Most Rails applications within AWS are deployed to a private subnet(s) within a VPC which allows you to have direct network access to your relational database. For most folks, this is the default VPC which means finding your subnet ids and security groups are fairly easy. Once you have those, add this VpcConfig to your project's template.yaml file within the existing globals section.

- subnet-09792e6cd06dd59ad
- subnet-0501f3136415021da
- sg-07be99aff5fb14557

Adding it here will ensure every function within your stack has a common VPC setting. Using a VpcConfig should automatically add the AWSLambdaVPCAccessExecutionRole managed policy to your Lambda's execution role. If not, you can manually add it to your Policies section.

- AWSLambdaVPCAccessExecutionRole

Database Migrations

Please see the Running Tasks guide on how to use Lamby's task runner for migrations or other on-demand tasks like Rake.

Using DynamoDB

In some cases Rails with DynamoDB is an excellent choice. If this sounds right for you, I highly recommend using the Aws::Record gem which leverages the aws-sdk-dynamodb in a very Rails like ActiveModel way. Please share your stories with us.