awscli-lambda函数更新触发器

时间:2018-06-21 11:07:29

标签: amazon-web-services aws-lambda gitlab

我们有一个lambda @ edge函数,该函数侦听云前端分发源requestresponse事件。我们正在尝试使部署自动化。到目前为止,我们已经成功更新了代码并发布了新版本。

  - npm install
  - zip -r lambda.zip *
  - aws lambda update-function-code --function-name LambdaFunction1 --zip-file fileb://lambda.zip
  - aws lambda publish-version --function-name LambdaFunction1

但是我们如何更新CloudFront触发器以指向最新发布的版本?

2 个答案:

答案 0 :(得分:3)

执行以下步骤-

  1. 通过运行以下cli首先检查lambda的版本 命令。并获取最新版本的FunctionARN。   *按功能显示lambda list-versions --function-name LAMBDA_NAME *

  2. 首先通过以下命令获取cloudfront分发json数据。   aws cloudfront get-distribution-config --id DISTRIBUTION_ID>  cf_config.json

  3. 通过从以下位置获取 DistributionConfig 键来创建名为updated_cf_config.json文件的文件 cf_config.json。

  4. 现在将最新版本的lambda的FunctionARN放入 “ LambdaFunctionAssociations”->“ LambdaFunctionARN”

  5. 通过运行以下命令更新Cloudfront分发 要更新Cloudfront发行版本,我们需要从 cf_config.json。 看到cloudfront更新-分布-分布-配置 file://cf_config.json --id DISTRIBUTION_ID --if-match ETAG

答案 1 :(得分:0)

如果有人偶然发现这一点,我创建了一个简单的Node.js脚本来处理CloudFront更新。

这要求您将aws lambda publish-version命令的响应捕获到lambda_publish_response.json中,并更改cloudFrontDistributionId变量,但其他所有操作“都应该起作用”:

const fs = require('fs');
const {exec} = require('child_process');

const cloudFrontDistributionId = 'EXXXXXXXXXXXXX';
const currentCloudFrontConfigFile = 'cf_config.json';
const updatedCloudFrontConfigFile = 'cf_config_updated.json';
const lambdaPublishResponseFile = 'lambda_publish_response.json';

exec(`aws cloudfront get-distribution-config --id ${cloudFrontDistributionId} > ${currentCloudFrontConfigFile}`, (error, stdout, stderr) => {
    if (error) {
        console.error(`error: ${error.message}`);

        return process.exit(1);
    }

    if (stderr) {
        console.error(`stderr: ${stderr}`);

        return process.exit(1);
    }

    if (!fs.existsSync(lambdaPublishResponseFile)) {
        console.error('Run this first: `aws lambda publish-version --function-name LambdaFunctionName > lambda_publish_response.json`');

        return process.exit(1);
    }

    let cfConfig = JSON.parse(fs.readFileSync(currentCloudFrontConfigFile));
    const etag = cfConfig.ETag;
    const lambdaPublishData = JSON.parse(fs.readFileSync(lambdaPublishResponseFile));

    cfConfig.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Items[0].LambdaFunctionARN = lambdaPublishData.FunctionArn;

    fs.writeFileSync(updatedCloudFrontConfigFile, JSON.stringify(cfConfig.DistributionConfig));

    exec(`aws cloudfront update-distribution --distribution-config file://${updatedCloudFrontConfigFile} --id ${cloudFrontDistributionId} --if-match ${etag}`, (error, stdout, stderr) => {
        if (error) {
            return console.error(`error: ${error.message}`);
        }

        if (stderr) {
            return console.error(`stderr: ${stderr}`);
        }

        console.log(`stdout: ${stdout}`);

        fs.unlinkSync(lambdaPublishResponseFile);
        fs.unlinkSync(currentCloudFrontConfigFile);
        fs.unlinkSync(updatedCloudFrontConfigFile);
    });
});

以下是要点,列出了实现此目的所需的所有命令:https://gist.github.com/neonexus/3062b34b09896fa027e22d332dd65069