【问题标题】:Pulumi: How to serialize Output<string>[] to JSONPulumi:如何将 Output<string>[] 序列化为 JSON
【发布时间】:2020-02-09 00:02:12
【问题描述】:

我想允许 Lambda 服务在我的 VPC 中创建部署,因此我有 Output&lt;string&gt;[] 类型的子网 ID 数组,我想将其放入角色策略中,如下所示:

export const createNetworkInterfacePolicy = new aws.iam.RolePolicy(
  "network-interface-policy-2",
  {
    policy: pulumi.interpolate `{
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": ["ec2:CreateNetworkInterfacePermission"],
          "Resource": [
            "arn:aws:ec2:${region}:${callerIdentity.accountId}:network-interface/*"
          ],
          "Condition": {
            "StringEquals": {
              "ec2:Subnet": ${JSON.stringify(vpc.vpcPrivateSubnetIds.map(item => item.apply(JSON.stringify)))},
              "ec2:AuthorizedService": "lambda.amazonaws.com"
            }
          }
        }
      ]
    }`,
    role: deploymentRole
  }
);

不幸的是,我最终得到的是:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterfacePermission"
            ],
            "Resource": [
                "arn:aws:ec2:us-east-2:removedAccountId:network-interface/*"
            ],
            "Condition": {
                "StringEquals": {
                    "ec2:Subnet": [
                        "Calling [toJSON] on an [Output<T>] is not supported.\n\nTo get the value of an Output as a JSON value or JSON string consider either:\n    1: o.apply(v => v.toJSON())\n    2: o.apply(v => JSON.stringify(v))\n\nSee https://pulumi.io/help/outputs for more details.\nThis function may throw in a future version of @pulumi/pulumi.",
                        "Calling [toJSON] on an [Output<T>] is not supported.\n\nTo get the value of an Output as a JSON value or JSON string consider either:\n    1: o.apply(v => v.toJSON())\n    2: o.apply(v => JSON.stringify(v))\n\nSee https://pulumi.io/help/outputs for more details.\nThis function may throw in a future version of @pulumi/pulumi."
                    ],
                    "ec2:AuthorizedService": "lambda.amazonaws.com"
                }
            }
        }
    ]
}

我尝试了许多组合,但没有一个有效。如何从Output&lt;string&gt;[] 生成 JSON 数组?

【问题讨论】:

    标签: amazon-web-services pulumi


    【解决方案1】:

    有时,在另一个资源的整个创建过程中封装一个应用程序是最简单的。在这种情况下,appTaskPolicy 变为 OutputInstance&lt;aws.iam.Policy&gt;,然后您可以使用它自己的输出将其输入到程序的其他部分。

    如果您还没有使用此功能,您需要import * as pulumi from '@pulumi/pulumi';

    const vpc = awsx.Network.getDefault();
    const appTaskPolicyName = named('app-task-policy');
    
    const appTaskPolicy = pulumi.all(vpc.publicSubnetIds).apply(([...subnetIds]) => {
        return new aws.iam.Policy(appTaskPolicyName, {
            policy: {
                Version: '2012-10-17',
                Statement: [
                    {
                        Action: ['sqs:GetQueueUrl', 'sqs:SendMessage'],
                        Resource: [
                            'someresourcearn'
                        ],
                        Effect: 'Allow',
                        Condition: {
                            StringEquals: {
                                'ec2:Subnet': subnetIds,
                                'ec2:AuthorizedService': 'lambda.amazonaws.com'
                            }
                        }
                    }
                ]
            }
        });
    });
    

    【讨论】:

    • @pbn 你能检查一下这个解决方案是否适合你
    • 这对我有用 - 非常感谢。我已经从另一个堆栈中导出了一个字符串数组,很难找出如何将下一个堆栈作为字符串数组而不是作为输出导入回来:|
    • 同样的解决方法对我使用 Python pulumi API 有效。谢谢!
    猜你喜欢
    • 2017-01-15
    • 2017-12-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多