Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runtime configuration for AWS Lambda function

Tags:

aws-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?

like image 238
palimpsestor Avatar asked Oct 25 '15 23:10

palimpsestor


1 Answers

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);
      }
    });
  });
};
like image 66
Mike Hogan Avatar answered Oct 11 '22 03:10

Mike Hogan