-
Notifications
You must be signed in to change notification settings - Fork 7
migrations assume role support #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edwardsb
wants to merge
1
commit into
main
Choose a base branch
from
edwardsb-migration-assume-role
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,57 @@ | ||
# Migrations addon | ||
This addon enables automatic migrations for Fleet. | ||
Due to limitations in the AWS provider for Terraform, only Linux platforms are supported. | ||
This module uses the local-exec provisioner to call aws-cli to complete migrations. | ||
Due to this, the following commands must be available to the shell: | ||
- aws | ||
# 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: | ||
|
||
1. Scaling down the main Fleet application ECS service. | ||
2. 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`). | ||
3. Scaling the main Fleet application ECS service back up once the migration is complete. | ||
|
||
## Usage | ||
|
||
```hcl | ||
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 | ||
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" | ||
|
||
# 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 | ||
|
||
1. When `var.task_definition_revision` changes, Terraform triggers the `null_resource`. | ||
2. The `local-exec` provisioner executes the `migrate.sh` script located within the module's directory. | ||
3. 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. | ||
4. The `migrate.sh` script (which you must provide and maintain) performs the migration steps against the Fleet database, using the provided parameters to interact with AWS ECS. | ||
|
||
## Prerequisites | ||
|
||
* **`bash` shell:** 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_arn` is provided. | ||
* **`migrate.sh` script:** A script named `migrate.sh` *must* 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 by `assume_role_arn`) require sufficient IAM permissions to manage the specified ECS services and tasks. | ||
* **State:** The `null_resource` uses the `task_definition_revision` in its `triggers` map. This ensures that Terraform re-runs the provisioner if (and only if) the revision number changes between applies. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if we document some permission set to execute the migration script. Also which sources it should taget. Potentially this module could provide the role even? Given that the migrate.sh script can change and depends on certain resources.