2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

put_role_policyするとMalformedPolicyDocumentExceptionが発生した

Posted at

pythonでRoleのpolicyを設定使用としたところ以下のエラーが発生して小一時間ハマりました。

botocore.errorfactory.MalformedPolicyDocumentException: An error occurred (MalformedPolicyDocument) when calling the PutRolePolicy operation: The policy failed legacy parsing

使用したプログラムは次のようなものです。

# 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
)

原因

どうやらヒアドキュメントの1行目が改行になってしまっていることが原因のようでした。
以下のようにpolicyの設定行を変更したところ正常に設定できるようになりました。

policy = '''{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "mobileanalytics:PutEvents",
                "cognito-sync:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
'''
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?