Lambda@Edge "The Lambda function result failed validation" in CI
CloudFront validates the object a Lambda@Edge function returns against a strict schema for each event type. A wrong status type, a missing field, or an illegal header makes CloudFront reject the result and return an error to the viewer.
What this error means
CloudFront returns a 502 and CloudWatch logs show "The Lambda function result failed validation: The body is not a string", "The status code ... is invalid", or a similar shape error during integration tests in CI.
The Lambda function result failed validation: The body is not a string,
is not an object, or exceeds the maximum size.Common causes
The returned object has the wrong shape
A viewer response must return an object with a string status, a headers map of the right structure, and a string body. A number status or a malformed headers entry fails validation.
A disallowed or oversized header or body
The function sets a read-only header, exceeds the allowed body size for the event, or omits required fields, which CloudFront rejects.
How to fix it
Return the exact required shape
- Check the event type (viewer/origin, request/response) and its schema.
- Return status as a string and headers as the documented key/value-array map.
- Re-run the integration test against CloudFront to confirm it validates.
callback(null, {
status: '302',
statusDescription: 'Found',
headers: { location: [{ key: 'Location', value: '/new' }] },
});Avoid setting read-only headers
Do not write headers CloudFront marks read-only for that event, and keep the generated body within the size limit.
How to prevent it
- Match the returned object to the schema for the specific event type.
- Use string status codes and the documented headers structure.
- Test the function against CloudFront, not just with a local invoke.