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.
from aws_cdk.aws_apigatewayv2_integrations import HttpLambdaIntegration
# books_default_fn: lambda.Function
books_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 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
.
from aws_cdk.aws_apigatewayv2_integrations import HttpUrlIntegration
books_integration = HttpUrlIntegration("BooksIntegration", "https://get-books-proxy.example.com")
http_api = apigwv2.HttpApi(self, "HttpApi")
http_api.add_routes(
path="/books",
methods=[apigwv2.HttpMethod.GET],
integration=books_integration
)
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:
from aws_cdk.aws_apigatewayv2_integrations import HttpStepFunctionsIntegration
import aws_cdk.aws_stepfunctions as sfn
# state_machine: sfn.StateMachine
# http_api: apigwv2.HttpApi
http_api.add_routes(
path="/start",
methods=[apigwv2.HttpMethod.POST],
integration=HttpStepFunctionsIntegration("StartExecutionIntegration",
state_machine=state_machine,
subtype=apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_START_EXECUTION
)
)
http_api.add_routes(
path="/start-sync",
methods=[apigwv2.HttpMethod.POST],
integration=HttpStepFunctionsIntegration("StartSyncExecutionIntegration",
state_machine=state_machine,
subtype=apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_START_SYNC_EXECUTION
)
)
http_api.add_routes(
path="/stop",
methods=[apigwv2.HttpMethod.POST],
integration=HttpStepFunctionsIntegration("StopExecutionIntegration",
state_machine=state_machine,
subtype=apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_STOP_EXECUTION,
# For the `STOP_EXECUTION` subtype, it is necessary to specify the `executionArn`.
parameter_mapping=apigwv2.ParameterMapping().custom("ExecutionArn", "$request.querystring.executionArn")
)
)
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:
from aws_cdk.aws_apigatewayv2_integrations import HttpAlbIntegration
vpc = 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, 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:
from aws_cdk.aws_apigatewayv2_integrations import HttpNlbIntegration
vpc = 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, 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 aws_cdk.aws_servicediscovery as servicediscovery
from aws_cdk.aws_apigatewayv2_integrations import HttpServiceDiscoveryIntegration
vpc = 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 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
.
from aws_cdk.aws_apigatewayv2_integrations import HttpAlbIntegration
# lb: elbv2.ApplicationLoadBalancer
listener = 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 the custom()
method:
from aws_cdk.aws_apigatewayv2_integrations import HttpAlbIntegration
# lb: elbv2.ApplicationLoadBalancer
listener = 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 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
from aws_cdk.aws_apigatewayv2_integrations import WebSocketLambdaIntegration
# message_handler: lambda.Function
web_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)
)
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.
from aws_cdk.aws_apigatewayv2_integrations import WebSocketAwsIntegration
import aws_cdk.aws_dynamodb as dynamodb
import aws_cdk.aws_iam as iam
# api_role: iam.Role
# table: dynamodb.Table
web_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("$connect",
integration=WebSocketAwsIntegration("DynamodbPutItem",
integration_uri=f"arn:aws:apigateway:{this.region}:dynamodb:action/PutItem",
integration_method=apigwv2.HttpMethod.POST,
credentials_role=api_role,
request_templates={
"application/json": JSON.stringify({
"TableName": table.table_name,
"Item": {
"id": {
"S": "$context.requestId"
}
}
})
}
)
)
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.