- Newest
- Most votes
- Most comments
The behavior you're experiencing is expected when working with Lambda@Edge functions. There are a few important points to understand about how Lambda@Edge works and how to ensure your code changes are reflected:
-
Version Management: When you associate a Lambda@Edge function with a CloudFront distribution, you're actually associating a specific version of the function, not the $LATEST version. This means that simply updating the code in the Lambda console won't automatically update the version being used by CloudFront.
-
Publishing New Versions: To see your changes take effect, you need to publish a new version of your Lambda function after making changes. This creates a new, immutable version of your function.
-
Updating CloudFront: After publishing a new version, you need to update your CloudFront distribution to use this new version. This typically involves editing the behavior in your CloudFront distribution and selecting the new function version.
-
Propagation Time: Changes to CloudFront distributions can take some time to propagate globally, usually around 5-10 minutes, but it can sometimes take longer.
To get your code changes reflected:
- Make your changes in the Lambda console.
- Publish a new version of your Lambda function.
- Update your CloudFront distribution to use the new version.
- Wait for the changes to propagate.
Remember, Lambda@Edge functions are replicated globally when associated with a CloudFront distribution, which is why there's an additional step compared to regular Lambda functions. This process ensures that your edge functions remain consistent and performant across all edge locations.
For easier development and debugging, consider setting up a local testing environment that mimics the Lambda@Edge event structure. This can help you iterate more quickly before deploying changes to your production environment.
Sources
Get started with Lambda@Edge functions (console) - Amazon CloudFront
Customize at the edge with Lambda@Edge - Amazon CloudFront
yes you need to Deploy your changes in the Code Editor for them to take effect. Below are the steps which you can follow :
- Make your code changes in the editor
- Click the "Deploy" button to save and apply your changes
- After deployment, run your tests to see the new behavior
Further, sharing one of the ways to setup the local testing environment for Lambda@Edge :
- Create a Mock Event Helper.
// mockEventGenerator.js
const generateViewerRequestEvent = () => ({
Records: [{
cf: {
config: {
distributionDomainName: 'test.cloudfront.net',
distributionId: 'EDFDVBD6EXAMPLE',
eventType: 'viewer-request',
requestId: 'MRVMF7KydIvxMWfJIglgwHQwZsbG2IhRJ07sn9AkfUdSQ=='
},
request: {
clientIp: '2001:cdba::3257:9652',
headers: {
'host': [{ key: 'Host', value: 'test.cloudfront.net' }],
'user-agent': [{ key: 'User-Agent', value: 'Test Agent' }]
},
method: 'GET',
uri: '/test',
querystring: 'test=123'
}
}
}]
});
module.exports = {
generateViewerRequestEvent
};
- Setup Local Test Environment :
// local-test.js
const { handler } = require('./your-lambda-function');
const { generateViewerRequestEvent } = require('./mockEventGenerator');
async function runLocalTest() {
const event = generateViewerRequestEvent();
try {
const result = await handler(event);
console.log('Result:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error:', error);
}
}
runLocalTest();
- Use Jest for Unit Testing:
// your-lambda-function.test.js
const { handler } = require('./your-lambda-function');
const { generateViewerRequestEvent } = require('./mockEventGenerator');
describe('Lambda@Edge Function Tests', () => {
test('handles viewer request correctly', async () => {
const event = generateViewerRequestEvent();
const response = await handler(event);
expect(response).toBeDefined();
// Add more specific assertions
});
});
- Create a Package.json with respective dependency.
{
"scripts": {
"test": "jest",
"local": "node local-test.js"
},
"devDependencies": {
"jest": "^29.0.0"
}
}
answered a year ago
Relevant content
asked 6 years ago
asked 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago

What is the recommended way to setup a local testing environment that mimics the Lambda@Edge event structure?