0

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

这是我的 YAML 模板片段

    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是定义的参数字段,以便我每次都可以获得新版本

为清晰起见更新了图像 在此处输入图像描述

提前感谢任何线索或帮助

4

1 回答 1

2

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

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

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

但可能你的LambdaDeployFunction. 您正在那里创建新版本,因此它似乎也是正确设置别名的理想场所。

于 2020-12-17T05:52:59.097 回答