AWS Lambda函数的运行时配置 [英] runtime configuration for AWS Lambda function

查看:136
本文介绍了AWS Lambda函数的运行时配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AWS Lambda函数,需要连接到远程TCP服务.将Lambda功能部署到AWS后,是否可以通过远程服务的IP地址配置Lambda功能?还是我必须在部署之前将配置烘焙到打包的Lambda函数中?

I have an an AWS Lambda function that needs to connect to a remote TCP service. Is there any way to configure the Lambda function with the IP address of the remote service after the Lambda function has been deployed to AWS? Or do I have to bake the configuration into the packaged Lambda function before it's deployed?

推荐答案

我找到了一种用于支持测试环境和生产环境的方法,该方法将为您提供帮助.

I found a way that I use for supporting a test environment and a production environment that will help you.

对于功能的测试版本,我将其称为TEST-ConnectToRemoteTcpService,对于功能的生产版本,我将其命名为PRODUCTION-ConnectToRemoteTcpService.这使我可以使用正则表达式提取环境名称.

For the test version of the function, I am calling it TEST-ConnectToRemoteTcpService and for the production version of the function I am naming the function PRODUCTION-ConnectToRemoteTcpService. This allows me pull out the environment name using a regular expression.

然后我将config/test.jsonconfig/production.json存储在我作为函数代码上传的zip文件中.该函数运行时,此zip文件将被提取到目录process.env.LAMBDA_TASK_ROOT中.这样我就可以加载该文件并获取所需的配置.

Then I am storing config/test.json and config/production.json in the zip file that I upload as code for the function. This zip file will be extracted into the directory process.env.LAMBDA_TASK_ROOT when the function runs. So I can load that file and get the config I need.

有些人不喜欢将配置存储在代码zip文件中,这很好-您可以从S3加载文件或使用任何喜欢的策略.

Some people don't like storing the config in the code zip file, which is fine - you can just load a file from S3 or use whatever strategy you like.

从zip读取文件的代码:

Code for reading the file from the zip:

const readConfiguration = () => {
  return new Promise((resolve, reject) => {
    let environment = /^(.*?)-.*/.exec(process.env.AWS_LAMBDA_FUNCTION_NAME)[1].toLowerCase();
    console.log(`environment is ${environment}`);

    fs.readFile(`${process.env.LAMBDA_TASK_ROOT}/config/${environment}.json`, 'utf8', function (err,data) {
      if (err) {
        reject(err);
      } else {
        var config = JSON.parse(data);
        console.log(`configuration is ${data}`);
        resolve(config);
      }
    });
  });
};

这篇关于AWS Lambda函数的运行时配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