Although our application has been deployed and is running, we clearly have a problem in that we haven't run database migrations, which is a required deployment task. We already have dealt with running another deployment task, which is to collect static files, however database migrations should only be run as a single deployment task per deployment. For example, if you are deploying multiple instances of your service, you don't want to run migrations for each instance you deploy, you just want to run migrations once per deployment, regardless of the number of instances that are in service.
One obvious solution is to manually run migrations after each deployment, however ideally you want to fully automate your deployments and ensure you have a mechanism to automatically run migrations. CloudFormation does not provide a resource that allows you run one-off ECS tasks, however an extremely powerful feature of CloudFormation is the ability to create your own custom resources, which allow you to perform custom provisioning tasks. A benefit of creating custom resources is that you can incorporate custom provisioning tasks into the workflow of deploying various AWS services and resources, using the CloudFormation framework to manage this for you.
Let's now learn how you can create a simple ECS task runner custom resource that will run the migrate task as part of creating and updating your application environment via CloudFormation.