简体   繁体   English

SAM 模板事件类型

[英]SAM Template Event Type

How to add an event type as Cloudfront for SAM template for a Lambda function如何为 Lambda 函数添​​加事件类型作为 Cloudfront for SAM 模板

I have a SAM template where there will be a lambda function and Api gateway as a trigger , Now instead of Api gateway as a Trigger , I need to add an existing CLoudfront Distribution.我有一个 SAM 模板,其中将有一个 lambda 函数和 Api 网关作为触发器,现在我需要添加一个现有的 CLoudfront Distribution,而不是将 Api 网关作为触发器。 But I am not finding any such option to do that.但我没有找到任何这样的选择来做到这一点。

cloudfrontlambda:    
    Type: AWS::Serverless::Function    
    Properties:    
      FunctionName: cloudfrontlambda   
      Handler: index.handler    
      Runtime: nodejs8.10    
      CodeUri: ./Lambda/cloudfrontlambda/   
      Description: Function is used for validating the id token    
      MemorySize: 128    
      Timeout: 30    
      Role: 'arn:aws:iam::1234567:role/DR_lambda_web_execution'    
      Events:
     --------- ## how to add an Existing cloudfront distribution here

At present there is no event source type for CloudFront distributions within the SAM model [1].目前,SAM 模型 [1] 中没有 CloudFront 分配的事件源类型。 Assuming you are attempting to create an edge lambda function, you will need to attach the function either manually or via the cloudformation template that you used to create the distribution - as it needs to be declared directly against the cloud front distribution.假设您正在尝试创建一个边缘 lambda 函数,您将需要手动或通过用于创建分发的 cloudformation 模板附加该函数 - 因为它需要直接针对云前端分发进行声明。 I've edited it down for brevity, but for example -为简洁起见,我对其进行了编辑,但例如 -

cloudfrontlambda:    
    Type: AWS::Serverless::Function    
    Properties:    
      FunctionName: cloudfrontlambda   
      Handler: index.handler    
      Runtime: nodejs8.10    
      CodeUri: ./Lambda/cloudfrontlambda/   
      Description: Function is used for validating the id token    
      MemorySize: 128    
      Timeout: 30    
      Role: 'arn:aws:iam::1234567:role/DR_lambda_web_exec’

  cloudfrontdistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        CacheBehaviors:
          - LambdaFunctionAssociations:
              - EventType: string-value
                LambdaFunctionARN: !Sub ‘${cloudfrontlambda.Arn}:${cloudfrontlambda.Version}’

Eg例如

Note that your lambda function must be deployed in us-east-1 to work in this way.请注意,您的 lambda 函数必须部署在 us-east-1 中才能以这种方式工作。 I can confirm that you can deploy a cloud front distribution via a cloudformation template in another region and still reference lambda functions that are deployed to us-east-1.我可以确认您可以通过另一个区域的 cloudformation 模板部署云前端分发,并且仍然引用部署到 us-east-1 的 lambda 函数。

Also, you must specify the version of the lambda function within the ARN.此外,您必须在 ARN 中指定 lambda 函数的版本。 The AWS::Lambda::Function cloudformation type does not provide this on its own (requiring an additional AWS::Lambda::Version resource), so this may complicate your CloudFront distribution template if it is not already contained with a SAM template. AWS::Lambda::Function cloudformation 类型本身不提供此功能(需要额外的 AWS::Lambda::Version 资源),因此如果您的 CloudFront 分配模板尚未包含在 SAM 模板中,则这可能会使您的 CloudFront 分配模板复杂化。

[1] https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#event-source-object [1] https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#event-source-object

Short answer, SNS.简短的回答,SNS。 Long answer, you can't except for SNS.长答案,除了SNS,你不能。

You can't add an existing CloudFront resource, because that breaks the mold of CF.您不能添加现有的CloudFront 资源,因为这打破了 CF 的模式。 This took me a while to wrap my head around too, but essentially a CloudFront template can only create/modify resources within itself.这也花了我一段时间来回想,但本质上 CloudFront 模板只能在其内部创建/修改资源。 It cannot modify resources outside of its stack.它不能修改其堆栈之外的资源。 Here are a few links supporting this: https://github.com/awslabs/serverless-application-model/issues/241以下是一些支持此操作的链接: https : //github.com/awslabs/serverless-application-model/issues/241

https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#s3 (specifically says existing S3 buckets cannot be an event source) https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#s3 (特别是说现有的 S3 存储桶不能作为事件源)

This last link shows that recently they supported importing resources from other stacks, but the resource must first be deleted from its previous stack before it can be used.这最后一个链接显示,最近他们支持从其他堆栈导入资源,但该资源必须先从其先前的堆栈中删除才能使用。 I'm not sure about your architecture, but I know for mine that would break things very quickly.我不确定你的架构,但我知道我的架构会很快破坏。 https://github.com/awslabs/serverless-application-model/issues/249 https://github.com/awslabs/serverless-application-model/issues/249

The work around is to add Custom Resources (lambdas) to alleviate this impediment.解决方法是添加自定义资源 (lambdas) 以缓解这一障碍。 In the last link I sent there is an example of an SNS workaround since this limitation does not apply to SNS.在我发送的最后一个链接中,有一个 SNS 解决方法的示例,因为此限制不适用于 SNS。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM