Trying to set an IAM role for cross account access, wherein lambda from management account can run tasks on all other accounts by assuming cross account role. I was able to create the role n the non management account by console, but boto3 gives malformed policy document error.
LambdaCrossAccountRole is the role that has already been created in management account, and this role would need to be assumed by the role being created in non management account
The trust policy has been given as
trust_role="""
{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Principal":{
"AWS":"arn:aws:iam::<management_account_id>:role/LambdaCrossAccountRole"},
"Action":"sts.AssumeRole"}
]}"""
The iam role is being created by
iam-boto3.client("iam")
response_acct=iam.create_role(
RoleName="CrossAccountLambdaRole",
AssumeRolePolicyDocument=json.dumps(trust_role))
Error:
botocore.errorfactory.MalformedPolicyDocumentException: An error occurred(MalformedPolicyDocument) when calling CreateRole operation:This policy contains invalid Json
What maybe reasons for the error? Thanks
From
create_role
documentation:AssumeRolePolicyDocument
should be a string, don't usejson.dumps()
Also update this:
Otherwise it will throw an error: