Package software.amazon.awscdk.services.apigatewayv2.integrations


@Stability(Experimental) @Deprecated package software.amazon.awscdk.services.apigatewayv2.integrations
Deprecated.

AWS APIGatewayv2 Integrations

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


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 is 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.services.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.myproxy.internal.

 import software.amazon.awscdk.services.apigatewayv2.integrations.HttpUrlIntegration;
 
 
 HttpUrlIntegration booksIntegration = new HttpUrlIntegration("BooksIntegration", "https://get-books-proxy.myproxy.internal");
 
 HttpApi httpApi = new HttpApi(this, "HttpApi");
 
 httpApi.addRoutes(AddRoutesOptions.builder()
         .path("/books")
         .methods(List.of(HttpMethod.GET))
         .integration(booksIntegration)
         .build());
 

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.services.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.services.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.services.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.services.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.services.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 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.services.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());
 
Deprecated: AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2. For more information on how to migrate, see https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html