This is an old revision of the document!
−Table of Contents
API Gateway
Real-time applications with API Gateway WebSockets and AWS Lambda https://serverless.com/blog/api-gateway-websockets-support/
Calling API gateways via generated SDKs https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk-javascript.html
REST API vs HTTP API
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html
REST-Api has more functionality regarding inspection.
Lambda behind the API gateway
Invoke Lambda POST
The API must invoke the backend Lambda function using the HTTP method POST. https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax
Otherwise you will get
- a 500 error on the Gateway
- a 403 error on the Lambda, behind the Gateway https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-lambda-template-invoke-error/
Debugging
If a Lambda does not behave as intended - use the Test button. ONe can see much more exceptions in there.
Java Lambda behind the API gateway
https://www.baeldung.com/aws-lambda-api-gateway
Example: https://github.com/skipidar/aws-javalambda-behind-apigateway
Java signature
There are obligations on the Java-Lambda signature, when executed behind Gateway.
It is important to have
- InputStream inputStream,
- OutputStream outputStream
in the interface of the Lambda - otherwise the API Gateway will throw exceptions
Dependencies
dependencies { implementation 'com.amazonaws:aws-lambda-java-core:1.2.1' implementation 'com.google.code.gson:gson:2.8.6' testImplementation 'org.apache.logging.log4j:log4j-api:2.13.0' testImplementation 'org.apache.logging.log4j:log4j-core:2.13.0' testImplementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.13.0' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0' compile 'com.googlecode.json-simple:json-simple:1.1.1' }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package example; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestStreamHandler; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.nimbusds.oauth2.sdk.TokenRequest; import no.nav.security.mock.oauth2.MockOAuth2Server; import no.nav.security.mock.oauth2.token.OAuth2TokenCallback; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.io.*; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; // Handler value: example.Handler public class Handler implements RequestStreamHandler { /** * The handleRequest interface, as below, * with InputStream, OutputStream * * when Lambda is used with the API Gateway - is required. * * * @param inputStream * @param outputStream * @param context * @throws IOException */ public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { JSONParser parser = new JSONParser(); BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); JSONObject responseJson = new JSONObject(); JSONObject event = null ; try { event = (JSONObject) parser.parse(reader); handleEvent(event, responseJson, inputStream, outputStream, context); } catch (ParseException e) { responseJson.put( "statusCode" , 400 ); responseJson.put( "exception" , e); } // example response JSONObject obj2 = new JSONObject(); obj2.put( "Content-Type" , "application/json" ); responseJson.put( "statusCode" , 200 ); responseJson.put( "headers" , obj2); responseJson.put( "body" , "hello world" ); // writing the result into the OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8" ); writer.write(responseJson.toString()); writer.close(); } |
Return structure
Lambdas needs to return JSON, to be capable to be used from the gateway.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Handler value: example.Handler public class Handler implements RequestStreamHandler { public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { // example response JSONObject obj2 = new JSONObject(); obj2.put( "Content-Type" , "application/json" ); responseJson.put( "statusCode" , 200 ); responseJson.put( "headers" , obj2); responseJson.put( "body" , "hello world" ); // writing the result into the OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8" ); writer.write(responseJson.toString()); writer.close(); |