You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def ensure_iam_role(role_name, role_description, role_policy_document):
try:
# Check if the role already exists
logger.info(f"Checking if IAM role '{role_name}' exists...")
response = iam_client.get_role(RoleName=role_name)
logger.info(f"Role '{role_name}' already exists. Skipping creation.")
return True # Role exists
except iam_client.exceptions.NoSuchEntityException:
# Role does not exist, proceed to create it
try:
logger.info(f"Role '{role_name}' does not exist. Creating a new one...")
create_response = iam_client.create_role(
Path='/',
RoleName=role_name,
Description=role_description,
AssumeRolePolicyDocument=role_policy_document
)
logger.debug(f"Got response from IAM client for creating role: {create_response}")
logger.info(f"Role create response code: {create_response['ResponseMetadata']['HTTPStatusCode']}")
return True # Role created successfully
except Exception as e:
logger.error(f"Error occurred while creating role: {e}")
return False # Role creation failed
except Exception as e:
# Handle other exceptions from get_role
logger.error(f"Unexpected error occurred while checking role: {e}")
return False
`
Check if IAM role already exists and then skip creating a new one.
The text was updated successfully, but these errors were encountered: