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.
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.
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.
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.
Here's the fastest way to return custom HTTP Status Codes and a custom errorMessage
:
In the API Gateway dashboard, do the following:
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: .*
Your API Gateway route should return this:
HTTP Status Code: 400 JSON Error Response: { errorMessage: "Bad Request: You submitted invalid input" }
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With