AWS CDK 将路径应用于已创建的 lambda 角色

AWS CDK apply path to lambda role created

我正在使用 AWS CDK (node js) 创建 lambda 函数。下面是我的函数的定义:

const receiverFunction = new lambda.Function(this, "Receiver", {
      description: 'Lambda function responsible for receiving the audit message',
      runtime: lambda.Runtime.NODEJS_10_X,
      code: lambda.Code.fromAsset("application"),
      handler: "receiver.handler",
      environment: {. . .},
      timeout: core.Duration.seconds(15),
      logRetention: logs.RetentionDays.ONE_YEAR
    });

// Define a audit queue where the messages will be published
const auditQueue = new sqs.Queue(this, 'audit-queue', {
  queueName: 'audit-queue'
});


auditQueue.grantSendMessages(receiverFunction);

这将创建一个 lambda 和一个 SQS,其中包括一个 lambda 角色,该角色授予将消息放入 SQS 的权限。使用创建此堆栈所需的权限运行良好。

我正在使用将 CFN 部署角色作为输入的 --role-arn 参数。为了安全措施,允许此角色创建具有路径 cloudformation 的 IAM 角色。为了符合此规则,我需要能够将 path 添加到角色,而无需将完整的角色定义指定为 new iam.Role ....

有什么方法可以获取上面创建的 lambda 角色并向其添加 path 吗?

因为 lambda 角色是在 Function 构造中创建的。我们可以使用cdk escape hatches来设置路径。

您可以使用下面的代码来设置 path 或任何其他变量。

const role = receiverFunction.node.children.find(child => child instanceof Role) as Role
const cfnRole = role.node.defaultChild  as CfnRole
cfnRole.path = "/cloudformation/"