Output 类型的对象不是 JSON 可序列化的

Object of type Output is not JSON serializable

stack_name = "test-stack"
bucket = aws.s3.Bucket(
    f"{stack_name}",
    acl="private",
)
firehose_s3_policy = aws.iam.Policy(
    f"{stack_name}-firehose-s3-access-policy",
    policy=json.dumps(
        {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Action": [
                        "s3:*",
                    ],
                    "Resource": [
                        bucket.arn,
                    ]
                }
            ]
        }
    ))

我得到的错误是:

TypeError: Object of type Output is not JSON serializable

如果我将 bucket.arn 设为字符串 (str(bucket.arn)) 我会得到 \"Resource\": [\"<pulumi.output.Output object at 0x7f404d2ebed0>\"]

我试过apply,也没用。如何解决?

您需要使用 apply 将字符串的输出转换为 JSON 的输出:

def public_read_policy_for_bucket(arn):
    return json.dumps({
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                arn,
            ]
        }]
    })

firehose_s3_policy = aws.iam.Policy(
    f"{stack_name}-firehose-s3-access-policy",
    policy=bucket.arn.apply(public_read_policy_for_bucket))

看到一个full example