Package: aws-cdk.aws-apigatewayv2-integrations
AWS APIGatewayv2 Integrations---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 theMigrating to AWS CDK v2guide.Table of ContentsHTTP APIsLambda IntegrationHTTP Proxy IntegrationPrivate IntegrationRequest ParametersWebSocket APIsLambda WebSocket IntegrationHTTP APIsIntegrations 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.LambdaLambda 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 atWorking with AWS Lambda
proxy integrations.The following code configures a routeGET /bookswith a Lambda proxy integration.fromaws_cdk.aws_apigatewayv2_integrationsimportHttpLambdaIntegration# books_default_fn: lambda.Functionbooks_integration=HttpLambdaIntegration("BooksIntegration",books_default_fn)http_api=apigwv2.HttpApi(self,"HttpApi")http_api.add_routes(path="/books",methods=[apigwv2.HttpMethod.GET],integration=books_integration)HTTP ProxyHTTP 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 atWorking with HTTP proxy
integrations.The following code configures a routeGET /bookswith an HTTP proxy integration to an HTTP endpointget-books-proxy.myproxy.internal.fromaws_cdk.aws_apigatewayv2_integrationsimportHttpUrlIntegrationbooks_integration=HttpUrlIntegration("BooksIntegration","https://get-books-proxy.myproxy.internal")http_api=apigwv2.HttpApi(self,"HttpApi")http_api.add_routes(path="/books",methods=[apigwv2.HttpMethod.GET],integration=books_integration)Private IntegrationPrivate 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 BalancerThe following code is a basic application load balancer private integration of HTTP API:fromaws_cdk.aws_apigatewayv2_integrationsimportHttpAlbIntegrationvpc=ec2.Vpc(self,"VPC")lb=elbv2.ApplicationLoadBalancer(self,"lb",vpc=vpc)listener=lb.add_listener("listener",port=80)listener.add_targets("target",port=80)http_endpoint=apigwv2.HttpApi(self,"HttpProxyPrivateApi",default_integration=HttpAlbIntegration("DefaultIntegration",listener))When an imported load balancer is used, thevpcoption must be specified forHttpAlbIntegration.Network Load BalancerThe following code is a basic network load balancer private integration of HTTP API:fromaws_cdk.aws_apigatewayv2_integrationsimportHttpNlbIntegrationvpc=ec2.Vpc(self,"VPC")lb=elbv2.NetworkLoadBalancer(self,"lb",vpc=vpc)listener=lb.add_listener("listener",port=80)listener.add_targets("target",port=80)http_endpoint=apigwv2.HttpApi(self,"HttpProxyPrivateApi",default_integration=HttpNlbIntegration("DefaultIntegration",listener))When an imported load balancer is used, thevpcoption must be specified forHttpNlbIntegration.Cloud Map Service DiscoveryThe following code is a basic discovery service private integration of HTTP API:importaws_cdk.aws_servicediscoveryasservicediscoveryfromaws_cdk.aws_apigatewayv2_integrationsimportHttpServiceDiscoveryIntegrationvpc=ec2.Vpc(self,"VPC")vpc_link=apigwv2.VpcLink(self,"VpcLink",vpc=vpc)namespace=servicediscovery.PrivateDnsNamespace(self,"Namespace",name="boobar.com",vpc=vpc)service=namespace.create_service("Service")http_endpoint=apigwv2.HttpApi(self,"HttpProxyPrivateApi",default_integration=HttpServiceDiscoveryIntegration("DefaultIntegration",service,vpc_link=vpc_link))Request ParametersRequest 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. SeeTransforming API requests and
responses.The following example creates a new header -header2- as a copy ofheader1and removesheader1.fromaws_cdk.aws_apigatewayv2_integrationsimportHttpAlbIntegration# lb: elbv2.ApplicationLoadBalancerlistener=lb.add_listener("listener",port=80)listener.add_targets("target",port=80)http_endpoint=apigwv2.HttpApi(self,"HttpProxyPrivateApi",default_integration=HttpAlbIntegration("DefaultIntegration",listener,parameter_mapping=apigwv2.ParameterMapping().append_header("header2",apigwv2.MappingValue.request_header("header1")).remove_header("header1")))To add mapping keys and values not yet supported by the CDK, use thecustom()method:fromaws_cdk.aws_apigatewayv2_integrationsimportHttpAlbIntegration# lb: elbv2.ApplicationLoadBalancerlistener=lb.add_listener("listener",port=80)listener.add_targets("target",port=80)http_endpoint=apigwv2.HttpApi(self,"HttpProxyPrivateApi",default_integration=HttpAlbIntegration("DefaultIntegration",listener,parameter_mapping=apigwv2.ParameterMapping().custom("myKey","myValue")))WebSocket APIsWebSocket integrations connect a route to backend resources. The following integrations are supported in the CDK.Lambda WebSocket IntegrationLambda 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 functionThe API Gateway service will invoke the lambda function with an event payload of a specific format.The following code configures asendmessageroute with a Lambda integrationfromaws_cdk.aws_apigatewayv2_integrationsimportWebSocketLambdaIntegration# message_handler: lambda.Functionweb_socket_api=apigwv2.WebSocketApi(self,"mywsapi")apigwv2.WebSocketStage(self,"mystage",web_socket_api=web_socket_api,stage_name="dev",auto_deploy=True)web_socket_api.add_route("sendmessage",integration=WebSocketLambdaIntegration("SendMessageIntegration",message_handler))