runtime configuration for AWS Lambda function

2019-04-20 00:50发布

问题:

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?

回答1:

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

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.

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.

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.

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);
      }
    });
  });
};


回答2:

Support for environment variables was added for AWS Lambda starting November 18, 2016. Adding a variable to an existing function can be done via command line as shown below or from the AWS Console.

aws lambda update-function-configuration \
    --function-name MyFunction \
    --environment Variables={REMOTE_SERVICE_IP=100.100.100.100}

Documentation can be found here.



回答3:

You can invoke the Lambda function via SNS topic subscription and have it configure itself from the payload inside the SNS event.

Here's the official guide on how to do that Invoking Lambda via SNS.



回答4:

A few options, depending on the use-case

  • If your config will not change then you can use S3 objects and access from Lambda or set your Lambda to trigger on new config changes. (Though this is the cheapest way, you are limited in what you can do compared to other alternatives)
  • If the config is changing constantly, then DynamoDB - Key/value is an alternative.
  • If DynamoDB is expensive for the frequent read/writes and not worth the value then you can have TCP service post config into a SQS queue. (or an SNS if you want to trigger when the service posts a new config)


标签: aws-lambda