cloud:aws:apigateway
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
cloud:aws:apigateway [2023/11/01 07:13] – removed - external edit (Unknown date) 127.0.0.1 | cloud:aws:apigateway [2023/11/01 07:13] (current) – ↷ Page moved from business_process_management:camunda:cloud:aws:apigateway to cloud:aws:apigateway skipidar | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== API Gateway ===== | ||
+ | |||
+ | |||
+ | Real-time applications with API Gateway WebSockets and AWS Lambda | ||
+ | https:// | ||
+ | |||
+ | |||
+ | |||
+ | Calling API gateways via generated SDKs | ||
+ | https:// | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== REST API vs HTTP API ===== | ||
+ | https:// | ||
+ | |||
+ | REST-Api has more functionality regarding inspection. | ||
+ | |||
+ | |||
+ | ===== Lambda behind the API gateway ===== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | |||
+ | ==== Fallpit: Invoke Lambda POST ==== | ||
+ | |||
+ | The API must invoke the backend Lambda function using the **HTTP method POST**. | ||
+ | https:// | ||
+ | |||
+ | Otherwise you will get | ||
+ | * a 500 error on the Gateway | ||
+ | * a 403 error on the Lambda, behind the Gateway https:// | ||
+ | |||
+ | ==== Debugging ==== | ||
+ | |||
+ | If a Lambda does not behave as intended - use the **Test button**. | ||
+ | ONe can see much more exceptions in there. | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | ==== Java Lambda behind the API gateway ==== | ||
+ | https:// | ||
+ | |||
+ | |||
+ | Example: https:// | ||
+ | |||
+ | === 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 ' | ||
+ | implementation ' | ||
+ | testImplementation ' | ||
+ | testImplementation ' | ||
+ | testImplementation ' | ||
+ | testImplementation ' | ||
+ | testRuntimeOnly ' | ||
+ | |||
+ | compile ' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | <sxh java> | ||
+ | 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, | ||
+ | * | ||
+ | * when Lambda is used with the API Gateway - is required. | ||
+ | * | ||
+ | * https:// | ||
+ | * | ||
+ | * @param inputStream | ||
+ | * @param outputStream | ||
+ | * @param context | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public void handleRequest(InputStream inputStream, | ||
+ | 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(" | ||
+ | responseJson.put(" | ||
+ | } | ||
+ | | ||
+ | // example response | ||
+ | JSONObject obj2 = new JSONObject(); | ||
+ | obj2.put(" | ||
+ | responseJson.put(" | ||
+ | responseJson.put(" | ||
+ | responseJson.put(" | ||
+ | |||
+ | // writing the result into the | ||
+ | OutputStreamWriter writer = new OutputStreamWriter(outputStream, | ||
+ | writer.write(responseJson.toString()); | ||
+ | writer.close(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Return format of Lambda === | ||
+ | |||
+ | Lambdas needs to return JSON, to be capable to be used from the gateway. | ||
+ | https:// | ||
+ | |||
+ | <sxh java> | ||
+ | |||
+ | // Handler value: example.Handler | ||
+ | public class Handler implements RequestStreamHandler { | ||
+ | public void handleRequest(InputStream inputStream, | ||
+ | // example response | ||
+ | JSONObject obj2 = new JSONObject(); | ||
+ | obj2.put(" | ||
+ | responseJson.put(" | ||
+ | responseJson.put(" | ||
+ | responseJson.put(" | ||
+ | |||
+ | // writing the result into the | ||
+ | OutputStreamWriter writer = new OutputStreamWriter(outputStream, | ||
+ | writer.write(responseJson.toString()); | ||
+ | writer.close(); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Use Lambda Proxy integration ==== | ||
+ | |||
+ | When a client submits an API request, API Gateway passes to the integrated Lambda function the raw request as-is: | ||
+ | |||
+ | |||
+ | That means, that the option `Use Lambda Proxy integration` | ||
+ | * **deactivates** the **" | ||
+ | * https:// | ||
+ | |||
+ | |||
+ | {{https:// | ||
+ | |||
+ | |||
+ | This is the most powerful integration: | ||
+ | - **\{{proxy}}** sends all resources - to the Lambda | ||
+ | - **ANY** - accepts all methods | ||
+ | |||
+ | https:// | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | * <color # | ||
+ | * <color # | ||
+ | |||
+ | After that the lambda reacts on | ||
+ | < | ||
+ | curl -X POST https:// | ||
+ | curl -X GET https:// | ||
+ | </ | ||
+ | |||
+ | ==== Mapping Templates ==== | ||
+ | |||
+ | Can be used to add custom headers | ||
+ | from the API gateway | ||
+ | to the Lambda | ||
+ | |||
+ | |||
+ | https:// | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Token Exchange ===== | ||
+ | The token exchange is possible with 2 pieces: | ||
+ | |||
+ | * obtain a new token at the lambda authorizer | ||
+ | * set the header via VLT as in | ||
+ | |||
+ | https:// | ||
+ | |||
+ | |||
+ | |||
+ | ===== A Detailed Overview of AWS API Gateway ===== | ||
+ | An overview of the API gateway functions. | ||
+ | https:// | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | {{https:// | ||
+ | |||