4

whenever I am trying to add response headers, the CloudFront throws me the

ERROR Validation error: Lambda function result failed validation, the function tried to delete read-only header, headerName : Content-Length.

ERROR Validation error: Lambda function result failed validation, the function tried to delete read-only header, headerName : Content-Encoding.

const response = {
      status: '302',
      statusDescription: 'Found',
      headers: {
                'location': [{
                              key: 'location',
                              value: 'https://abc.test.io'
                             }],
                 'set-cookie': [{
                               key: 'set-cookie',
                               value: 'sessiontoken='+sessionObjectData.session.sessionId+'; Secure; HttpOnly'
                                }]
                }
  }
callback(null, response)

Can someone let me know what I am doing wrong here ? BTW, I am using viewer-response event

af_khan
  • 1,030
  • 4
  • 25
  • 49

1 Answers1

4

As mentioned on CloudFront functions docs, it is not possible to modify some of the response headers from Edge Functions (including "Content-Length"). What you can try instead is to update only headers you need to change and left others untouched:

const response = event.Records[0].cf.response;
response.status = 302;
response.statusDescription = 'Found';
response.headers['location'] = [{ key: 'Location', value:'https://abc.test.io'}];
lmX2015
  • 400
  • 3
  • 9
  • Worked for me. I was trying to reset the whole response object. But all I wanted to do was a redirect which I did using above method. – Oliver Jul 22 '22 at 06:09