简体   繁体   English

使用 AWS CDK 将现有角色附加到 AWS Lambda

[英]Attach an existing role to AWS Lambda with AWS CDK

I want to attach and existing role to a lambda created using CDK I am doing the below我想将现有角色附加到使用 CDK 创建的 lambda 我正在执行以下操作

  const role1 = iam.Role.fromRoleArn(this, 'Role', 'ARN', {
         mutable: true,
    });
 const lambda1 = new lambda.Function(this, 'lambda1', {
        runtime: lambda.Runtime.PYTHON_3_7,
        code: lambda.Code.asset('lambda/lambda1_function'),
        handler: 'lambda_function.lambda_handler',
        role:role1,
             reservedConcurrentExecutions: 1
    });

getting the below exception when I run cdk deploy运行 cdk deploy 时出现以下异常

The role defined for the function cannot be assumed by Lambda. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID:

If someone could help to fix this PS: I am using typescript CDK@1.27.0如果有人可以帮助解决这个问题 PS:我正在使用 typescript CDK@1.27.0

Based on the comments, the issue was incorrect trust policy in the role.根据评论,问题是角色中的信任策略不正确。

The issue was solved by adding lambda.amazonaws.com to the trust policy .该问题已通过将lambda.amazonaws.com添加到trust policy得到解决。

As per the role parameter documentation:根据role参数文档:

Lambda execution role. Lambda 执行角色。

This is the role that will be assumed by the function upon execution.这是 function 在执行时将承担的角色。 It controls the permissions that the function will have.它控制 function 将拥有的权限。 The Role must be assumable by the 'lambda.amazonaws.com' service principal .角色必须由“lambda.amazonaws.com”服务主体承担

this can be achieved by granting permission to lambda service:这可以通过授予 lambda 服务权限来实现:

role1.grant(new iam.ServicePrincipal("lambda.amazonaws.com"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法使用 AWS-CDK 将新的 AWS ApiGateway 连接到现有的 lambda 函数? (打字稿) - Is there a way to connect a new AWS ApiGateway to existing lambda funtion using AWS-CDK? (TypeScript) 如何在 aws cdk 中使用 aws-lambda 导入和调用 lambda 函数? - How to import and call a lambda function with aws-lambda in aws cdk? 无法将 Nodejs 和 Typescript 部署到 AWS cdk + lambda - Cannot deploy Nodejs and Typescript to AWS cdk + lambda AWS CDK EcsDeployAction 更新现有 Fargate 服务 - AWS CDK EcsDeployAction update existing Fargate Service 无法将 EC2 实例附加到 AWS CDK 中的经典负载均衡器 - Unable to attach EC2 instance to a classic load balancer in AWS CDK 如何使用 AWS Lambda for Firehose with CDK 启用转换源记录 - How to enable Transform source records with AWS Lambda for Firehose with CDK AWS CDK Typescript,如何从 lambda 触发步进函数? - AWS CDK Typescript, how to trigger step function from lambda? 使用 AWS CDK 部署 Lambda function(不作为应用程序的一部分) - Deploying Lambda function (not as part of an Application) using AWS CDK 使用 CDK 将 AWS SM 密钥传递给 Lambda 环境 - Pass AWS SM Secret Key to Lambda Environment with CDK AWS Lambda CDK 不生成 SNS 主题和订阅 - AWS Lambda CDK does not generate SNS topic and subscription
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM