EC2CreateInstanceOperator could leave EC2 instances running when failing #60904
+116
−24
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.
Description
Added best-effort cleanup to
EC2CreateInstanceOperatorto ensure EC2 instances are terminated when failures occur after successful instance creation.Previously, the operator could create an EC2 instance via
RunInstancesand then fail during post-creation steps (for example, when waiting for the instance withwait_for_completion=TrueandDescribeInstancespermissions 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
EC2CreateInstanceOperatormanages 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
Backwards Compatibility
No changes to the public API or operator parameters.
Closes: #60903