In this section, we explore the secure architectural pattern connecting Amazon API Gateway into a private corporate network via VPC Link, and write the TypeScript source code for ApiGatewayStack using AWS CDK.
In modern, production-grade microservices architectures on AWS:
HttpNlbIntegration) with Network Load Balancers in just a few lines of CDK code.api-gate-way-stack.ts)Navigate to IaC-ECS-CDK/cdk/lib and open api-gate-way-stack.ts (or create it). We will construct the stack step-by-step as outlined below:
Import the required AWS CDK constructs and define the ApiGatewayStackProps interface to receive dependencies (vpc from VpcStack and nlbListener from NlbStack):
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as apigwv2 from 'aws-cdk-lib/aws-apigatewayv2';
import * as integrations from 'aws-cdk-lib/aws-apigatewayv2-integrations';
interface ApiGatewayStackProps extends cdk.StackProps {
vpc: ec2.IVpc;
nlbListener: elbv2.INetworkListener; // Receives port 80 listener from NlbStack
}
export class ApiGatewayStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: ApiGatewayStackProps) {
super(scope, id, props);
Provision a dedicated Security Group permitting outbound traffic to the NLB, then establish a secure private tunnel (VPC Link) from API Gateway into the VPC’s private subnets without assigning public IPs to backend services:
// 1. Create a dedicated Security Group for VPC Link (permitting outbound traffic to NLB)
const vpcLinkSecurityGroup = new ec2.SecurityGroup(this, 'VpcLinkSecurityGroup', {
vpc: props.vpc,
description: 'Security group for API Gateway VPC Link to Internal NLB',
allowAllOutbound: true,
});
// 2. Create VPC Link connecting API Gateway into private VPC subnets with the explicit Security Group
const vpcLink = new apigwv2.VpcLink(this, 'HttpVpcLink', {
vpc: props.vpc,
securityGroups: [vpcLinkSecurityGroup],
subnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
});
Important Note on VPC Link Security Group:
If the securityGroups property is omitted, AWS automatically assigns the VPC’s Default Security Group to the VPC Link Elastic Network Interfaces (ENIs). AWS CDK enforces security hardening by disabling all outbound rules on the default security group (restrictDefaultSecurityGroup), causing all VPC Link $\rightarrow$ NLB requests to be dropped and triggering a 503 Service Unavailable (due to 30s timeout). Defining an explicit Security Group completely prevents this failure mode.
Provision the public HTTP API entry point and configure CORS Preflight for standard HTTP methods (GET, POST, PUT, DELETE):
// 3. Initialize HTTP API Gateway (v2) with CORS preflight support
const httpApi = new apigwv2.HttpApi(this, 'ProductsHttpApi', {
apiName: 'fcaj-products-api-enterprise',
corsPreflight: {
allowHeaders: ['*'],
allowMethods: [
apigwv2.CorsHttpMethod.GET,
apigwv2.CorsHttpMethod.POST,
apigwv2.CorsHttpMethod.PUT,
apigwv2.CorsHttpMethod.DELETE,
],
allowOrigins: ['*'],
},
});
Use the native HttpNlbIntegration construct to connect the HTTP API with the NLB port 80 listener through the provisioned VPC Link:
// 4. Connect HTTP API to the NLB Listener through the VPC Link
const nlbIntegration = new integrations.HttpNlbIntegration(
'NlbIntegration',
props.nlbListener,
{
vpcLink: vpcLink,
method: apigwv2.HttpMethod.ANY,
}
);
Configure a catch-all proxy route /{proxy+} to route all sub-paths and a root route / for base URL requests to the NLB, and emit the public invoke URL to CloudFormation outputs:
// 5. Catch-all proxy route forwarding all sub-paths and methods to NLB
httpApi.addRoutes({
path: '/{proxy+}',
methods: [apigwv2.HttpMethod.ANY],
integration: nlbIntegration,
});
// 6. Catch-all route for base URL "/" to prevent 503 errors when calling base Invoke URL
httpApi.addRoutes({
path: '/',
methods: [apigwv2.HttpMethod.ANY],
integration: nlbIntegration,
});
// 7. Output the public API Gateway invoke endpoint
new cdk.CfnOutput(this, 'ApiEndpoint', {
value: httpApi.apiEndpoint,
description: 'API Gateway Endpoint',
});
}
}
api-gate-way-stack.tsBelow is the complete implementation of api-gate-way-stack.ts for cross-referencing and deployment:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as apigwv2 from 'aws-cdk-lib/aws-apigatewayv2';
import * as integrations from 'aws-cdk-lib/aws-apigatewayv2-integrations';
interface ApiGatewayStackProps extends cdk.StackProps {
vpc: ec2.IVpc;
nlbListener: elbv2.INetworkListener; // Receives port 80 listener from NlbStack
}
export class ApiGatewayStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: ApiGatewayStackProps) {
super(scope, id, props);
// 1. Create a dedicated Security Group for VPC Link (permitting outbound traffic to NLB)
const vpcLinkSecurityGroup = new ec2.SecurityGroup(this, 'VpcLinkSecurityGroup', {
vpc: props.vpc,
description: 'Security group for API Gateway VPC Link to Internal NLB',
allowAllOutbound: true,
});
// 2. Create VPC Link connecting API Gateway into private VPC subnets with the explicit Security Group
const vpcLink = new apigwv2.VpcLink(this, 'HttpVpcLink', {
vpc: props.vpc,
securityGroups: [vpcLinkSecurityGroup],
subnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
});
// 3. Initialize HTTP API Gateway (v2) with CORS preflight support
const httpApi = new apigwv2.HttpApi(this, 'ProductsHttpApi', {
apiName: 'fcaj-products-api-enterprise',
corsPreflight: {
allowHeaders: ['*'],
allowMethods: [
apigwv2.CorsHttpMethod.GET,
apigwv2.CorsHttpMethod.POST,
apigwv2.CorsHttpMethod.PUT,
apigwv2.CorsHttpMethod.DELETE,
],
allowOrigins: ['*'],
},
});
// 4. Connect HTTP API to the NLB Listener through the VPC Link
const nlbIntegration = new integrations.HttpNlbIntegration(
'NlbIntegration',
props.nlbListener,
{
vpcLink: vpcLink,
method: apigwv2.HttpMethod.ANY,
}
);
// 5. Catch-all proxy route forwarding all sub-paths and methods to NLB
httpApi.addRoutes({
path: '/{proxy+}',
methods: [apigwv2.HttpMethod.ANY],
integration: nlbIntegration,
});
// 6. Catch-all route for base URL "/" to prevent 503 errors when calling base Invoke URL
httpApi.addRoutes({
path: '/',
methods: [apigwv2.HttpMethod.ANY],
integration: nlbIntegration,
});
// 7. Output the public API Gateway invoke endpoint
new cdk.CfnOutput(this, 'ApiEndpoint', {
value: httpApi.apiEndpoint,
description: 'API Gateway Endpoint',
});
}
}

