Terraform AWS Fleet Database Migration Module
This Terraform module provides a mechanism to trigger database migrations for a Fleet application running on AWS ECS. It is designed to integrate into an infrastructure deployment pipeline, ensuring that database schema changes are applied gracefully, typically during application upgrades.
The core functionality relies on a null_resource which executes a local script (migrate.sh) when specific triggers change (primarily the task_definition_revision). This script is expected to handle the actual migration process, which usually involves:
- Scaling down the main Fleet application ECS service.
- Running a one-off ECS task using the new task definition revision (which contains the updated application code capable of performing the migration). This task executes the necessary Fleet migration command (e.g.,
fleetctl prepare db). - Scaling the main Fleet application ECS service back up once the migration is complete.
Usage
module "fleet_migration" {
source = "./path/to/this/module" # Or Git source
ecs_cluster = "my-fleet-cluster"
ecs_service = "my-fleet-service"
task_definition = "arn:aws:ecs:us-west-2:123456789012:task-definition/my-fleet-app" # Base ARN without revision
task_definition_revision = 5 # The *new* revision to migrate *to*
min_capacity = 0 # Scale down to this during migration
max_capacity = 5 # Set maximum target scale after migration
desired_count = 2 # Scale back up to this after migration
subnets = ["subnet-xxxxxxxxxxxxxxxxx", "subnet-yyyyyyyyyyyyyyyyy"]
security_groups = ["sg-xxxxxxxxxxxxxxxxx"]
# Optional: Specify if a separate vulnerability processing service needs coordination
# vuln_service = "my-fleet-vuln-service"
# Optional: Provide an IAM Role ARN for the local-exec script to assume
# assume_role_arn = "arn:aws:iam::123456789012:role/MyMigrationRole"
# assume_role_session_name = "TerraformFleetMigration"
# Optional: Maximum time in seconds to wait for the migration task (default 600).
# Increase to 1800+ for large production databases with heavy schema migrations.
# timeout_seconds = 600
# Ensure this module depends on the resource that creates/updates the task definition revision
# For example:
# depends_on = [aws_ecs_task_definition.fleet_app]
}
Workflow
- When
var.task_definition_revisionchanges, Terraform triggers thenull_resource. - The
local-execprovisioner executes themigrate.shscript located within the module's directory. - It passes essential AWS and ECS details (region, cluster, service, task definition, revision, network configuration, scaling parameters, optional role ARN) as command-line arguments or environment variables to the script.
- The
migrate.shscript (which you must provide and maintain) performs the migration steps against the Fleet database, using the provided parameters to interact with AWS ECS.
Prerequisites
bashshell: Must be available in the environment where Terraform is executed.- AWS CLI: Must be installed and configured with credentials in the environment where Terraform is executed. The credentials need permissions to perform ECS actions (DescribeServices, UpdateService, RunTask, DescribeTasks) and potentially STS AssumeRole if
assume_role_arnis provided. migrate.shscript: A script namedmigrate.shmust exist within this module's directory (path.module). This script contains the actual logic for scaling services and running the migration task. This module only triggers the script; it does not contain the migration logic itself.- Existing Resources: The specified ECS Cluster, Service, Task Definition (base ARN), Subnets, and Security Groups must exist.
Important Considerations
local-exec: This provisioner runs commands on the machine executing Terraform. Ensure this machine has the necessary tools (bash, AWS CLI) and network access/credentials to interact with your AWS environment. This might require specific configuration in CI/CD pipelines.- IAM Permissions: The credentials used by
local-exec(either default AWS credentials or the assumed role specified byassume_role_arn) require sufficient IAM permissions to manage the specified ECS services and tasks. - State: The
null_resourceuses thetask_definition_revisionin itstriggersmap. This ensures that Terraform re-runs the provisioner if (and only if) the revision number changes between applies.
Requirements
No requirements.
Providers
| Name | Version |
|---|---|
| aws | n/a |
| null | n/a |
Modules
No modules.
Resources
| Name | Type |
|---|---|
| null_resource.main | resource |
| aws_region.current | data source |
Inputs
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| assume_role_arn | ARN of the IAM role to assume for ECS permissions | string |
"" |
no |
| assume_role_session_name | Session name to use when assuming the IAM role | string |
"" |
no |
| desired_count | n/a | number |
n/a | yes |
| ecs_cluster | n/a | string |
n/a | yes |
| ecs_service | n/a | string |
n/a | yes |
| max_capacity | n/a | number |
n/a | yes |
| min_capacity | n/a | number |
n/a | yes |
| security_groups | n/a | list(string) |
n/a | yes |
| subnets | n/a | list(string) |
n/a | yes |
| task_definition | n/a | string |
n/a | yes |
| task_definition_revision | n/a | number |
n/a | yes |
| timeout_seconds | Maximum time in seconds to wait for the migration ECS task to complete. Default 600 (10 min). Increase to 1800+ for large production databases. | number |
600 |
no |
| vuln_service | n/a | string |
"" |
no |
Outputs
No outputs.