使用云形成从SNS触发lambda?
答案 0 :(得分:2)
您可以使用事件来设置触发器。
lambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler:
------
Events:
SNS1:
Type: SNS
Properties:
Topic:
Ref: SNSTopic1
SNSTopic1:
Type: 'AWS::SNS::Topic'
参考:参考:https://docs.aws.amazon.com/lambda/latest/dg/serverless_app.html#serverless_app_resources
答案 1 :(得分:2)
我们要做的是,我们不将sns指向不合格的lambda,而是将其指向lambda-alias。基本上,创建一个lambda,然后创建一个别名,使用sns指向lambda-alias。
当您具有lambda的新代码时(您的ci / cd可以执行以下操作),更新lambda函数代码,创建新的lambda版本,然后将别名重新指向新版本。这样,您的sns完全不需要更改新的Lambda代码。
Resources:
AwsServerlessExpressFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: mylambda
Description: mylambda
Runtime: nodejs8.10
Handler: index.handler
MemorySize: 512
Timeout: 60
Role: !Ref LambdaExecutionRoleArn
AwsServerlessExpressFunctionAlias:
Type: AWS::Lambda::Alias
Properties:
FunctionName: !Ref AwsServerlessExpressFunction
FunctionVersion: '$LATEST'
Name: live
SNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: mysnstopic
TopicName: mysnstopic
Subscription:
-
Endpoint: !Ref AwsServerlessExpressFunctionAlias
Protocol: lambda
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Ref SNSTopic
FunctionName: !Ref AwsServerlessExpressFunctionAlias
答案 2 :(得分:1)
您不应忘记添加LambdaFunctionPermission
以允许使用SNS主题。
这是yaml中Cloud Formation模板的资源部分:
Resources:
SNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: sns-topic-for-lambda
TopicName: sns-topic-for-lambda
Subscription:
- Endpoint: !GetAtt LambdaFunction.Arn
Protocol: lambda
LambdaFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt LambdaFunction.Arn
Principal: sns.amazonaws.com
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
...