121

For instance if I want to return a specific 400 error for invalid parameters or perhaps a 201 when the lambda function call resulted in a create.

I'd like to have different http status codes but it looks like api gateway always returns a 200 status code even if the lambda function is returning an error.

Charles
  • 1,153
  • 1
  • 15
  • 27
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • 2
    so it looks like the issue I was having was that I was returning a custom error type - which makes the errorMessage regex not work correctly. Returning a standard string in the fail response from lambda will make the below solution work - returning your own custom error object however, will not. – MonkeyBonkey Jul 13 '15 at 21:29
  • my solution was to switch from Serveless version 0.5 to 1.0. Also, I'm using the response from Serveless documentation, specifying the statusCode in the response object as a property. Hope it helps – Relu Mesaros Oct 12 '16 at 15:39

12 Answers12

109

Update per 20-9-2016

Amazon finally made this easy using the Lambda Proxy integration. This allows your Lambda function to return proper HTTP codes and headers:

let response = {
    statusCode: '400',
    body: JSON.stringify({ error: 'you messed up!' }),
    headers: {
        'Content-Type': 'application/json',
    }
};

context.succeed(response);

Say goodbye request/response mapping in the API Gateway!

Option 2

Integrate an existing Express app with Lambda/API Gateway using aws-serverless-express.

Eric Eijkelenboom
  • 6,943
  • 2
  • 25
  • 29
  • 1
    I can't integrate it, I mean, I get 200 status and the created response(the created error). Am I missing something ? How does the "s-function.json" looks like ? – Relu Mesaros Oct 11 '16 at 14:48
  • For the simplest example, look at AWS's own Lambda blueprint called microservice-http-endpoint (in the AWS Lambda console). You mention "s-function.json", which sounds like you are using the Serverless framework (https://serverless.com/). This is another beast entirely and should not be confused with aws-serverless-express or 'raw' Lambda/API Gateway. My answer does not describe how to make this work using the Serverless framework. – Eric Eijkelenboom Oct 11 '16 at 18:21
  • yep, aws-serverless-express is another 'beast', I'm reading about now .. the 'express' module is required in order to use aws-serverless-express ? – Relu Mesaros Oct 12 '16 at 08:48
  • 9
    For anyone wondering, this can also be achieved using the new `callback` style. Just do `callback(null, {statusCode: 200, body: 'whatever'})`. – Widdershin May 11 '17 at 03:46
  • 1
    @unclemeat I have the same questions. Did you figure it out? How to do this in python? – Sushil Jul 06 '17 at 14:21
  • 1
    @Sushil yeah, you just return the JSON like in the response variable above. – unclemeat Jul 07 '17 at 22:56
  • 9
    @Sushil I have solved this in Python with LambdaProxyIntegration and returning `return { "isBase64Encoded": True, "statusCode": 200, "headers": { }, "body": "" }` – Jithu R Jacob Aug 05 '17 at 10:15
  • You also need to make sure you have "Use Lambda Proxy integration" turned on in the "Integration Request" settings. – Casey Gibson Jun 15 '18 at 00:09
  • If you're using Java you need to return a POJO object class and not a String JSON – rboy Sep 12 '19 at 22:11
  • I have another one issue here -- I want to return any of 1701 or 13550 status codes, but it is always 502! – donutello Jan 23 '21 at 10:33
  • 1
    those aren't valid status codes... https://developer.mozilla.org/en-US/docs/Web/HTTP/Status – Ryan Schaefer Jan 28 '21 at 23:48
79

Here's the fastest way to return custom HTTP Status Codes and a custom errorMessage:

In the API Gateway dashboard, do the following:

  1. In the method for your resource, click on method response
  2. In the HTTP Status table, click add response and add in each HTTP Status Code you would like to use.
  3. In the method for your resource, click on integration response
  4. Add an integration response for each of the HTTP Status Codes you created earlier. Make sure input passthrough is checked. Use lambda error regex to identify which status code should be used when you return an error message from your lambda function. For example:

    // Return An Error Message String In Your Lambda Function
    
    return context.fail('Bad Request: You submitted invalid input');
    
    // Here is what a Lambda Error Regex should look like.
    // Be sure to include the period and the asterisk so any text
    // after your regex is mapped to that specific HTTP Status Code
    
    Bad Request: .*
    
  5. Your API Gateway route should return this:

    HTTP Status Code: 400
    JSON Error Response: 
        {
            errorMessage: "Bad Request: You submitted invalid input"
        }
    
  6. I see no way to copy these settings and re-use it for different methods, so we have much annoying redundant manual inputting to do!

My Integration Responses look like this:

