Skip to content

Commit a483130

Browse files
committed
add error handling for entityalreadyexists
1 parent 1736d42 commit a483130

File tree

1 file changed

+14
-6
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/src

1 file changed

+14
-6
lines changed

aws_sra_examples/solutions/genai/bedrock_org/lambda/src/sra_iam.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ def create_role(self, role_name: str, trust_policy: dict, solution_name: str) ->
104104
Dictionary output of a successful CreateRole request
105105
"""
106106
self.LOGGER.info("Creating role %s.", role_name)
107-
return self.IAM_CLIENT.create_role(
108-
RoleName=role_name, AssumeRolePolicyDocument=json.dumps(trust_policy), Tags=[{"Key": "sra-solution", "Value": solution_name}]
109-
)
107+
try:
108+
return self.IAM_CLIENT.create_role(
109+
RoleName=role_name, AssumeRolePolicyDocument=json.dumps(trust_policy), Tags=[{"Key": "sra-solution", "Value": solution_name}]
110+
)
111+
except ClientError as error:
112+
if error.response["Error"]["Code"] == "EntityAlreadyExists":
113+
self.LOGGER.info(f"{role_name} role already exists!")
110114

111115
def create_policy(self, policy_name: str, policy_document: dict, solution_name: str) -> CreatePolicyResponseTypeDef:
112116
"""Create IAM policy.
@@ -120,9 +124,13 @@ def create_policy(self, policy_name: str, policy_document: dict, solution_name:
120124
Dictionary output of a successful CreatePolicy request
121125
"""
122126
self.LOGGER.info(f"Creating {policy_name} IAM policy")
123-
return self.IAM_CLIENT.create_policy(
124-
PolicyName=policy_name, PolicyDocument=json.dumps(policy_document), Tags=[{"Key": "sra-solution", "Value": solution_name}]
125-
)
127+
try:
128+
return self.IAM_CLIENT.create_policy(
129+
PolicyName=policy_name, PolicyDocument=json.dumps(policy_document), Tags=[{"Key": "sra-solution", "Value": solution_name}]
130+
)
131+
except ClientError as error:
132+
if error.response["Error"]["Code"] == "EntityAlreadyExists":
133+
self.LOGGER.info(f"{policy_name} policy already exists!")
126134

127135
def attach_policy(self, role_name: str, policy_arn: str) -> EmptyResponseMetadataTypeDef:
128136
"""Attach policy to IAM role.

0 commit comments

Comments
 (0)