When I set and used Role's policy in python, the following error occurred and I was addicted to it for an hour.
botocore.errorfactory.MalformedPolicyDocumentException: An error occurred (MalformedPolicyDocument) when calling the PutRolePolicy operation: The policy failed legacy parsing
The program used is as follows.
# coding:utf-8
# !/usr/bin/python
import boto3
iamClient = boto3.client('iam')
policy = '''
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*"
],
"Resource": [
"*"
]
}
]
}
'''
iamClient.put_role_policy(
RoleName='MyRole',
PolicyName='Policy_Of_MyRole',
PolicyDocument=policy
)
Apparently, the cause was that the first line of the here document had a line break. After changing the policy setting line as shown below, it can be set normally.
policy = '''{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*"
],
"Resource": [
"*"
]
}
]
}
'''