【问题标题】:CloudFormation : Previous Lambda Alias is getting lost when a new alias is created from new versionCloudFormation:从新版本创建新别名时,以前的 Lambda 别名会丢失
【发布时间】:2021-03-27 19:07:11
【问题描述】:

每当代码有更新时,我都会发布一个新版本,并为该版本创建一个别名。 但是以前的别名在新版本中丢失了

这是我的 YAML 模板 sn-p

    Description: Publish a new version of a Lambda function whenever the code is updated.
Parameters:
  MyTestString:
    Description: Change this string when code is updated.
    Type: String
    Default: "Test"
  LambdaFuncName:
    Type: String
    Default: stack_over_flow
  LambdaCustomFuncName:
    Type: String
    Default: custom_stack_over_flow
  LambdaVersionNumber:
    Type: String
    Default: MyAlias-001
Resources:
  MyCustomResource:
    Type: Custom::Resource
    Properties:
      ServiceToken: !GetAtt MyFunction.Arn
      MyTestString: !Ref MyTestString
  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      FunctionName:
        Ref: LambdaFuncName
      Code:
        ZipFile: !Sub |
          var response = require('cfn-response');
          exports.handler = function(event, context) {
            return response.send(event, context, response.SUCCESS, {Result: '${MyTestString}'});
          };
      Runtime: nodejs12.x
  LambdaDeploy:
    Type: Custom::LambdaVersion
    Properties:
      ServiceToken: !GetAtt LambdaDeployFunction.Arn
      FunctionName: !Ref MyFunction
      MyTestString: !Ref MyTestString
  LambdaDeployFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: "index.handler"
      Role: !GetAtt LambdaExecutionRole.Arn
      FunctionName:
        Ref: LambdaCustomFuncName
      Code:
        ZipFile: !Sub |
          var AWS = require('aws-sdk');
          var response = require('cfn-response');
          exports.handler = (event, context) => {
            console.log("Request received:\n", JSON.stringify(event));
            if (event.RequestType == 'Delete') {
              return response.send(event, context, response.SUCCESS);
            }
            var lambda = new AWS.Lambda();
            lambda.publishVersion({FunctionName: event.ResourceProperties.FunctionName}).promise().then((data) => {
              return response.send(event, context, response.SUCCESS, {Version: data.Version}, data.FunctionArn);
            }).catch((e) => {
              return response.send(event, context, response.FAILED, e);
            });
          };
      Runtime: nodejs12.x
  LambdaAlias:
    Type: 'AWS::Lambda::Alias'
    Properties:
      FunctionName: !Ref MyFunction
      FunctionVersion: !GetAtt 
        - LambdaDeploy
        - Version
      Name: 
        Ref: LambdaVersionNumber
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    DeletionPolicy: Retain
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal: {Service: [lambda.amazonaws.com]}
          Action: ['sts:AssumeRole']
      Path: /
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
      - PolicyName: PublishVersion
        PolicyDocument:
          Version: 2012-10-17
          Statement:
          - Effect: Allow
            Action: ['lambda:PublishVersion']
            Resource: '*'
Outputs:
  LambdaVersion:
    Value: !GetAtt LambdaDeploy.Version
  CustomResourceResult:
    Value: !GetAtt MyCustomResource.Result

LambdaVersionNumber是定义的参数字段,以便我每次都可以获得新版本

为清晰起见更新了图像

提前感谢任何线索或帮助

【问题讨论】:

  • “迷路”是什么意思?
  • Lambda 别名未指向以前的版本。 Alias 仅适用于最新版本
  • 您正在使用自定义资源。您还没有解释 Version 是什么,因为这是由您的自定义函数设置的。也许您的自定义资源设置 Version 不正确?
  • @Marcin,版本正在正确创建,在 Lambda 上,我可以看到多个版本,但它仅适用于 Alias,更新了完整代码

标签: amazon-web-services amazon-cloudformation aws-cloudformation-custom-resource


【解决方案1】:

您正在执行lambda.publishVersion,它只返回data.Version 中的last version created。因此,您的 Version 将始终增加 1,随后,您的 LambdaAlias 也将更新以指向 Version 的新值。

如果您不希望这样,也许不要为每个部署增加Version 并始终保持在旧版本上:

                    return response.send(event, context, 
                                         response.SUCCESS, 
                                         { 
                                          Version: (data.Version == 1 ? data.Version : data.Version - 1)}, 
                                         data.FunctionArn);

但是从LambdaDeployFunction控制别名可能会更简单。您正在那里创建新版本,因此它似乎是正确设置别名的理想场所。

【讨论】:

  • 附在主帖上的图片将清楚地说明发生了什么
  • 更新堆栈时,别名将被删除。使用 UpdateReplacePolicy:Retain 保留以前的别名
猜你喜欢
  • 2020-03-01
  • 2018-09-19
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 2017-05-14
  • 2023-04-10
相关资源
最近更新 更多