Package software.amazon.awscdk.aws_apigatewayv2_integrations
AWS APIGatewayv2 Integrations
Table of Contents
HTTP APIs
Integrations connect a route to backend resources. HTTP APIs support Lambda proxy, AWS service, and HTTP proxy integrations. HTTP proxy integrations are also known as private integrations.
Lambda
Lambda integrations enable integrating an HTTP API route with a Lambda function. When a client invokes the route, the API Gateway service forwards the request to the Lambda function and returns the function's response to the client.
The API Gateway service will invoke the Lambda function with an event payload of a specific format. The service expects the function to respond in a specific format. The details on this format are available at Working with AWS Lambda proxy integrations.
The following code configures a route GET /books
with a Lambda proxy integration.
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpLambdaIntegration; Function booksDefaultFn; HttpLambdaIntegration booksIntegration = new HttpLambdaIntegration("BooksIntegration", booksDefaultFn); HttpApi httpApi = new HttpApi(this, "HttpApi"); httpApi.addRoutes(AddRoutesOptions.builder() .path("/books") .methods(List.of(HttpMethod.GET)) .integration(booksIntegration) .build());
HTTP Proxy
HTTP Proxy integrations enables connecting an HTTP API route to a publicly routable HTTP endpoint. When a client invokes the route, the API Gateway service forwards the entire request and response between the API Gateway endpoint and the integrating HTTP endpoint. More information can be found at Working with HTTP proxy integrations.
The following code configures a route GET /books
with an HTTP proxy integration to an HTTP endpoint
get-books-proxy.example.com
.
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpUrlIntegration; HttpUrlIntegration booksIntegration = new HttpUrlIntegration("BooksIntegration", "https://get-books-proxy.example.com"); HttpApi httpApi = new HttpApi(this, "HttpApi"); httpApi.addRoutes(AddRoutesOptions.builder() .path("/books") .methods(List.of(HttpMethod.GET)) .integration(booksIntegration) .build());
StepFunctions Integration
Step Functions integrations enable integrating an HTTP API route with AWS Step Functions. This allows the HTTP API to start state machine executions synchronously or asynchronously, or to stop executions.
When a client invokes the route configured with a Step Functions integration, the API Gateway service interacts with the specified state machine according to the integration subtype (e.g., starts a new execution, synchronously starts an execution, or stops an execution) and returns the response to the client.
The following code configures a Step Functions integrations:
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpStepFunctionsIntegration; import software.amazon.awscdk.services.stepfunctions.*; StateMachine stateMachine; HttpApi httpApi; httpApi.addRoutes(AddRoutesOptions.builder() .path("/start") .methods(List.of(HttpMethod.POST)) .integration(HttpStepFunctionsIntegration.Builder.create("StartExecutionIntegration") .stateMachine(stateMachine) .subtype(HttpIntegrationSubtype.STEPFUNCTIONS_START_EXECUTION) .build()) .build()); httpApi.addRoutes(AddRoutesOptions.builder() .path("/start-sync") .methods(List.of(HttpMethod.POST)) .integration(HttpStepFunctionsIntegration.Builder.create("StartSyncExecutionIntegration") .stateMachine(stateMachine) .subtype(HttpIntegrationSubtype.STEPFUNCTIONS_START_SYNC_EXECUTION) .build()) .build()); httpApi.addRoutes(AddRoutesOptions.builder() .path("/stop") .methods(List.of(HttpMethod.POST)) .integration(HttpStepFunctionsIntegration.Builder.create("StopExecutionIntegration") .stateMachine(stateMachine) .subtype(HttpIntegrationSubtype.STEPFUNCTIONS_STOP_EXECUTION) // For the `STOP_EXECUTION` subtype, it is necessary to specify the `executionArn`. .parameterMapping(new ParameterMapping().custom("ExecutionArn", "$request.querystring.executionArn")) .build()) .build());
Note:
- The
executionArn
parameter is required for theSTOP_EXECUTION
subtype. It is necessary to specify theexecutionArn
in theparameterMapping
property of theHttpStepFunctionsIntegration
object. START_SYNC_EXECUTION
subtype is only supported for EXPRESS type state machine.
Private Integration
Private integrations enable integrating an HTTP API route with private resources in a VPC, such as Application Load Balancers or Amazon ECS container-based applications. Using private integrations, resources in a VPC can be exposed for access by clients outside of the VPC.
The following integrations are supported for private resources in a VPC.
Application Load Balancer
The following code is a basic application load balancer private integration of HTTP API:
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpAlbIntegration; Vpc vpc = new Vpc(this, "VPC"); ApplicationLoadBalancer lb = ApplicationLoadBalancer.Builder.create(this, "lb").vpc(vpc).build(); ApplicationListener listener = lb.addListener("listener", BaseApplicationListenerProps.builder().port(80).build()); listener.addTargets("target", AddApplicationTargetsProps.builder() .port(80) .build()); HttpApi httpEndpoint = HttpApi.Builder.create(this, "HttpProxyPrivateApi") .defaultIntegration(new HttpAlbIntegration("DefaultIntegration", listener)) .build();
When an imported load balancer is used, the vpc
option must be specified for HttpAlbIntegration
.
Network Load Balancer
The following code is a basic network load balancer private integration of HTTP API:
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpNlbIntegration; Vpc vpc = new Vpc(this, "VPC"); NetworkLoadBalancer lb = NetworkLoadBalancer.Builder.create(this, "lb").vpc(vpc).build(); NetworkListener listener = lb.addListener("listener", BaseNetworkListenerProps.builder().port(80).build()); listener.addTargets("target", AddNetworkTargetsProps.builder() .port(80) .build()); HttpApi httpEndpoint = HttpApi.Builder.create(this, "HttpProxyPrivateApi") .defaultIntegration(new HttpNlbIntegration("DefaultIntegration", listener)) .build();
When an imported load balancer is used, the vpc
option must be specified for HttpNlbIntegration
.
Cloud Map Service Discovery
The following code is a basic discovery service private integration of HTTP API:
import software.amazon.awscdk.services.servicediscovery.*; import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpServiceDiscoveryIntegration; Vpc vpc = new Vpc(this, "VPC"); VpcLink vpcLink = VpcLink.Builder.create(this, "VpcLink").vpc(vpc).build(); PrivateDnsNamespace namespace = PrivateDnsNamespace.Builder.create(this, "Namespace") .name("boobar.com") .vpc(vpc) .build(); Service service = namespace.createService("Service"); HttpApi httpEndpoint = HttpApi.Builder.create(this, "HttpProxyPrivateApi") .defaultIntegration(HttpServiceDiscoveryIntegration.Builder.create("DefaultIntegration", service) .vpcLink(vpcLink) .build()) .build();
Request Parameters
Request parameter mapping allows API requests from clients to be modified before they reach backend integrations. Parameter mapping can be used to specify modifications to request parameters. See Transforming API requests and responses.
The following example creates a new header - header2
- as a copy of header1
and removes header1
.
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpAlbIntegration; ApplicationLoadBalancer lb; ApplicationListener listener = lb.addListener("listener", BaseApplicationListenerProps.builder().port(80).build()); listener.addTargets("target", AddApplicationTargetsProps.builder() .port(80) .build()); HttpApi httpEndpoint = HttpApi.Builder.create(this, "HttpProxyPrivateApi") .defaultIntegration(HttpAlbIntegration.Builder.create("DefaultIntegration", listener) .parameterMapping(new ParameterMapping().appendHeader("header2", MappingValue.requestHeader("header1")).removeHeader("header1")) .build()) .build();
To add mapping keys and values not yet supported by the CDK, use the custom()
method:
import software.amazon.awscdk.aws_apigatewayv2_integrations.HttpAlbIntegration; ApplicationLoadBalancer lb; ApplicationListener listener = lb.addListener("listener", BaseApplicationListenerProps.builder().port(80).build()); listener.addTargets("target", AddApplicationTargetsProps.builder() .port(80) .build()); HttpApi httpEndpoint = HttpApi.Builder.create(this, "HttpProxyPrivateApi") .defaultIntegration(HttpAlbIntegration.Builder.create("DefaultIntegration", listener) .parameterMapping(new ParameterMapping().custom("myKey", "myValue")) .build()) .build();
WebSocket APIs
WebSocket integrations connect a route to backend resources. The following integrations are supported in the CDK.
Lambda WebSocket Integration
Lambda integrations enable integrating a WebSocket API route with a Lambda function. When a client connects/disconnects or sends a message specific to a route, the API Gateway service forwards the request to the Lambda function
The API Gateway service will invoke the Lambda function with an event payload of a specific format.
The following code configures a sendMessage
route with a Lambda integration
import software.amazon.awscdk.aws_apigatewayv2_integrations.WebSocketLambdaIntegration; Function messageHandler; WebSocketApi webSocketApi = new WebSocketApi(this, "mywsapi"); WebSocketStage.Builder.create(this, "mystage") .webSocketApi(webSocketApi) .stageName("dev") .autoDeploy(true) .build(); webSocketApi.addRoute("sendMessage", WebSocketRouteOptions.builder() .integration(new WebSocketLambdaIntegration("SendMessageIntegration", messageHandler)) .build());
AWS WebSocket Integration
AWS type integrations enable integrating with any supported AWS service. This is only supported for WebSocket APIs. When a client connects/disconnects or sends a message specific to a route, the API Gateway service forwards the request to the specified AWS service.
The following code configures a $connect
route with a AWS integration that integrates with a dynamodb table. On websocket api connect,
it will write new entry to the dynamodb table.
import software.amazon.awscdk.aws_apigatewayv2_integrations.WebSocketAwsIntegration; import software.amazon.awscdk.services.dynamodb.*; import software.amazon.awscdk.services.iam.*; Role apiRole; Table table; WebSocketApi webSocketApi = new WebSocketApi(this, "mywsapi"); WebSocketStage.Builder.create(this, "mystage") .webSocketApi(webSocketApi) .stageName("dev") .autoDeploy(true) .build(); webSocketApi.addRoute("$connect", WebSocketRouteOptions.builder() .integration(WebSocketAwsIntegration.Builder.create("DynamodbPutItem") .integrationUri(String.format("arn:aws:apigateway:%s:dynamodb:action/PutItem", this.region)) .integrationMethod(HttpMethod.POST) .credentialsRole(apiRole) .requestTemplates(Map.of( "application/json", JSON.stringify(Map.of( "TableName", table.getTableName(), "Item", Map.of( "id", Map.of( "S", "$context.requestId")))))) .build()) .build());
You can also set additional properties to change the behavior of your integration, such as contentHandling
.
See Working with binary media types for WebSocket APIs.
Import Issues
jsiirc.json
file is missing during the stablization process of this module, which caused import issues for DotNet and Java users who attempt to use this module. Unfortunately, to guarantee backward compatibility, we cannot simply correct the namespace for DotNet or package for Java. The following outlines the workaround.
DotNet Namespace
Instead of the conventional namespace Amazon.CDK.AWS.Apigatewayv2.Integrations
, you would need to use the following namespace:
using Amazon.CDK.AwsApigatewayv2Integrations;
Java Package
Instead of conventional package import software.amazon.awscdk.services.apigatewayv2_integrations.*
, you would need to use the following package:
import software.amazon.awscdk.aws_apigatewayv2_integrations.*; // If you want to import a specific construct import software.amazon.awscdk.aws_apigatewayv2_integrations.WebSocketAwsIntegration;
-
ClassDescriptionThe Application Load Balancer integration resource for HTTP API.A fluent builder for
HttpAlbIntegration
.Properties to initializeHttpAlbIntegration
.A builder forHttpAlbIntegrationProps
An implementation forHttpAlbIntegrationProps
The Lambda Proxy integration resource for HTTP API.A fluent builder forHttpLambdaIntegration
.Lambda Proxy integration properties.A builder forHttpLambdaIntegrationProps
An implementation forHttpLambdaIntegrationProps
The Network Load Balancer integration resource for HTTP API.A fluent builder forHttpNlbIntegration
.Properties to initializeHttpNlbIntegration
.A builder forHttpNlbIntegrationProps
An implementation forHttpNlbIntegrationProps
Base options for private integration.A builder forHttpPrivateIntegrationOptions
An implementation forHttpPrivateIntegrationOptions
The Service Discovery integration resource for HTTP API.A fluent builder forHttpServiceDiscoveryIntegration
.Properties to initializeHttpServiceDiscoveryIntegration
.A builder forHttpServiceDiscoveryIntegrationProps
An implementation forHttpServiceDiscoveryIntegrationProps
The StepFunctions integration resource for HTTP API.A fluent builder forHttpStepFunctionsIntegration
.Properties to initializeHttpStepFunctionsIntegration
.A builder forHttpStepFunctionsIntegrationProps
An implementation forHttpStepFunctionsIntegrationProps
The HTTP Proxy integration resource for HTTP API.A fluent builder forHttpUrlIntegration
.Properties to initialize a newHttpProxyIntegration
.A builder forHttpUrlIntegrationProps
An implementation forHttpUrlIntegrationProps
AWS WebSocket AWS Type Integration.A fluent builder forWebSocketAwsIntegration
.Props for AWS type integration for a WebSocket Api.A builder forWebSocketAwsIntegrationProps
An implementation forWebSocketAwsIntegrationProps
Lambda WebSocket Integration.A fluent builder forWebSocketLambdaIntegration
.Props for Lambda type integration for a WebSocket Api.A builder forWebSocketLambdaIntegrationProps
An implementation forWebSocketLambdaIntegrationProps
Mock WebSocket Integration.