如何为PolicyDocument赋值?

时间:2017-09-14 11:49:52

标签: amazon-web-services boto3

我正在尝试使用AWS Boto3创建策略。我需要知道如何为PolicyDocument赋值。我尝试如下,但没有工作。

import boto3

iamclient = boto3.client('iam')

response = iamclient.create_policy(
    PolicyName='Test',

    PolicyDocument=
{
    "Version": "2012-10-17",
    "Statement":
        [
            {
            "Effect": "Allow",
            "Action":   [
                "sns:GetTopicAttributes",
                "sns:ListTopics",
                "sns:Publish",
                "sns:CreateTopic",
                "sqs:ListQueues"
                        ],
            "Resource": "*"
            }
        ]
},
    Description='Test'

)

1 个答案:

答案 0 :(得分:0)

您需要导入json模块并使用json.dumps(),如下所示。

import boto3, json

iamclient = boto3.client('iam')

policy_json = {
    "Version": "2012-10-17",
    "Statement":
        [
            {
                "Effect": "Allow",
                "Action": [
                    "sns:GetTopicAttributes",
                    "sns:ListTopics",
                    "sns:Publish",
                    "sns:CreateTopic",
                    "sqs:ListQueues"
                ],
                "Resource": "*"
            }
        ]
}

response = iamclient.create_policy(
    PolicyName='Test',
    PolicyDocument=json.dumps(policy_json),
    Description='Test'
)