-
Notifications
You must be signed in to change notification settings - Fork 2
Database Migration Management
Basil Vandegriend edited this page Dec 13, 2022
·
3 revisions
As per the FAM Architecture page, FAM for its database will use AWS RDS provisioned using Terraform. The FAM team manages database migrations in an automated fashion using Github Actions and Terraform.
- The database structure (database objects like tables, views, etc.) can change over time, both during development and especially after production go-live.
- Data changes need to be handled. This can be reference data (e.g. code sets) or test data.
- Automation should be smart (aka idempotent, desired state configuration) in that it only applies changes that are not yet made for a particular environment. Rerunning the same automation a second time would result in no changes.
- A method of rolling back bad changes is required, ideally using a fail-safe approach like database snapshots.
- There is a general 'DevOps' requirement to be able to provision new environments in a fully-automated fashion and apply updates to these environments as part of the CD pipeline.
- Prefer the use of open source tooling that is actively supported and broadly used within the BC government.
RDS runs inside a VPC and is not meant to expose connections to the general internet. Therefore there is no database connectivity available from the Github Actions runner environment nor from the Terraform Cloud runner environment. So any database scripts must be run from a component that is inside the VPC and has privileges to connect to the database.
Options:
- Provision a temporary bastion host and connect via SessionManager
- Use an ECS task which runs a script
- Create a lambda function inside the VPC and give it permissions to connect to the database
After a fairly extensive spike, we decided to run a lambda function that could be invoked as a step in our normal pipeline.
- Terraform provisions RDS Aurora (PostgreSQL) using a sysadmin owner and a random password. The password is stored in an AWS Secret.
- Terraform provisions a lambda function that lives in the VPC. The lambda function comes from an open source project called flyway-lambda. The lambda function has permissions to access the AWS Secret for the sysadmin username and password.
- Terraform provisions a database secret that contains the username and a randomly generated password for the API user (this will be used later in sql scripts that create the user in the database).
- The deploy pipeline creates a database snapshot from a terraform job each time code is pushed (deleting the one from the previous deployment).
- The deploy pipeline invokes the flyway-lambda function with the right parameters each time code is pushed. In particular, the terraform job that invokes the lambda function passes any necessary variables to the sql scripts (example: username and password of the api database user).
- The flyway-lambda function downloads the sql scripts from github and applies them in the right order in the database (replacing any variables as necessary).
- The flyway-lambda function updates its own state table in the database to keep track of which migrations have been run.
- Check out bcgov/nr-forests-access-management
- Change to the dev branch
- Create a new local branch from dev
- Add a new file in the directory "server/flyway/sql". Follow the flyway naming conventions. If the change adds a database table, you will need to add a grant statement to grant access to ${api_db_username}.
- Check in your change locally and push to a new branch in Github
- Create a pull request from the new Github branch back to the dev branch
- Wait for checks to pass and reviews to be approved
- Squash & merge the change to the dev branch
- Github actions will run a Terraform Cloud job which will invoke Flyway to execute your script
- Eventually, dev will be merged to test and prod with pull requests, and the same scripts will be run in those environments
- Environment Management
- Release Management
- Creating a Release
- Database Backups and Restores
- OIDC Client Testing
- FAM Onboarding Ops Guide
- Setup AWS CloudWatch
- Setup AWS EC2 instance to connect to RDS Postgres Database
- Technical Troubleshooting
- Managing Terraform State
- Enable Cloudwatch Logs for API Gateway
- Update AWS CloudFront Certificate
- Verify IDIM BCeID Client SOAP Web Service