Skip to content

Conversation

@SameerMesiah97
Copy link
Contributor

Description

Added best-effort cleanup to EC2CreateInstanceOperator to ensure EC2 instances are terminated when failures occur after successful instance creation.

Previously, the operator could create an EC2 instance via RunInstances and then fail during post-creation steps (for example, when waiting for the instance with wait_for_completion=True and DescribeInstances permissions are missing). In these cases, the task failed while leaving the EC2 instance running.

The operator now attempts to terminate any created instances if an exception is raised after instance creation. Cleanup is performed opportunistically and does not mask or replace the original exception if termination fails.

Rationale

EC2CreateInstanceOperator manages the lifecycle of an external infrastructure resource. If instance creation succeeds but subsequent steps fail, leaving the instance running is both surprising and potentially costly.

Failures after instance creation can occur for multiple reasons, including IAM permission errors (for example, missing DescribeInstances) as well as loss of access to observability or metadata systems used during task execution. In all of these cases, the operator has failed to complete successfully from Airflow’s perspective, and execution state may no longer be reliable.

Attempting best-effort cleanup in these scenarios avoids leaving unmanaged EC2 instances running when the task itself has failed, and aligns with the behavior of other Airflow operators that manage external resources. Cleanup failures are logged and do not mask the original exception, preserving existing failure semantics while improving safety.

Tests

  • Added a unit test verifying that EC2 instances are terminated when a failure occurs after instance creation (simulated via a waiter error).
  • Added a unit test ensuring that failures during cleanup do not mask or override the original exception raised by the operator.

Backwards Compatibility

No changes to the public API or operator parameters.

Closes: #60903

…ures

occurred after successful instance creation (e.g. waiter failures due to missing
DescribeInstances permissions).

This change adds best-effort cleanup when post-creation steps fail by attempting
to terminate created instances. Cleanup errors are logged but do not mask the
original exception.

Tests cover successful cleanup on failure and ensure cleanup failures do not
override the original error.
@boring-cyborg boring-cyborg bot added area:providers provider:amazon AWS/Amazon - related issues labels Jan 21, 2026
@eladkal eladkal requested a review from vincbeck January 21, 2026 21:38
Copy link
Contributor

@vincbeck vincbeck left a comment

Choose a reason for hiding this comment

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

Makes sense to me

@vincbeck vincbeck merged commit 88f1645 into apache:main Jan 22, 2026
89 checks passed
@SameerMesiah97 SameerMesiah97 changed the title EC2CreateInstanceOperator could leave EC2 instances running when fail… EC2CreateInstanceOperator could leave EC2 instances running when failing Jan 24, 2026
suii2210 pushed a commit to suii2210/airflow that referenced this pull request Jan 26, 2026
…ures (apache#60904)

occurred after successful instance creation (e.g. waiter failures due to missing
DescribeInstances permissions).

This change adds best-effort cleanup when post-creation steps fail by attempting
to terminate created instances. Cleanup errors are logged but do not mask the
original exception.

Tests cover successful cleanup on failure and ensure cleanup failures do not
override the original error.

Co-authored-by: Sameer Mesiah <smesiah971@gmail.com>
shreyas-dev pushed a commit to shreyas-dev/airflow that referenced this pull request Jan 29, 2026
…ures (apache#60904)

occurred after successful instance creation (e.g. waiter failures due to missing
DescribeInstances permissions).

This change adds best-effort cleanup when post-creation steps fail by attempting
to terminate created instances. Cleanup errors are logged but do not mask the
original exception.

Tests cover successful cleanup on failure and ensure cleanup failures do not
override the original error.

Co-authored-by: Sameer Mesiah <smesiah971@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EC2CreateInstanceOperator leaks EC2 instance on failure with partial IAM permissions

2 participants