in notebooks/iot_greengress/health_tracker/lambda_setup.py [0:0]
def _create_lambda_policies(assume_role_policy_doc, func_name, lambda_dir,
role_name, role_policy):
iam = boto3.client('iam')
role_arn = ''
try:
tf = lambda_dir + '/' + assume_role_policy_doc
with open(tf) as trust_file:
trust = json.dumps(json.load(trust_file))
resp = iam.create_role(RoleName=role_name,
# Path=dir_path+'/',
AssumeRolePolicyDocument=trust)
role_arn = resp['Role']['Arn']
logging.info('created iam role:{0} with arn:{1}'.format(
role_name, role_arn))
except ClientError as ce:
if ce.response['Error']['Code'] == 'EntityAlreadyExists':
logging.warning(
"Role '{0}' already exists. Using existing Role".format(
role_name))
role = iam.get_role(RoleName=role_name)
role_arn = role['Role']['Arn']
else:
logging.error("Unexpected Error: {0}".format(ce))
try:
pf = lambda_dir + '/' + role_policy
with open(pf) as policy_file:
policy = json.dumps(json.load(policy_file))
resp = iam.put_role_policy(RoleName=role_name,
PolicyName=func_name + '_policy',
PolicyDocument=policy)
except ClientError as ce:
if ce.response['Error']['Code'] == 'EntityAlreadyExists':
logging.warning("Policy '{0}' already exists.".format(role_name))
else:
logging.error("Unexpected Error: {0}".format(ce))
return role_arn