DEV Community

Kyle Parisi
Kyle Parisi

Posted on

Understanding AWS lambda qualifiers

Heres the scenario

You have an application which is making use of AWS cloudwatch logs. You want a beautifully simple way to parse those logs for errors and send a message to monitoring slack channel. We have several log streams (service1, service2, etc) for 2 different environments. One for staging and one for production. These environments should send messages to their respective slack channels.

Using qualifiers

Qualifiers are a way to name the different versions of your code; such as production, staging, etc.. AWS lambda qualifiers are good for dividing environments as well as promoting versions of code in staging before promoting production. Qualifiers allow for unique triggers. From this we attach our staging qualifier to watch a set of streams. For example:

lambda example

From the image above you can see I've set the qualifier for version 1 of my code to have 2 cloudwatch log triggers based on different text patterns. Each stream can be turned on and off at a click of a button with out redeploying. I find this super cool.

Unfortunately you can't set environment variables based on a qualifier. Only versioned code will be given static environment variables. However, there is a work around.

function isNotAnAliasName(context) {
  return isNumber(context.alias) || context.functionName === context.alias;
}

// Add this to your exports.handler
context.alias = context.invokedFunctionArn.split(":").slice(-1)[0];
console.log("Alias: " + context.alias);
// Set a default value
if (isNotAnAliasName(context)) {
  context.alias = "UAT";
}
Enter fullscreen mode Exit fullscreen mode

From there you can namespace your environment variables similar to this:

const slackPostPath = process.env["SLACK_POST_PATH_" + context.alias];
const slackBotUsername = process.env.SLACK_BOT_USERNAME + " " + context.alias;
Enter fullscreen mode Exit fullscreen mode

The code

The code might seem dense but this is because there are no dependencies. Set up a lambda with a copy of the linked code and save it. Set up the qualifiers you want along with the appropriate triggers and slack details and bam! Log monitoring on the cheap.

Oldest comments (0)