laconia icon indicating copy to clipboard operation
laconia copied to clipboard

Make envVarInstances API more explicit

Open ceilfors opened this issue 5 years ago • 1 comments

Currently, there is only one way to register invoker and config to LaconiaContext, which is via envVarInstances. After gathering offline feedback, developers found that it is not clear what the factory is doing most of the times when reading the handler code. They also have to jump back and forth from the handler to serverless.yml to make sure that they're using the right variable names.

In conjunction, Laconia still would like to encourage the use of environment variable, so that those values are not hardcoded in the Lambda code.

Summary

  • Rename envVarInstances() to scanEnvVar(). This factory will scan environment variables with LACONIA_ prefixes.
  • Introduce fromEnvVar(["MY_LAMBDA"]). This factory will take the specified environment variable names and create the instance name based on them.
  • Introduce mapEnvVar({MY_LAMBDA: myOwnName}): This factory will take the mapping specified and create the instance names appropriately.

Example

Using scanEnvVar

/**
 * LambdaEnvironment variables:
 * - LACONIA_CONFIG_SOME_SECRET: ssm://path/to/secret
 */
const config = require("@laconia/config");
const laconia = require("@laconia/core");

const app = async ({ someSecret }) => {
  /* logic */
};

exports.handler = laconia(app).register(config.scanEnvVar());

Using fromEnvVar

/**
 * LambdaEnvironment variables:
 * - SOME_SECRET: ssm://path/to/secret
 */
const config = require("@laconia/config");
const laconia = require("@laconia/core");

const app = async ({ someSecret }) => {
  /* logic */
};

exports.handler = laconia(app).register(config.fromEnvVar(["SOME_SECRET"]));

Using fromEnvVar

/**
 * LambdaEnvironment variables:
 * - SOME_SECRET: ssm://path/to/secret
 */
const config = require("@laconia/config");
const laconia = require("@laconia/core");

const app = async ({ mySecret }) => {
  /* logic */
};

exports.handler = laconia(app).register(config.mapEnvVar({ mySecret: "SOME_SECRET" }));

ceilfors avatar Apr 01 '19 05:04 ceilfors

@laconiajs/core-contributors Any input on the design above? This is one of the feedback I've got from our users.

ceilfors avatar May 24 '19 05:05 ceilfors