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



我正在使用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,其中包括一个授予将消息放入SQS的权限的lambda角色。与创建此堆栈所需的权限配合良好。

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

有没有什么方法可以获取上面创建的lambda角色并将path添加到其中?

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

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

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

最新更新