简体   繁体   中英

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 ?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM