Skip to content

Commit 615f202

Browse files
author
Sid Madipalli
committed
Addressing comments
1 parent 83e1f03 commit 615f202

10 files changed

+18
-16
lines changed

samtranslator/model/sam_resources.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -426,36 +426,37 @@ def _make_lambda_role(
426426
resources.append(execution_role)
427427
lambda_function.Role = execution_role.get_runtime_attr("arn")
428428

429-
if is_intrinsic_if(lambda_role):
430-
resources.append(execution_role)
429+
elif is_intrinsic_if(lambda_role):
431430

432431
# We need to create and if else condition here
433-
role_resolved_value = intrinsics_resolver.resolve_parameter_refs(self.Role)
434-
role_list = role_resolved_value.get("Fn::If")
432+
role_resolved_value = intrinsics_resolver.resolve_parameter_refs(lambda_role)
433+
role_condition, role_if, role_else = role_resolved_value.get("Fn::If")
434+
435+
is_both_intrinsic_no_values = is_intrinsic_no_value(role_if) and is_intrinsic_no_value(role_else)
435436

436-
is_both_intrinsic_no_values = is_intrinsic_no_value(role_list[1]) and is_intrinsic_no_value(role_list[2])
437+
# Create a role only when no value is in either of the conditions
438+
if is_intrinsic_no_value(role_if) or is_intrinsic_no_value(role_else):
439+
resources.append(execution_role)
437440

438-
# both are none values, we need to create a role
441+
# both are none values, we always need to create a role
439442
if is_both_intrinsic_no_values:
440443
lambda_function.Role = execution_role.get_runtime_attr("arn")
441444

442445
# first value is none so we should create condition ? create : [2]
443446
# create a condition for IAM role to only create on if case
444-
elif is_intrinsic_no_value(role_list[1]):
447+
elif is_intrinsic_no_value(role_if):
445448
lambda_function.Role = make_conditional(
446-
role_list[0], execution_role.get_runtime_attr("arn"), role_list[2]
449+
role_condition, execution_role.get_runtime_attr("arn"), role_else
447450
)
448-
execution_role.set_resource_attribute("Condition", f"{role_list[0]}")
451+
execution_role.set_resource_attribute("Condition", f"{role_condition}")
449452

450453
# second value is none so we should create condition ? [1] : create
451454
# create a condition for IAM role to only create on else case
452455
# with top level condition that negates the condition passed
453-
elif is_intrinsic_no_value(role_list[2]):
454-
lambda_function.Role = make_conditional(
455-
role_list[0], role_list[1], execution_role.get_runtime_attr("arn")
456-
)
457-
execution_role.set_resource_attribute("Condition", f"NOT{role_list[0]}")
458-
conditions[f"NOT{role_list[0]}"] = make_not_conditional(role_list[0])
456+
elif is_intrinsic_no_value(role_else):
457+
lambda_function.Role = make_conditional(role_condition, role_if, execution_role.get_runtime_attr("arn"))
458+
execution_role.set_resource_attribute("Condition", f"NOT{role_condition}")
459+
conditions[f"NOT{role_condition}"] = make_not_conditional(role_condition)
459460

460461
def _construct_event_invoke_config( # noqa: PLR0913
461462
self,

tests/model/test_sam_resources.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,8 @@ def test_role_fn_if_no_aws_no_value_keeps_original(self):
793793
generated_roles = [x for x in cfn_resources if isinstance(x, IAMRole)]
794794
lambda_function = next(r for r in cfn_resources if r.resource_type == "AWS::Lambda::Function")
795795

796-
self.assertEqual(len(generated_roles), 1)
796+
# Should not create a role if a role is passed in for both cases
797+
self.assertEqual(len(generated_roles), 0)
797798
self.assertEqual(lambda_function.Role, role_conditional)
798799

799800
def test_role_fn_if_both_no_value_creates_execution_role(self):

tests/translator/input/function_with_iam_role_create.yaml renamed to tests/translator/input/function_with_conditional_iam_role_else.yaml

File renamed without changes.

tests/translator/input/function_with_iam_role.yaml renamed to tests/translator/input/function_with_conditional_iam_role_if.yaml

File renamed without changes.

tests/translator/output/aws-cn/function_with_iam_role_create.json renamed to tests/translator/output/aws-cn/function_with_conditional_iam_role_else.json

File renamed without changes.

tests/translator/output/aws-cn/function_with_iam_role.json renamed to tests/translator/output/aws-cn/function_with_conditional_iam_role_if.json

File renamed without changes.

tests/translator/output/aws-us-gov/function_with_iam_role_create.json renamed to tests/translator/output/aws-us-gov/function_with_conditional_iam_role_else.json

File renamed without changes.

tests/translator/output/aws-us-gov/function_with_iam_role.json renamed to tests/translator/output/aws-us-gov/function_with_conditional_iam_role_if.json

File renamed without changes.

tests/translator/output/function_with_iam_role_create.json renamed to tests/translator/output/function_with_conditional_iam_role_else.json

File renamed without changes.

tests/translator/output/function_with_iam_role.json renamed to tests/translator/output/function_with_conditional_iam_role_if.json

File renamed without changes.

0 commit comments

Comments
 (0)