Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Akeboshiwind committed May 23, 2024
1 parent ed96ff5 commit de773da
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/pages/blog/clojure-lambda-http.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ API Gateway is a bit of a different beast as you'll soon see, but it's very flex

The first thing to know is there are **three**(?) ways to setup a lambda:

[With the HTTP API](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop.html) during setup point it at your lambda (no Function URL needed!) (also make sure to point it at your alias) with the method & resource path you want and off you go! Note that it **really** doesn't like the response not being json and will return a 500 with `{"message":"Internal Server Error"}.
[With the HTTP API](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop.html) during setup point it at your lambda (no Function URL needed!) (also make sure to point it at your alias) with the method & resource path you want and off you go! Note that it **really** doesn't like the response not being json and will return a 500 with `{"message":"Internal Server Error"}`.

With a REST API first [create a new api](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-rest-new-console.html) using the default lambda integration (i.e. don't check the `Lambda proxy integration` box during setup) (again make sure to use the alias). The thing to note here is that with the default integration, API Gateway is calling the raw `Invoke` request of the Lambda API. This invoke call will return a 200 response even if your lambda throws an uncaught exception(!) and so will your AWS Gateway method. Luckily API Gateway has a way around this, you can add a new `Method Response` with the status code you want to respond with (say `400` for invalid user input), next you add a new `Integration Response` (you can leave the default as it is) set the `Method response status code` to the one you just created and set the `Lambda error regex` to match the error message you want to catch (you can use `(\n|.)+` to match all errors). Note that this error regex will [match against the `errorMessage` key in the **JSON** response from your lambda](https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-method-settings-execution-console.html). Also note that if you throw an uncaught exception it will be formatted [like so](https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html) (which helpfully includes the `exceptionMessage` field).

Expand All @@ -94,7 +94,9 @@ With a REST API first [create a new api](https://docs.aws.amazon.com/apigateway/

And last but certainly not least is the same REST API as above but with the Lambda proxy integration. To create just follow the [same guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-rest-new-console.html) but this time **do** check the `lambda proxy integration` box during setup. Now API Gateway will pass the input as a JSON object with [this format](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format) and expects a response with [this format](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format). Note that now the input **does not** have to be valid json as this will just be put in the request data. Also note that the output **does** have to be valid json, if it's not you'll get a 502 with `{"message": "Internal server error"}`

{/*
---

### My notes (for while writing

AWS Lambda will *always* return a 200 status code, even if an exception is thrown:
https://docs.aws.amazon.com/lambda/latest/dg/java-exceptions.html
Expand Down Expand Up @@ -180,5 +182,3 @@ An alternative layout for this article might be:
| Throwing an uncaught exception | 502 | `{"message": "Internal server error"}` | - |

Maybe add `Valid json in lambda response` and `Invalid json in lambda response`?
*/}

0 comments on commit de773da

Please sign in to comment.