Skip to content

Commit f799c60

Browse files
Batch: Allow using IAM instance profile name when creating compute env
1 parent ec3224f commit f799c60

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

moto/batch/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,10 @@ def _validate_compute_resources(self, cr: Dict[str, Any]) -> None:
13441344
"Error executing request, Exception : Instance role is required."
13451345
)
13461346
for profile in self.iam_backend.get_instance_profiles():
1347-
if profile.arn == cr["instanceRole"]:
1347+
if (
1348+
profile.arn == cr["instanceRole"]
1349+
or profile.name == cr["instanceRole"]
1350+
):
13481351
break
13491352
else:
13501353
raise InvalidParameterValueException(

tests/test_batch/test_batch_compute_envs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99

1010

1111
@mock_aws
12-
def test_create_managed_compute_environment():
12+
@pytest.mark.parametrize("use_arn_for_instance_role", [True, False])
13+
def test_create_managed_compute_environment(use_arn_for_instance_role):
1314
ec2_client, iam_client, ecs_client, _, batch_client = _get_clients()
1415
_, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client)
1516

17+
instance_role = (
18+
iam_arn.replace("role", "instance-profile")
19+
if use_arn_for_instance_role
20+
else iam_arn.split("/")[-1]
21+
)
1622
compute_name = str(uuid4())
1723
resp = batch_client.create_compute_environment(
1824
computeEnvironmentName=compute_name,
@@ -28,7 +34,7 @@ def test_create_managed_compute_environment():
2834
"subnets": [subnet_id],
2935
"securityGroupIds": [sg_id],
3036
"ec2KeyPair": "string",
31-
"instanceRole": iam_arn.replace("role", "instance-profile"),
37+
"instanceRole": instance_role,
3238
"tags": {"string": "string"},
3339
"bidPercentage": 123,
3440
"spotIamFleetRole": "string",

0 commit comments

Comments
 (0)