aws api gateway lambda error response handling

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
ac360
  • 7,735
  • 13
  • 52
  • 91
  • 3
    so it looks like my issue was that the regex trigger was never working since I return an error object from lambda in the fail method, rather than just a string. e.g. `return context.fail(new Error('bad one'))` – MonkeyBonkey Jul 13 '15 at 21:30
  • MonkeyBonkey, It also may be possible to return an error object by using Output Mapping. I'm going to dig into this today and possibly update this answer if it works. – ac360 Jul 14 '15 at 15:13
  • I follow this, but my Lambda func calls 3rd party API, returns JSON like this: { "code": 401, "error": "The access token provided has expired" } I'm trying to work out a way to parse out and set the HTTP Response Code (ie: 401) and the message, without having to add a Response Method for each possible server message. All I get in the Output Mapping Template is the composited error message from the context.fail(errorMsg), and I can't see a way to "parse" out the code & message to generate the JSON output. – Darren Ehlers Jul 21 '15 at 20:19
  • What if your lambda function returns an error object instead of a string? I want to return validation messages to indicate all (possible) errors. – kalisjoshua Oct 06 '15 at 17:23
  • Most of my experimentation and research has indicated that your lambda function can't really return an object, at least not if you want to use with API Gateway. No matter what you do, context.fail will always be transformed into { "errorMessage" : } OR { "errorMessage": , "errorType": , "stackTrace": blahblahblah} if your code threw an error. the only way that api gateway can allow for different status codes is to basically do regex tests on specifically the "errorMessage" property – Akshay Dhalwala Oct 30 '15 at 23:31
  • 8
    @kalisjoshua I recently published a fairly detailed post on error handling with API Gateway/Lambda: http://www.jayway.com/2015/11/07/error-handling-in-api-gateway-and-aws-lambda/ – Carl Nov 06 '15 at 22:28
  • 9
    What's the equivalent of context.fail for Python Lambda's? – routeburn Nov 09 '15 at 01:49
  • 1
    For python: raise an Exception. See http://docs.aws.amazon.com/lambda/latest/dg/python-exceptions.html – devxoul Jan 07 '16 at 06:26
  • 1
    Is there no way to change the status code in non-error responses? What if I wanted to send "201 Created" along with the created object? – Ben Davis Jan 08 '16 at 06:11
  • @BenDavis then i believe you'd make 201 the default response code (instead of 200) and it would be what gets returned for all non-error responses. – Cory Mawhorter Jan 08 '16 at 09:09
  • 1
    @CoryMawhorter that doesn't really solve the problem. A PUT request on a resource could result in either `200 OK` or `201 CREATED`. – Ben Davis Jan 10 '16 at 04:09
  • @BenDavis AFAIK that's not possible. You seem to be able to set all other things with api gateway other than the response code. In fact, the whole default response thing is pretty bad and leads to unforeseen error responses being treated as successes. It all feels extremely clunky. – Cory Mawhorter Jan 11 '16 at 06:01
  • @CoryMawhorter I have same issue, I need to send 202 for asynchronous request. Can you let me know how to do it – UserJ Sep 04 '20 at 17:48
21

To be able to return a custom error object as JSON you have to jump through a couple of hoops.

First, you must fail the Lambda and pass it a stringified JSON object:

exports.handler = function(event, context) {
    var response = {
        status: 400,
        errors: [
            {
              code:   "123",
              source: "/data/attributes/first-name",
              message:  "Value is too short",
              detail: "First name must contain at least three characters."
            },
            {
              code:   "225",
              source: "/data/attributes/password",
              message: "Passwords must contain a letter, number, and punctuation character.",
              detail: "The password provided is missing a punctuation character."
            },
            {
              code:   "226",
              source: "/data/attributes/password",
              message: "Password and password confirmation do not match."
            }
        ]
    }

    context.fail(JSON.stringify(response));
};

Next, you setup the regex mapping for each of the status codes you would like to return. Using the object I defined above you would setup this regex for 400:

.*"status":400.*

Finally, you setup a Mapping Template to extract the JSON response from the errorMessage property returned by Lambda. The Mapping Template looks like this:

$input.path('$.errorMessage')

I wrote an article on this that goes into more detail and explains the response flow from Lambda to API Gateway here: http://kennbrodhagen.net/2016/03/09/how-to-return-a-custom-error-object-and-status-code-from-api-gateway-with-lambda/

kennbrodhagen
  • 4,258
  • 2
  • 26
  • 21
  • @kennbrodhagen do you know about API Gateway and Java Lambdas? I'm using a kind of the same reg exp, and it is not working for me. I use .*statusCode":422.* – Perimosh Jul 19 '17 at 20:02
  • @Perimosh check out this article that explains how to do this with Java Exceptions: https://aws.amazon.com/blogs/compute/redirection-in-a-serverless-api-with-aws-lambda-and-amazon-api-gateway/ – kennbrodhagen Jul 21 '17 at 13:05
