Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to approach use of environment variables for Lambda@Edge

I'm building Lambda function for CloudFront which checks if request has cookies, if not then forwards to login page. I need to customize response header location based on environment - for each env that will be different.

Initially I tried with environment variables but I got an error during deployment: InvalidLambdaFunctionAssociation: The function cannot have environment variables So I switched to use aws-sdk with SSM ssm.getParameter but after zipping lambda archive with aws-sdk and one more depedency it's around 13 MB. The limit for Lambda@Edge functions is 1 MB.

I'm wondering would be the best way to approach that. Generate file with environment variables on each Lambda build and require it in index.js?

like image 549
makbol Avatar asked Nov 21 '25 05:11

makbol


1 Answers

Use SSM but don't include AWS SDK in your Lambda function. Lambda documentation says that the AWS SDK is included in the Lambda runtime.

To test this, I created a new Node.js 12 Lambda function from scratch in the Lambda console & replaced its existing code with this:

const AWS = require('aws-sdk');
const SSM = new AWS.SSM();
exports.handler = async() => {
    return {
        statusCode: 200,
        body: await SSM.getParameter({ Name: 'my-param' }).promise(),
    };
};

This works! Downloading the deployment package of this function from the Lambda console showed that it's just 276 bytes in size. I then deployed this to Lambda@Edge & that worked too!

like image 182
Harish KM Avatar answered Nov 23 '25 22:11

Harish KM