在无服务器中,我正在创建一个 AWS::Lambda::Permission
CFN 资源,允许 Cognito 调用 Custom Message Lambda Handler 。
AWS::Lambda::Permission
取决于 lambda。如何确保首先创建 lambda?
我已经尝试将 DependsOn
属性添加到 AWS::Lambda::Permission
CFN 资源,但没有成功。
下面是我的 CFN 资源,它尝试向 Cognito 添加调用 lambda 的权限:
UserPoolLambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:invokeFunction
Principal: cognito-idp.amazonaws.com
FunctionName: arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-cognitoCustomMessage
SourceArn: arn:aws:cognito-idp:${self:provider.region}:#{AWS::AccountId}:userpool/${self:provider.environment.USER_POOL_ID}
这是我的 lambda 在 serverless.yml
中的样子:
cognitoCustomMessage:
handler: src/main/lambdas/users_handler.cognitoCustomMessage
这是我的 lambda 在非常基本的层面上所做的事情:
cognitoCustomMessage(event, next) {
if (event.triggerSource === 'CustomMessage_ForgotPassword') {
// do stuff
}
return next(null, event);
}
我从上面得到的错误是:
An error occurred: CognitoCustomMessageLambdaFunction - Function not found: arn:aws:lambda:us-west-2:1234567890:my-service-dev-cognitoCustomMessage (Service: AWSLambdaInternal; Status Code: 404; Error Code: ResourceNotFoundException; Request ID: e2a98525-5090-4d0f-a1f5-20610474f93b).
如果我添加DependsOn
:
UserPoolLambdaInvokePermission:
Type: AWS::Lambda::Permission
DependsOn: arn:aws:lambda:${self:provider.region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-cognitoCustomMessage
...
....
我从上面得到的错误是:
The CloudFormation template is invalid: Template format error: DependsOn must be a string or list of strings.
我也尝试过:
UserPoolLambdaInvokePermission:
Type: AWS::Lambda::Permission
DependsOn: CognitoCustomMessageLambdaFunction
...
....
我从上面得到的错误是:
An error occurred: CognitoCustomMessageLambdaFunction - Function not found: arn:aws:lambda:us-west-2:1234567890:my-service-dev-cognitoCustomMessage (Service: AWSLambdaInternal; Status Code: 404; Error Code: ResourceNotFoundException; Request ID: b888ae82-a0d7-4d69-888e-9e63027925c1).
我希望在 CFN 资源需要使用 lambda 函数之前应该有某种方法来创建它,但 DependsOn
似乎并非如此。
最佳答案
您在 serverless.yml 中定义的函数将在后台转换为 cloudformation 资源。该资源称为 XLambdaFunction,其中 X = 函数名称,首字母大写。
所以如果你有:
functions:
hello:
handler: handler.hello
...other function stuff...
您可以引用:
DependsOn: HelloLambdaFunction
我只能假设,如果您的函数已命名为 CognitoCustomMessageLambdaFunction
那么您必须引用:
DependsOn: CognitoCustomMessageLambdaFunctionLambdaFunction
关于aws-lambda - 如何从 CFN 资源 "DependsOn"Lambda 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57167856/