title: Sending custom status codes from AWS Lambda
body: <p>I have a Lambda@Edge function set up in AWS that checks incoming requests for a given cookie and rejects the request if it's not present, or if it is present it modifies the request for another operation. So far the code works correctly in that the request is 'rejected' if the cookie isn't present, and passes if it is present, but the response I am getting from the Lambda is a 502 which isn't really appropriate for what I'm trying to achieve. Abbreviated code below:</p>
  <pre><code>exports.handler = (event, context, callback) =&gt; {
    const request = event.Records[0].cf.request;
    const requestHeaders = request.headers;
  
    const response = event.Records[0].cf.response;
  
    // Perform cookie check
    const downloadCookie = 'download-cookie=true';
    let downloadCookieFound = false;
  
    if (requestHeaders.cookie) {    
      for (let i = 0; i &lt; requestHeaders.cookie.length; i++) {
        if (requestHeaders.cookie[i].value.indexOf(downloadCookie) &gt;= 0) {
          downloadCookieFound = true;
          break;
        }   
      }
    }
  
    // Reject if cookie not present
    if (!downloadCookieFound) callback(null, request);
  
    // I have tried this as well but it doesn't seem to work
    // if (!downloadCookieFound) callback(null, { statusCode: 403, body: 'Cookie missing' };
  
    // Perform other operations
    // ...
  
    //Return modified response
    callback(null, response);
  };
  </code></pre>
  <p>How do I get the Lambda to return a 403 response instead of this 502:</p>
  <pre><code>502 ERROR
  The request could not be satisfied.
  The Lambda function returned invalid JSON: The JSON output must be an object type. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
  If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
  </code></pre>