AWS::Serverless::Function Lambda@Edge 事件处理程序的 lambda 版本

AWS::Serverless::Function lambda version for Lambda@Edge event handler

我正在尝试使用 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.)

我发现 促使我尝试了它。我的 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

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

更多信息可用here

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

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