【问题标题】:AWS::Serverless::Function lambda version for Lambda@Edge event handlerLambda@Edge 事件处理程序的 AWS::Serverless::Function lambda 版本
【发布时间】:2020-09-27 10:03:21
【问题描述】:

我正在尝试使用 AWS SAM 框架来创建一个 lambda,以用作 CloudFront 事件处理程序。 AWS::Serverless::Function 似乎不支持 Version 属性。我看到的错误:

com.amazonaws.services.cloudfront.model.InvalidLambdaFunctionAssociationException: The function ARN must reference a specific function version. (The ARN must end with the version number.)

我找到了this answer,这让我尝试了它。我的 CloudFormation YAML 文件的相关部分:

Resources:
  CloudFrontFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: cloudfront-handler/hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7

Outputs:
  CloudFrontFunctionArn: 
      Description: CloudFront Function ARN with Version
      Value: !Join [':', [!GetAtt CloudFrontFunction.Arn, !GetAtt CloudFrontFunction.Version]]

当我sam deploy 时出现以下错误。

Waiting for changeset to be created..
Error: Failed to create changeset for the stack: my-sam-app, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Template error: resource CloudFrontFunction does not support attribute type Version in Fn::GetAtt

AWS::Lambda::Function 上可用的属性是 documented here,它列出了 Version 作为属性之一。所以看来AWS::Serverless::Function 不支持获取版本。如何解决这个问题,以便部署使用 AWS SAM 框架实现的 CloudFront 事件处理程序?

更新

根据@mokugo-devops(谢谢!),解决这个问题的方法是像这样添加AutoPublishAlias: live

Resources:
  CloudFrontFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: cloudfront-handler/hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7
      AutoPublishAlias: live

Outputs:
  CloudFrontFunctionVersion: 
      Description: CloudFront Function ARN with Version
      Value: !Ref CloudFrontFunction.Version

【问题讨论】:

  • 您是否按照您提供的链接定义了资源AWS::Lambda::VersionAWS::Lambda::Version 是独立于 AWS::Serverless::Function 的资源。

标签: amazon-web-services amazon-cloudformation amazon-cloudfront aws-sam


【解决方案1】:

默认情况下,该函数不会自动部署版本。相反,您需要指定 AutoPublishAlias 属性。

更多信息here

这样做您将无法检索版本。

相反,您需要创建AWS::Lambda::Version 的资源并从CloudFrontFunction 资源传入Arn。然后,您可以从这个新资源中获取 Lambda 版本 arn 并将其传递给您的 CloudFrontFunctionArn 输出。

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2018-06-19
    • 2016-08-06
    相关资源
    最近更新 更多