Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to change the http status codes returned by Amazon API Gateway?

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.

like image 787
MonkeyBonkey Avatar asked Jul 09 '15 22:07

MonkeyBonkey


People also ask

Does AWS gateway support HTTP?

Amazon API Gateway does not support unencrypted (HTTP) endpoints. By default, Amazon API Gateway assigns an internal domain to the API that automatically uses the Amazon API Gateway certificate. When configuring your APIs to run under a custom domain name, you can provide your own certificate for the domain.

Does API gateway allow HTTP?

You can integrate an API method with an HTTP endpoint using the HTTP proxy integration or the HTTP custom integration. API Gateway supports the following endpoint ports: 80, 443 and 1024-65535. With proxy integration, setup is simple.


2 Answers

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.

like image 87
Eric Eijkelenboom Avatar answered Sep 21 '22 15:09

Eric Eijkelenboom


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

like image 31
ac360 Avatar answered Sep 20 '22 15:09

ac360