如何使用SAM部署通过AutoPublishAlias和其他别名获取lambda

时间:2020-09-10 00:40:11

标签: aws-lambda amazon-cloudformation aws-sam

我的目标是,额外的SAM deploy调用将导致:staging别名反映最新版本,而:live将通过外部方式进行更新,但必须初始化为在部署时创建的相同版本

我正在使用SAM部署,并且在我的lambda上需要别名。在初始模板中添加它们很棘手,因为您无法使用明确的版本号创建别名,并且AWS CloudFormation会强制保留旧的lambda版本,因此您无法在不检查资源的情况下知道该版本。这是到目前为止我发现的最好的方法,但是它很麻烦并且伸缩性不好(每个堆栈都需要相同的逻辑)。

此代码还依赖cfn-response.js进行自定义资源管理。

我该怎么做呢?

template.yaml

GetLatestVersionOfLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
        FunctionName: GetLatestVersionOfLambdaFunction
        Description: Given an existing Lambda Function Name, return the most recent version number
        VersionDescription: !Sub ${deployVersionDescription}
        AutoPublishAlias: staging
        CodeUri: src/
        Handler: getLatestLambdaVersionByName.handler
        MemorySize: 128
        Runtime: nodejs12.x
        Timeout: 10
        Tracing: Active
        Role: !GetAtt GetLatestVersionOfLambdaRole.Arn
SampleLambdaGetMaxVersionFunction:
    Type: Custom::FunctionVersion
    Properties:
        ServiceToken: !GetAtt GetLatestVersionOfLambdaFunction.Arn
        FunctionName: !Ref SampleLambdaFunction
SampleLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
        FunctionName: SampleLambda
        AutoPublishAlias: staging
        CodeUri: src/
        Handler: SampleLambda.handler
        MemorySize: 512
        Runtime: nodejs12.x
        Role: !GetAtt SampleLambdaRole.Arn
SampleLambdaLiveAlias:
    Type: AWS::Lambda::Alias
    Properties:
        FunctionName: !Ref SampleLambdaFunction
        FunctionVersion: !GetAtt SampleLambdaGetMaxVersionFunction.version
        Name: live

src / getLatestLambdaVersionByName.js

const response = require('./cfn-response.js'),
    AWS = require('aws-sdk'),
    lambda = new AWS.Lambda({ apiVersion: '2015-03-31' });

exports.handler = function (event, context) {
    console.log({ GetLatestLambdaVersionBynameEvent: event });

    if (event.RequestType == 'Delete') {
        response.send(event, context, response.SUCCESS);  //, { Delete: true, event } 
        return;
    }

    try {
        let params = {
            FunctionName: event.ResourceProperties.FunctionName,
            MaxItems: 999
        };
        // Use list-versions-by-function then determine the greatest version # and return it
        lambda.listVersionsByFunction(params, function (err, data) {
            console.log({ params, err });
            if (err) {
                console.error(err, err.stack); // an error occurred
                response.send(event, context, response.FAILED);
                return; // JSON.stringify({ error: err });
            } else {
                console.log(JSON.stringify({ Versions: data.Versions }));

                let version = data.Versions[data.Versions.length - 1].Version;
                if (version == '$LATEST') version = 1;
                let res = { version };
                response.send(event, context, response.SUCCESS, res);
                return; // JSON.stringify(res);
            }
        });

    } catch (err) {
        let error = { err, stack: err.stack };
        response.send(event, context, response.FAILED);
        return; // JSON.stringify({ error });
    }
}

0 个答案:

没有答案