Run Flyway database migrations as a Lambda function.
Run Flyway database migrations as a Lambda function.
ℹ️ This fork of the (seemingly abandoned) flyway-awslambda project introduces
the following improvements:
- Flyway can be configured via the officially supported environment variables (only config file was supported before)
- All Flyway configuration options are supported (only a small subset was supported before)
- Upgrade Flyway to version 6 (was 4)
- Improved documentation and examples
Other than possible breaking changes between Flyway 4 vs. 6 (I couldn’t identify any) the Lambda function itself is
100% backwards compatible with the original project, i.e. the JAR can be put in place of the old function without breaking changes.
ℹ️ CloudFormation samples can be found under
./src/main/aws/
. Read on for the manual setup.
First, create a new Lambda function and upload the flyway-awslambda-x.y.z.jar file as code. You can either download a pre-built
JAR file from the releases or build it yourself (Scala is required):
sbt assembly
Value | |
---|---|
Bucket | Your Flyway migration bucket, e.g. “my-flyway” |
Event type | Object created |
Prefix | The Flyway migration files location, e.g. “my-application/“ |
Suffix | sql |
The Lambda function expects the S3 bucket to be structured like this:
s3://my-flyway <- Flyway migration bucket.
- /my-application <- Flyway resource folder(prefix).
- flyway.conf <- Flyway configuration file (optional)
- V1__create_foo.sql <- SQL file(s)
- V2__create_bar.sql
migration-result.json
in the S3 bucket for the results or CloudWatch logs for more detail.
{
"bucket_name": "my-flyway",
"prefix": "my-application",
"flyway_conf": "flyway.conf" // Optional. Default 'flyway.conf'
}
migration-result.json
in the S3 bucket for the results or CloudWatch logs for more detail.The Lambda function can be configured either by way of a Flyway configuration file in the S3 bucket (1) or with
environment variables (2). In case no configuration file is found it is assumed that the function should be configured
via environment variables. Please note that the configuration method is a binary choice — you can only pick one.
Configuration file: For this configuration method place a Flyway compliant config file
in the application folder along with the migration scrips.
Environment variables: To configure the Lambda function via env vars it is crucial that no config
file is present in the bucket. The Lambda function passes the FLYWAY_*
env vars directly through to the library. See
the official documentation for a list of supported env vars.
Only MySQL is supported out of the box. If another database is used it is recommended to make use of Lambda Layers.
Here’s how to:
java/lib/postgresql-42.2.6.jre7
An example CloudFormation template demonstrating a PostgreSQL setup via Lambda layers can be found in /src/main/aws/3-postgres.yaml.