AWS CDK and Typescript: Using the New ApiGatewayV2
Migrating from API Gateway to API Gateway V2 using Typescript and the AWS CDK.
Some months ago, it was not that easy to use ApiGatewayV2 in your CDK projects since it was in beta and not really ready to use. Parts of the V2 are still experimental — but you can use the most important bits now. See my post on Stackoverflow for a quick way of converting ApiGateway to ApiGatewayV2. I have also made a video on YouTube for migrating from V1 to V2 (links to SO and YouTube below).
Using the new API Gateway V2 is a 3-steps-process:
- Create an integration.
- Define an HTTP API.
- Add Routes.
Note that there’s still no easy way to use WebSockets.
Create an integration
The integration is used to tell API Gateway where it should send incoming requests to. In the most simplistic case, this would be your Lambda handler. But Load Balancers or an HTTP Proxy are also possible. What might be strange is that this comes in its own package @aws-cdk/aws-apigatewayv2-integrations
.
const httpApiIntegration = new LambdaProxyIntegration({
handler: fn,
});