Skip to content

Delete task definitions after deregistering during cleanup#331

Closed
jonpspri wants to merge 1 commit intomattclay:mainfrom
jonpspri:ecs_taskdefinition
Closed

Delete task definitions after deregistering during cleanup#331
jonpspri wants to merge 1 commit intomattclay:mainfrom
jonpspri:ecs_taskdefinition

Conversation

@jonpspri
Copy link

Summary

  • Add ecs:DeleteTaskDefinitions permission to the paas policy
  • Update Ecs terminator to call delete_task_definitions after deregistering, fully removing task definitions instead of leaving them in INACTIVE state
  • Batches deletions in groups of 10 (API limit)

Test plan

  • Verify stale ECS task definitions are fully deleted during cleanup runs

Now that ecs:DeleteTaskDefinitions is available, fully remove task
definitions instead of leaving them in INACTIVE state after
deregistering.
Copilot AI review requested due to automatic review settings February 12, 2026 14:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances ECS cleanup by adding IAM permission and extending the ECS terminator to fully delete task definition revisions (rather than leaving them INACTIVE) after deregistration.

Changes:

  • Add ecs:DeleteTaskDefinitions to the PaaS IAM policy.
  • Update ECS terminator cleanup to call delete_task_definitions in batches of 10 after deregistering task definitions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
aws/terminator/paas.py Adds batched deletion of task definitions after deregistration during ECS cluster cleanup.
aws/policy/paas.yaml Grants ecs:DeleteTaskDefinitions needed for the new cleanup behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +290 to +295
# Deregister and delete task definitions
task_definitions = _paginate_task_definition_results()
for each in task_definitions:
self.client.deregister_task_definition(taskDefinition=each)
for i in range(0, len(task_definitions), 10):
self.client.delete_task_definitions(taskDefinitions=task_definitions[i:i + 10])
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list_task_definitions is account/region-wide (no cluster filter), so this will deregister and now delete every task definition in the account, not just ones used by the cluster being terminated. That can break other ECS clusters/services. Consider scoping the task definitions to those referenced by services/tasks in this cluster (e.g., describe_services / describe_tasks to collect taskDefinition ARNs), and only delete those; also consider listing/deleting only INACTIVE revisions rather than deregistering everything.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.

jonpspri added a commit to jonpspri/aws-terminator that referenced this pull request Feb 12, 2026
Move task definition lifecycle management out of Ecs.terminate() into a
new EcsTaskDefinition(DbTerminator) class. This fixes the blast radius
issue where cleaning up any stale cluster would deregister all task
definitions in the account. Task definitions are now independently
tracked and aged via DynamoDB.

Also adds ecs:DeleteTaskDefinitions to the paas policy and fully deletes
(not just deregisters) stale task definitions. Subsumes PR mattclay#331.
@jonpspri
Copy link
Author

Subsumed by #330 — task definition cleanup has been extracted into its own EcsTaskDefinition terminator class, which includes the DeleteTaskDefinitions permission and API call from this PR.

@jonpspri jonpspri closed this Feb 12, 2026
jonpspri added a commit to jonpspri/aws-terminator that referenced this pull request Feb 17, 2026
Move task definition lifecycle management out of Ecs.terminate() into a
new EcsTaskDefinition(DbTerminator) class. This fixes the blast radius
issue where cleaning up any stale cluster would deregister all task
definitions in the account. Task definitions are now independently
tracked and aged via DynamoDB.

Also adds ecs:DeleteTaskDefinitions to the paas policy and fully deletes
(not just deregisters) stale task definitions. Subsumes PR mattclay#331.
jonpspri added a commit to jonpspri/aws-terminator that referenced this pull request Feb 17, 2026
Add IAM permissions and terminator class to support ecs_service
Service Connect integration tests:

- Add servicediscovery permissions (CreateHttpNamespace, DeleteNamespace,
  GetOperation, ListNamespaces) to networking policy
- Add ELB service-linked role creation permission to paas policy
- Add ServiceDiscoveryHttpNamespace terminator class for cleanup of
  HTTP namespaces created during integration tests

Expand Service Discovery support for all namespace types

Generalize the terminator from HTTP-only to all namespace types
(HTTP, DNS_PUBLIC, DNS_PRIVATE) and delete child services before
deleting namespaces. Add corresponding IAM permissions.

Extract ECS task definition cleanup into its own terminator class

Move task definition lifecycle management out of Ecs.terminate() into a
new EcsTaskDefinition(DbTerminator) class. This fixes the blast radius
issue where cleaning up any stale cluster would deregister all task
definitions in the account. Task definitions are now independently
tracked and aged via DynamoDB.

Also adds ecs:DeleteTaskDefinitions to the paas policy and fully deletes
(not just deregisters) stale task definitions. Subsumes PR mattclay#331.

Split ECS policy into separate paas-ecs policy file
jonpspri added a commit to jonpspri/aws-terminator that referenced this pull request Feb 20, 2026
Add IAM permissions and terminator class to support ecs_service
Service Connect integration tests:

- Add servicediscovery permissions (CreateHttpNamespace, DeleteNamespace,
  GetOperation, ListNamespaces) to networking policy
- Add ELB service-linked role creation permission to paas policy
- Add ServiceDiscoveryHttpNamespace terminator class for cleanup of
  HTTP namespaces created during integration tests

Expand Service Discovery support for all namespace types

Generalize the terminator from HTTP-only to all namespace types
(HTTP, DNS_PUBLIC, DNS_PRIVATE) and delete child services before
deleting namespaces. Add corresponding IAM permissions.

Extract ECS task definition cleanup into its own terminator class

Move task definition lifecycle management out of Ecs.terminate() into a
new EcsTaskDefinition(DbTerminator) class. This fixes the blast radius
issue where cleaning up any stale cluster would deregister all task
definitions in the account. Task definitions are now independently
tracked and aged via DynamoDB.

Also adds ecs:DeleteTaskDefinitions to the paas policy and fully deletes
(not just deregisters) stale task definitions. Subsumes PR mattclay#331.

Split ECS policy into separate paas-ecs policy file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants