fix: make IAM role optional for AWS Batch job submission#3211
Conversation
On EC2 compute environments that use instance profiles, jobRoleArn is not required. The AWS Batch API documents it as optional, but Metaflow raised unconditionally when iam_role was None. Two changes: - Remove the unconditional raise in BatchJob.execute() when iam_role is None - Make jobRoleArn conditional in _register_job_definition so None is not passed to boto3 (which rejects None for string fields) Closes Netflix#3208
Greptile SummaryMakes
Confidence Score: 5/5Safe to merge — the production change is a small, targeted removal of a guard that was incorrectly mandatory, with no other code paths affected. Both changed lines in No files require special attention. The test file quality issues were flagged in prior review threads. Important Files Changed
Reviews (3): Last reviewed commit: "style: fix pre-commit formatting (black,..." | Re-trigger Greptile |
|
|
||
|
|
||
| def test_job_definition_omits_job_role_arn_when_none(): | ||
| """ | ||
| When job_role is None, jobRoleArn should not be present in the | ||
| job definition at all. boto3 rejects None for string fields. | ||
| """ | ||
| from pathlib import Path | ||
|
|
||
| batch_client_path = Path(__file__).resolve().parents[2] / ( | ||
| "metaflow/plugins/aws/batch/batch_client.py" | ||
| ) | ||
| source_text = batch_client_path.read_text() | ||
|
|
||
| assert "jobRoleArn" in source_text, ( | ||
| "jobRoleArn reference not found in batch_client.py" | ||
| ) | ||
| # The fix uses conditional dict unpacking: **({"jobRoleArn": ...} if ... else {}) | ||
| # If jobRoleArn is assigned unconditionally, this pattern won't be present | ||
| assert "if job_role" in source_text, ( | ||
| "jobRoleArn is not conditionally included — it will be passed as None " | ||
| "to boto3, which rejects None for string fields" | ||
| ) No newline at end of file |
There was a problem hiding this comment.
test_job_definition_omits_job_role_arn_when_none validates behavior by parsing source text rather than exercising the code. The assert "if job_role" in source_text assertion is brittle: a semantically equivalent rewrite such as if job_role is not None or if bool(job_role) would make this test fail even though the behavior is correct — and a stray if job_role: elsewhere in the file would keep it green even if the fix were reverted. Consider mocking boto3 (or the Batch client) and directly calling _register_job_definition() with job_role=None, then asserting that "jobRoleArn" is absent from the payload passed to register_job_definition.
|
Hi odncode, I think you are opening too many PRs. It would be better if you discuss the feature/bug first with the maintainers before opening a PR. |
|
Hi @LuisJG8, Thank you for the feedback. Completely Understood. My apologies for the noise. I'll make sure to discuss on the issue first before opening PRs going forward. Happy to close any of the open ones you'd prefer not to review right now. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3211 +/- ##
=========================================
Coverage ? 28.83%
=========================================
Files ? 381
Lines ? 52465
Branches ? 9259
=========================================
Hits ? 15129
Misses ? 36323
Partials ? 1013 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Signed-off-by: odncode <nnajiodera2@gmail.com>
Summary
Fixes #3208 —
@batchsteps fail with "No IAM role specified" on EC2 compute environments that use instance profiles.Problem
BatchJob.execute()raises unconditionally wheniam_roleisNone(L82-85 inbatch_client.py). Additionally,_register_job_definitionpasses"jobRoleArn": Nonetoboto3, which rejectsNonefor string fields.The AWS Batch ContainerProperties API documents
jobRoleArnas optional. EC2 compute environments can use instance profiles for container credentials instead.Changes Made
BatchJob.execute()jobRoleArnconditional in_register_job_definition— only included whenjob_roleis notNoneTesting
Added
test/unit/test_batch_optional_iam_role.pywith three tests:test_execute_does_not_raise_when_iam_role_is_none— verifies the fixtest_execute_still_requires_image— ensures docker image check remainstest_job_definition_omits_job_role_arn_when_none— verifiesjobRoleArnis conditionalVerified: tests PASS with fix, FAIL without it.
Closes #3208