AWS CDK and Typescript: Using the New ApiGatewayV2

Enrico Gruner
The Startup
Published in
2 min readNov 23, 2020

--

Migrating from API Gateway to API Gateway V2 using Typescript and the AWS CDK.

Photo by Jason Leung on Unsplash

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,
});

Define an HTTP API

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Enrico Gruner
Enrico Gruner

Written by Enrico Gruner

I write about new programming topics and explain concepts in simple words. I write about JavaScript, React, CSS, and Unity. Join me on my journey!