11

1) Configure your API Gateway resource to use Lambda Proxy Integration by checking the checkbox labeled "Use Lambda Proxy integration" on the "Integration Request" screen of the API Gateway resource definition. (Or define it in your cloudformation/terraform/serverless/etc config)

2) Change your lambda code in 2 ways

  • Process the incoming event (1st function argument) appropriately. It is no longer just the bare payload, it represents the entire HTTP request including headers, query string, and body. Sample below. Key point is that JSON bodies will be strings requiring explicit JSON.parse(event.body) call (don't forget try/catch around that). Example is below.
  • Respond by calling the callback with null then a response object that provides the HTTP details including statusCode, body, and headers.
    • body should be a string, so do JSON.stringify(payload) as needed
    • statusCode can be a number
    • headers is an object of header names to values

Sample Lambda Event Argument for Proxy Integration

{
    "resource": "/example-path",
    "path": "/example-path",
    "httpMethod": "POST",
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "US",
        "Content-Type": "application/json",
        "Host": "exampleapiid.execute-api.us-west-2.amazonaws.com",
        "User-Agent": "insomnia/4.0.12",
        "Via": "1.1 9438b4fa578cbce283b48cf092373802.cloudfront.net (CloudFront)",
        "X-Amz-Cf-Id": "oCflC0BzaPQpTF9qVddpN_-v0X57Dnu6oXTbzObgV-uU-PKP5egkFQ==",
        "X-Forwarded-For": "73.217.16.234, 216.137.42.129",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "queryStringParameters": {
        "bar": "BarValue",
        "foo": "FooValue"
    },
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "accountId": "666",
        "resourceId": "xyz",
        "stage": "dev",
        "requestId": "5944789f-ce00-11e6-b2a2-dfdbdba4a4ee",
        "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "apiKey": null,
            "sourceIp": "73.217.16.234",
            "accessKey": null,
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "insomnia/4.0.12",
            "user": null
        },
        "resourcePath": "/example-path",
        "httpMethod": "POST",
        "apiId": "exampleapiid"
    },
    "body": "{\n  \"foo\": \"FOO\",\n  \"bar\": \"BAR\",\n  \"baz\": \"BAZ\"\n}\n",
    "isBase64Encoded": false
}

Sample Callback Response Shape

callback(null, {
  statusCode: 409,
  body: JSON.stringify(bodyObject),
  headers: {
    'Content-Type': 'application/json'
  }
})

