Skip to content

Commit 5d6c2b3

Browse files
authored
feat: add PrivateLambdaApiGateway construct for VPC-integrated API Gateway (#168)
Add PrivateLambdaApiGateway, which allows the creation of a private REST API Gateway integrated with a Lambda function within a specified VPC. This includes options for VPC endpoint creation and customizable deployment settings.
1 parent 3ea1700 commit 5d6c2b3

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import {
2+
aws_apigateway as apigateway,
3+
aws_ec2 as ec2,
4+
aws_iam as iam,
5+
aws_lambda as lambda,
6+
} from "aws-cdk-lib";
7+
import { Construct } from "constructs";
8+
9+
export interface PrivateLambdaApiGatewayProps {
10+
/**
11+
* Lambda function to integrate with the API Gateway.
12+
*/
13+
lambdaFunction: lambda.IFunction;
14+
15+
/**
16+
* Lambda integration options for the API Gateway.
17+
*/
18+
lambdaIntegrationOptions?: apigateway.LambdaIntegrationOptions;
19+
20+
/**
21+
* VPC to create the API Gateway in.
22+
*/
23+
vpc: ec2.IVpc;
24+
25+
/**
26+
* Whether to create a VPC endpoint for the API Gateway.
27+
*
28+
* @default - true
29+
*/
30+
createVpcEndpoint?: boolean;
31+
32+
/**
33+
* The subnets in which to create a VPC endpoint network interface. At most one per availability zone.
34+
35+
*/
36+
vpcEndpointSubnetSelection?: ec2.SubnetSelection;
37+
38+
/**
39+
* Name for the API Gateway.
40+
*
41+
* @default - `${scope.node.id}-private-api`
42+
*/
43+
restApiName?: string;
44+
45+
/**
46+
* Description for the API Gateway.
47+
*
48+
* @default - "Private REST API Gateway"
49+
*/
50+
description?: string;
51+
52+
/**
53+
* Deploy options for the API Gateway.
54+
*/
55+
deployOptions?: apigateway.StageOptions;
56+
57+
/**
58+
* Policy for the API Gateway.
59+
*
60+
* @default - Policy that allows any principal with the same VPC to invoke the API.
61+
*/
62+
policy?: iam.PolicyDocument;
63+
}
64+
65+
export class PrivateLambdaApiGateway extends Construct {
66+
public readonly api: apigateway.RestApi;
67+
public readonly vpcEndpoint?: ec2.InterfaceVpcEndpoint;
68+
69+
constructor(
70+
scope: Construct,
71+
id: string,
72+
props: PrivateLambdaApiGatewayProps
73+
) {
74+
super(scope, id);
75+
76+
const {
77+
restApiName,
78+
description,
79+
lambdaFunction,
80+
vpc,
81+
vpcEndpointSubnetSelection,
82+
createVpcEndpoint = true,
83+
deployOptions,
84+
policy,
85+
lambdaIntegrationOptions,
86+
} = props;
87+
88+
if (createVpcEndpoint) {
89+
// Create VPC Endpoint for API Gateway
90+
this.vpcEndpoint = new ec2.InterfaceVpcEndpoint(this, "vpc-endpoint", {
91+
vpc,
92+
service: ec2.InterfaceVpcEndpointAwsService.APIGATEWAY,
93+
subnets: vpcEndpointSubnetSelection,
94+
});
95+
}
96+
97+
const defaultIntegration = new apigateway.LambdaIntegration(
98+
lambdaFunction,
99+
lambdaIntegrationOptions
100+
);
101+
102+
// Create Private REST API Gateway
103+
this.api = new apigateway.RestApi(this, "rest-api", {
104+
restApiName: restApiName ?? `${scope.node.id}-private-api`,
105+
description: description ?? "Private REST API Gateway",
106+
endpointTypes: [apigateway.EndpointType.PRIVATE],
107+
policy:
108+
policy ??
109+
new iam.PolicyDocument({
110+
statements: [
111+
new iam.PolicyStatement({
112+
effect: iam.Effect.ALLOW,
113+
principals: [new iam.AnyPrincipal()],
114+
actions: ["execute-api:Invoke"],
115+
resources: ["execute-api:/*"],
116+
conditions: {
117+
StringEquals: { "aws:SourceVpc": vpc.vpcId },
118+
},
119+
}),
120+
],
121+
}),
122+
deployOptions: deployOptions ?? {
123+
loggingLevel: apigateway.MethodLoggingLevel.INFO,
124+
dataTraceEnabled: true,
125+
},
126+
defaultIntegration,
127+
defaultMethodOptions: {
128+
authorizationType: apigateway.AuthorizationType.NONE,
129+
},
130+
});
131+
132+
this.api.root.addMethod("ANY");
133+
this.api.root.addResource("{proxy+}").addMethod("ANY");
134+
}
135+
}

0 commit comments

Comments
 (0)