new ec2.SecurityGroup(this, 'VpcLinkSecurityGroup', ...):
allowAllOutbound: true.new apigwv2.VpcLink(..., { securityGroups: [vpcLinkSecurityGroup] }):
ec2.SubnetType.PRIVATE_WITH_EGRESS) bound to our explicit Security Group.new apigwv2.HttpApi(...):
fcaj-products-api-enterprise.corsPreflight: Permits standard headers (allowHeaders: ['*']) and REST methods (GET, POST, PUT, DELETE) so single-page applications (React, Vue, Angular) can communicate with your backend without CORS restrictions.new integrations.HttpNlbIntegration(...):
nlbListener (forwarded from NlbStack) with the vpcLink. Incoming API Gateway requests are packaged and proxied directly to the NLB port 80 over the private link.httpApi.addRoutes({ path: '/{proxy+}' }) & httpApi.addRoutes({ path: '/' }):
/{proxy+}: Catches all sub-resource requests (/actuator/health, /api/products, /api/products/{id}, etc.) and forwards them to Spring Boot./: Catches root base URL requests, eliminating unexpected 503 errors when users test the base invoke URL directly.cdk.CfnOutput:
ApiEndpoint URL (https://<api-id>.execute-api.ap-southeast-1.amazonaws.com) to CloudFormation outputs and the terminal for immediate testing with Postman or curl.Architectural Flexibility: Thanks to the Catch-all proxy (/{proxy+}), adding new Spring Boot controllers (e.g. /api/orders, /api/users) requires zero modifications to API Gateway or CDK code. All routing logic is seamlessly handled downstream by Spring Boot and the Application Load Balancer!