Skip to content

Commit cf96ad9

Browse files
author
Sid Madipalli
committed
Adding condition for IAM resource
1 parent b01c455 commit cf96ad9

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

samtranslator/model/sam_resources.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: P
394394
intrinsics_resolver,
395395
get_managed_policy_map,
396396
)
397-
self._make_lambda_role(lambda_function, intrinsics_resolver, execution_role, resources)
397+
self._make_lambda_role(lambda_function, intrinsics_resolver, execution_role, resources, conditions)
398398

399399
try:
400400
resources += self._generate_event_resources(
@@ -418,6 +418,7 @@ def _make_lambda_role(
418418
intrinsics_resolver: IntrinsicsResolver,
419419
execution_role: IAMRole,
420420
resources: List[Any],
421+
conditions: Dict[str, Any],
421422
) -> None:
422423
lambda_role = lambda_function.Role
423424

@@ -432,8 +433,17 @@ def _make_lambda_role(
432433
role_resolved_value = intrinsics_resolver.resolve_parameter_refs(self.Role)
433434
role_list = role_resolved_value.get("Fn::If")
434435

435-
# both are none values then we need to create a role
436-
if is_intrinsic_no_value(role_list[1]) and is_intrinsic_no_value(role_list[2]):
436+
is_both_intrinsic_no_values = is_intrinsic_no_value(role_list[1]) and is_intrinsic_no_value(role_list[2])
437+
438+
# When either one of the condition is a non no value we need to conditionally
439+
# create IAM role, This requires generating a condition that negates the condition check
440+
# passed for IAM role creation and use that for the new role being created
441+
if not is_both_intrinsic_no_values:
442+
execution_role.set_resource_attribute("Condition", f"NOT{role_list[0]}")
443+
conditions[f"NOT{role_list[0]}"] = make_not_conditional(role_list[0])
444+
445+
# both are none values, we need to create a role
446+
if is_both_intrinsic_no_values:
437447
lambda_function.Role = execution_role.get_runtime_attr("arn")
438448

439449
# first value is none so we should create condition ? create : [2]

tests/translator/output/aws-cn/function_with_iam_role.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"Conditions": {
3+
"NOTRoleExists": {
4+
"Fn::Not": [
5+
{
6+
"Condition": "RoleExists"
7+
}
8+
]
9+
},
310
"RoleExists": {
411
"Fn::Not": [
512
{
@@ -52,6 +59,7 @@
5259
"Type": "AWS::Lambda::Function"
5360
},
5461
"MinimalFunctionRole": {
62+
"Condition": "NOTRoleExists",
5563
"Properties": {
5664
"AssumeRolePolicyDocument": {
5765
"Statement": [

tests/translator/output/aws-us-gov/function_with_iam_role.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"Conditions": {
3+
"NOTRoleExists": {
4+
"Fn::Not": [
5+
{
6+
"Condition": "RoleExists"
7+
}
8+
]
9+
},
310
"RoleExists": {
411
"Fn::Not": [
512
{
@@ -52,6 +59,7 @@
5259
"Type": "AWS::Lambda::Function"
5360
},
5461
"MinimalFunctionRole": {
62+
"Condition": "NOTRoleExists",
5563
"Properties": {
5664
"AssumeRolePolicyDocument": {
5765
"Statement": [

tests/translator/output/function_with_iam_role.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"Conditions": {
3+
"NOTRoleExists": {
4+
"Fn::Not": [
5+
{
6+
"Condition": "RoleExists"
7+
}
8+
]
9+
},
310
"RoleExists": {
411
"Fn::Not": [
512
{
@@ -52,6 +59,7 @@
5259
"Type": "AWS::Lambda::Function"
5360
},
5461
"MinimalFunctionRole": {
62+
"Condition": "NOTRoleExists",
5563
"Properties": {
5664
"AssumeRolePolicyDocument": {
5765
"Statement": [

0 commit comments

Comments
 (0)