Database & VPCs

Any database supported by Ruby or Rails can be used with Lambda assuming your VPC/Network allows those connections.

Our goal is to cover high level aspects on how to get started using Databases with AWS SAM & Lamby without getting into the various ways to create them. As such, these guides assume you are familiar setting up AWS Resources like RDS, Aurora, or DynamoDB. Likewise, how to use docker-compose for local database services is not covered.

Starter Project Changes

Our Quick Start cookiecutter project uses the --skip-active-record when creating a new Rails project. If you are using our starter, here are the steps you need to perform to enable ActiveRecord. First, open the config/application.rb file and uncomment this line.

require "active_record/railtie"

Add this to your config/environments/development.rb file.

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true

Add this to your config/environments/production.rb file.

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

Create the app/models/application_record.rb base model.

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

Finally, you may need to create a database.yml file matching your target adapter in the config directory. We recommend you create a DATABASE_URL environment variable with SSM Parameter Store using our Environment & Configuration guide.

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 is fairly easy. Once you have those, add this VpcConfig to your project's template.yaml file within the existing globals section.

Globals:
  Function:
    VpcConfig:
      SubnetIds:
        - subnet-09792e6cd06dd59ad
        - subnet-0501f3136415021da
      SecurityGroupIds:
        - sg-07be99aff5fb14557

Adding it here will ensure every function within your stack has a common VPC setting. Doing this in Globals ensures every function, like your ActiveJob ones, will be on the same VPC without duplication. 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.

RailsLambda:
  # ...
  Properties:
    # ..
    Policies:
      - AWSLambdaVPCAccessExecutionRole

Simple MySQL Setup

Using MySQL with Lambda is super easy since we have a precompiled and statically linked libmysqlclient in a gem named mysql2-lambda. Simply add the gem to your Gemfile.

gem 'mysql2-lambda'

Technically the gem above is not needed with container deployment package types since you could install the needed system dependencies via yum in both your project's development and production docker files.

Rails Database Migrations

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

Using Aurora Serverless

⚠️ Aurora Serverless v2 is coming soon.

Is your Rails project is used infrequently with sporadic access patterns? Such applications make great candidates for Aurora Serverless v1. The ActiveRecord Adapter for Amazon Aurora Serverless is a simple ActiveRecord Mysql2 adapter extensions that allow Rails to use AWS Aurora Serverless v1 the Aws::RDSDataService::Client interface.

Using DynamoDB

In some cases Rails with DynamoDB is an excellent choice. Some might say in all cases! 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.

Other Resources

Databases with Lambda is a big fun topic. Here are a few links to help you learn more.