Skip navigation links

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

AWS APIGatewayv2 Integrations

See: Description

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

AWS APIGatewayv2 Integrations

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object booksFn = Function.Builder.create(stack, "BooksDefaultFn")....build();
 Object booksIntegration = LambdaProxyIntegration.Builder.create()
         .handler(booksDefaultFn)
         .build();
 
 Object httpApi = new HttpApi(stack, "HttpApi");
 
 httpApi.addRoutes(Map.of(
         "path", "/books",
         "methods", List.of(HttpMethod.getGET()),
         "integration", booksIntegration));
 

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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object booksIntegration = HttpProxyIntegration.Builder.create()
         .url("https://get-books-proxy.myproxy.internal")
         .build();
 
 Object httpApi = new HttpApi(stack, "HttpApi");
 
 httpApi.addRoutes(Map.of(
         "path", "/books",
         "methods", List.of(HttpMethod.getGET()),
         "integration", booksIntegration));
 

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:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Vpc vpc = new Vpc(stack, "VPC");
 Object lb = ALB.Builder.create(stack, "lb").vpc(vpc).build();
 Object listener = lb.addListener("listener", Map.of("port", 80));
 listener.addTargets("target", Map.of(
         "port", 80));
 
 Object httpEndpoint = HttpApi.Builder.create(stack, "HttpProxyPrivateApi")
         .defaultIntegration(HttpAlbIntegration.Builder.create()
                 .listener(listener)
                 .build())
         .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:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Vpc vpc = new Vpc(stack, "VPC");
 Object lb = NLB.Builder.create(stack, "lb").vpc(vpc).build();
 Object listener = lb.addListener("listener", Map.of("port", 80));
 listener.addTargets("target", Map.of(
         "port", 80));
 
 Object httpEndpoint = HttpApi.Builder.create(stack, "HttpProxyPrivateApi")
         .defaultIntegration(HttpNlbIntegration.Builder.create()
                 .listener(listener)
                 .build())
         .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:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Vpc vpc = new Vpc(stack, "VPC");
 Object vpcLink = VpcLink.Builder.create(stack, "VpcLink").vpc(vpc).build();
 Object namespace = PrivateDnsNamespace.Builder.create(stack, "Namespace")
         .name("boobar.com")
         .vpc(vpc)
         .build();
 Object service = namespace.createService("Service");
 
 Object httpEndpoint = HttpApi.Builder.create(stack, "HttpProxyPrivateApi")
         .defaultIntegration(HttpServiceDiscoveryIntegration.Builder.create()
                 .vpcLink(vpcLink)
                 .service(service)
                 .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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object httpEndpoint = HttpApi.Builder.create(stack, "HttpProxyPrivateApi")
         .defaultIntegration(HttpAlbIntegration.Builder.create()
                 // ...
                 .requestParameters(new ParameterMapping()
                       .appendHeader('header2', MappingValue.header('header1')).removeHeader("header1"))
                 .build())
         .build();
 

To add mapping keys and values not yet supported by the CDK, use the custom() method:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object httpEndpoint = HttpApi.Builder.create(stack, "HttpProxyPrivateApi")
         .defaultIntegration(HttpAlbIntegration.Builder.create()
                 .listener(listener)
                 .requestParameters(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

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object webSocketApi = new WebSocketApi(stack, "mywsapi");
 WebSocketStage.Builder.create(stack, "mystage")
         .webSocketApi(webSocketApi)
         .stageName("dev")
         .autoDeploy(true)
         .build();
 
 Object messageHandler = Function.Builder.create(stack, "MessageHandler")....build();
 webSocketApi.addRoute("sendmessage", Map.of(
         "integration", LambdaWebSocketIntegration.Builder.create()
                 .handler(connectHandler)
                 .build()));
 
Skip navigation links