7

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

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"
                }
            }
        }
    ]
}

我尝试了很多组合,但没有一个有效。如何生成 JSON 数组Output<string>[]

4

1 回答 1

10

有时最简单的方法是在另一个资源的整个创建过程中包装应用程序。在这种情况下,您appTaskPolicy可以OutputInstance<aws.iam.Policy>使用它自己的输出将其输入程序的其他部分。

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'
                        }
                    }
                }
            ]
        }
    });
});
于 2019-10-11T19:27:37.290 回答