Notes - I believe the methods on context such as context.succeed() are deprecated. They are no longer documented although they do still seem to work. I think coding to the callback API is the correct thing going forward.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 2
    This does not work. I still get 200 status returned with this entire response output. Cannot set the api to actually return 409 status – Andy N Apr 15 '20 at 17:07
  • The problem with 4xx and 5xx using this setup is usually related to CORS. [This is the way to fix it](https://serverless-stack.com/chapters/handle-api-gateway-cors-errors.html). – Ricardo Nolde Jan 17 '22 at 05:41
10

I wanted an error from Lambda to be proper 500 error, after doing a lot of research, came up with the below, that works:

On LAMBDA

For a good response, I am returning as below:

exports.handler = (event, context, callback) => {
    // ..

    var someData1 =  {
        data: {
            httpStatusCode: 200,
            details: [
                {
                    prodId: "123",
                    prodName: "Product 1"
                },
                {
                    "more": "213",
                    "moreDetails": "Product 2"
                }
            ]
        }
    };
    return callback(null, someData1);
}

For a bad response, returning as below

exports.handler = (event, context, callback) => {
    // ..

    var someError1 = {
        error: {
            httpStatusCode: 500,
            details: [
                {
                    code: "ProductNotFound",
                    message: "Product not found in Cart",
                    description: "Product should be present after checkout, but not found in Cart",
                    source: "/data/attributes/product"
                },
                {
                    code: "PasswordConfirmPasswordDoesntMatch",
                    message: "Password and password confirmation do not match.",
                    description: "Password and password confirmation must match for registration to succeed.",
                    source: "/data/attributes/password",
                }
            ]
        }
    };

    return callback(new Error(JSON.stringify(someError1)));
}

On API Gateway

For a GET METHOD, say GET of /res1/service1:

Through Method Response > Add Response, added 3 responses:
- 200
- 300
- 400

Then,

Through 'Integration Response' > 'Add integration response', create a Regex for 400 errors (client error):

Lambda Error Regex    .*"httpStatusCode":.*4.*

'Body Mapping Templates' > Add mapping template as:  
    Content-Type                 application/json  
    Template text box*           $input.path('$.errorMessage')  


Similarly, create a Regex for 500 errors (server error):

Lambda Error Regex    .*"httpStatusCode":.*5.*

'Body Mapping Templates' > Add mapping template as:  
    Content-Type                 application/json  
    Template text box*           $input.path('$.errorMessage')  

Now, publish /res1/service1, hit the published URL, that is connected to above lambda

Used Advanced REST client (or Postman) chrome plugin, you will see proper http codes like server error (500) or 400, instead of 200 http response code for all requests where were given in "httpStatusCode".

From the 'Dashboard' of API, in API Gateway, we can see the http status codes like below:

400 & 500 errors

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
8

The easiest way to do this is to use LAMBDA_PROXY integration. Using this method, you don't need any special transformations to be set into API Gateway pipeline.

Your return object would have to be similar to the snippet below:

module.exports.lambdaHandler = (event, context, done) => {
    // ...
    let response = {
        statusCode: 200, // or any other HTTP code
        headers: {       // optional
             "any-http-header" : "my custom header value"
        },
        body: JSON.stringify(payload) // data returned by the API Gateway endpoint
    };
    done(null, response); // always return as a success
};

It does have a few drawbacks: as having to be specially careful about error handling, and coupling your lambda function to the API Gateway endpoint; that said, if you were not really going to use it anywhere else, it is not that much of a problem.

Ricardo Nolde
  • 33,390
  • 4
  • 36
  • 40
7

For those who tried everything put on this question and couldn't make this work (like me), check the thedevkit comment on this post (saved my day):

https://forums.aws.amazon.com/thread.jspa?threadID=192918

Reproducing it entirely below:

I've had issues with this myself, and I believe that the newline characters are the culprit.

foo.* will match occurrences of "foo" followed by any characters EXCEPT newline. Typically this is solved by adding the '/s' flag, i.e. "foo.*/s", but the Lambda error regex doesn't seem to respect this.

As an alternative you can use something like: foo(.|\n)*

Carlos Ballock
  • 145
  • 1
  • 3
5

If you do not want to use a proxy, you can use this template:

#set($context.responseOverride.status =  $input.path('$.statusCode'))
George Ogden
  • 596
  • 9
  • 15
2

This is how it's recommended on an AWS Compute Blog if using API Gateway. Checking to see if integration works with direct Lambda invocation.

var myErrorObj = {
    errorType : "InternalServerError",
    httpStatus : 500,
    requestId : context.awsRequestId,
    message : "An unknown error has occurred. Please try again."
}
callback(JSON.stringify(myErrorObj));

For direct Lambda invocations, this appears to be the best solution parsing on the client-side.

spakmad
  • 880
  • 6
  • 15
  • what if the example was a lambda to lambda call. is this still what the called lambda would return? and how can i read that httpStatus on the calling lambda. – Rod Aug 21 '18 at 05:47
1

I'm using serverless 0.5. This is how it works, for my case

s-function.json:

{
  "name": "temp-err-test",
  "description": "Deployed",
  "runtime": "nodejs4.3",
  "handler": "path/to/handler.handler",
  "timeout": 6,
  "memorySize": 1024,
  "endpoints": [
    {
      "path": "test-error-handling",
      "method": "GET",
      "type": "AWS_PROXY",
      "responses": {
        "default": {
          "statusCode": "200"
        }
      }
    }
  ]
}

handler.js:

'use strict';
function serveRequest(event, context, cb) {
  let response = {
    statusCode: '400',
    body: JSON.stringify({ event, context }),
    headers: {
      'Content-Type': 'application/json',
    }
  };
  cb(null, response);
}
module.exports.handler = serveRequest;
Relu Mesaros
  • 4,952
  • 4
  • 25
  • 33
1

Valid as of Feb 2021

The easiest way to set custom HTTP status code is to setup a Lambda Proxy Integration in API Gateway.

In API Gateway > Resource > Actions Dropdown > Create Method > tick Lambda Proxy Integration and select appropriate Lambda function.

API Gateway enter image description here

enter image description here

Lambda

For async functions just return with an object with statusCode and body. For sync function use callback(null,obj); refer full documentation.

export const dummyFunction = async (event, context, callback) => 
{
 // ... logic
   return {
   statusCode: 400,
   body: JSON.stringify({...data}),
   }
};

Result

Custom status code of 400.

enter image description here

HUGO-DEV
  • 897
  • 8
  • 20
1

I have an express application and have used api gateway infront of it with http integration. To return status code from my application instead of 200 OK , I have simply added the http status code returned by the error handler of my application to the http status regex in integration response section and it works fine. Make sure your application properly handles error. enter image description here

Vienna
  • 61
  • 1
  • 4