@certenza/aws-cdk-infrastructure-commons 2.3.1 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,7 @@
1
1
  import * as apigateway from "aws-cdk-lib/aws-apigateway";
2
+ import * as apigatewayv2 from "aws-cdk-lib/aws-apigatewayv2";
3
+ import * as lambda from "aws-cdk-lib/aws-lambda";
4
+ import * as sqs from "aws-cdk-lib/aws-sqs";
2
5
  import { Construct } from "constructs";
3
6
  /**
4
7
  * Gets the domain name for the API Gateway
@@ -8,20 +11,42 @@ import { Construct } from "constructs";
8
11
  */
9
12
  declare const getApiGatewayDomainName: (service: string, domainName: string, environment: string) => string;
10
13
  /**
11
- * Creates an API Gateway with the given parameters
14
+ * Creates a REST API Gateway with the given parameters
12
15
  * @param scope - The scope of the API Gateway
13
16
  * @param apiName - The name of the API Gateway
14
17
  * @param domainName - The domain name of the API Gateway
15
18
  * @param hostedZoneId - The ID of the hosted zone
16
19
  * @param zoneName - The name of the hosted zone
17
- * @returns The API Gateway
20
+ * @returns The REST API Gateway
18
21
  */
19
22
  declare const createApiGateway: (scope: Construct, apiName: string, domainName: string, hostedZoneId: string, zoneName: string) => apigateway.RestApi;
20
23
  /**
21
- * Imports an API Gateway from the given stack name
24
+ * Creates an HTTP API Gateway with the given parameters
22
25
  * @param scope - The scope of the API Gateway
23
- * @param apiName - The name of the API Gateway to import
24
- * @returns The imported API Gateway
26
+ * @param apiName - The name of the API Gateway
27
+ * @param domainName - The domain name of the API Gateway
28
+ * @param hostedZoneId - The ID of the hosted zone
29
+ * @param zoneName - The name of the hosted zone
30
+ * @param defaultAuthorizer - The default authorizer for the API Gateway (default is an IAM authorizer)
31
+ * @returns The HTTP API Gateway
32
+ */
33
+ declare const createHttpApiGateway: (scope: Construct, apiName: string, domainName: string, hostedZoneId: string, zoneName: string, defaultAuthorizer?: apigatewayv2.IHttpRouteAuthorizer) => apigatewayv2.HttpApi;
34
+ /**
35
+ * Creates an HTTP API Gateway Lambda endpoint
36
+ * @param method - The HTTP method for the endpoint (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
37
+ * @param path - The path for the endpoint (e.g., "/messages")
38
+ * @param lambdaFunction - The Lambda function to integrate
39
+ * @param api - The HTTP API Gateway to add the endpoint to
40
+ * @returns The HTTP API Gateway Lambda endpoint
41
+ */
42
+ declare const createHttpApiGatewayLambdaEndpoint: (method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD", path: string, lambdaFunction: lambda.IFunction, api: apigatewayv2.HttpApi) => apigatewayv2.IHttpRoute[];
43
+ /**
44
+ * Creates an HTTP API Gateway SQS endpoint
45
+ * @param method - The HTTP method for the endpoint (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
46
+ * @param path - The path for the endpoint (e.g., "/messages")
47
+ * @param queue - The SQS queue to integrate with
48
+ * @param api - The HTTP API Gateway to add the endpoint to
49
+ * @returns The HTTP API Gateway SQS endpoint
25
50
  */
26
- declare const importApiGateway: (scope: Construct, apiName: string) => apigateway.IRestApi;
27
- export { createApiGateway, getApiGatewayDomainName, importApiGateway };
51
+ declare const createHttpApiGatewaySQSEndpoint: (method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD", path: string, queue: sqs.IQueue, api: apigatewayv2.HttpApi) => apigatewayv2.IHttpRoute[];
52
+ export { createApiGateway, createHttpApiGateway, createHttpApiGatewayLambdaEndpoint, createHttpApiGatewaySQSEndpoint, getApiGatewayDomainName, };
@@ -33,15 +33,18 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.importApiGateway = exports.getApiGatewayDomainName = exports.createApiGateway = void 0;
36
+ exports.getApiGatewayDomainName = exports.createHttpApiGatewaySQSEndpoint = exports.createHttpApiGatewayLambdaEndpoint = exports.createHttpApiGateway = exports.createApiGateway = void 0;
37
37
  const apigateway = __importStar(require("aws-cdk-lib/aws-apigateway"));
38
+ const apigatewayv2 = __importStar(require("aws-cdk-lib/aws-apigatewayv2"));
39
+ const apigatewayv2_integrations = __importStar(require("aws-cdk-lib/aws-apigatewayv2-integrations"));
38
40
  const logs = __importStar(require("aws-cdk-lib/aws-logs"));
39
41
  const route53 = __importStar(require("aws-cdk-lib/aws-route53"));
40
42
  const route53targets = __importStar(require("aws-cdk-lib/aws-route53-targets"));
43
+ const aws_apigatewayv2_authorizers_1 = require("aws-cdk-lib/aws-apigatewayv2-authorizers");
44
+ const iam = __importStar(require("aws-cdk-lib/aws-iam"));
41
45
  const cdk = __importStar(require("aws-cdk-lib"));
42
46
  const acm_1 = require("./acm");
43
47
  const route53_1 = require("./route53");
44
- const aws_cdk_lib_1 = require("aws-cdk-lib");
45
48
  /**
46
49
  * Gets the domain name for the API Gateway
47
50
  * @param environment - The environment to get the domain name for
@@ -55,13 +58,13 @@ const getApiGatewayDomainName = (service, domainName, environment) => {
55
58
  };
56
59
  exports.getApiGatewayDomainName = getApiGatewayDomainName;
57
60
  /**
58
- * Creates an API Gateway with the given parameters
61
+ * Creates a REST API Gateway with the given parameters
59
62
  * @param scope - The scope of the API Gateway
60
63
  * @param apiName - The name of the API Gateway
61
64
  * @param domainName - The domain name of the API Gateway
62
65
  * @param hostedZoneId - The ID of the hosted zone
63
66
  * @param zoneName - The name of the hosted zone
64
- * @returns The API Gateway
67
+ * @returns The REST API Gateway
65
68
  */
66
69
  const createApiGateway = (scope, apiName, domainName, hostedZoneId, zoneName) => {
67
70
  // Import the existing hosted zone
@@ -93,36 +96,125 @@ const createApiGateway = (scope, apiName, domainName, hostedZoneId, zoneName) =>
93
96
  recordName: domainName.replace(`${hostedZone.zoneName}.`, ""),
94
97
  target: route53.RecordTarget.fromAlias(new route53targets.ApiGateway(api)),
95
98
  });
96
- // Add a dummy endpoint to prevent API Gateway deployment errors
97
- // when the API has only one endpoint. This ensures the API can be deployed
98
- // even if no other methods are added yet.
99
- api.root.addMethod("GET", new apigateway.MockIntegration({
100
- requestTemplates: {
101
- "application/json": '{"statusCode": 200}',
102
- },
103
- integrationResponses: [
104
- {
105
- statusCode: "200",
106
- responseTemplates: {
107
- "application/json": "",
108
- },
109
- },
110
- ],
111
- }));
112
99
  // Return the API Gateway
113
100
  return api;
114
101
  };
115
102
  exports.createApiGateway = createApiGateway;
116
103
  /**
117
- * Imports an API Gateway from the given stack name
104
+ * Creates an HTTP API Gateway with the given parameters
118
105
  * @param scope - The scope of the API Gateway
119
- * @param apiName - The name of the API Gateway to import
120
- * @returns The imported API Gateway
106
+ * @param apiName - The name of the API Gateway
107
+ * @param domainName - The domain name of the API Gateway
108
+ * @param hostedZoneId - The ID of the hosted zone
109
+ * @param zoneName - The name of the hosted zone
110
+ * @param defaultAuthorizer - The default authorizer for the API Gateway (default is an IAM authorizer)
111
+ * @returns The HTTP API Gateway
112
+ */
113
+ const createHttpApiGateway = (scope, apiName, domainName, hostedZoneId, zoneName, defaultAuthorizer) => {
114
+ // Import the existing hosted zone
115
+ const hostedZone = (0, route53_1.getHostedZone)(scope, "HostedZone", hostedZoneId, zoneName);
116
+ // Create a certificate for the API Gateway domain
117
+ const certificate = (0, acm_1.createCertificate)(scope, `HttpApiGatewayCertificate`, domainName, hostedZone);
118
+ // Create the domain name for the HTTP API
119
+ const apiDomainName = new apigatewayv2.DomainName(scope, `${apiName}-Domain`, {
120
+ domainName: domainName,
121
+ certificate: certificate,
122
+ securityPolicy: apigatewayv2.SecurityPolicy.TLS_1_2,
123
+ });
124
+ // Create the HTTP API Gateway
125
+ const api = new apigatewayv2.HttpApi(scope, apiName, {
126
+ description: `Public HTTP API Gateway for ${apiName}`,
127
+ defaultDomainMapping: {
128
+ domainName: apiDomainName,
129
+ },
130
+ defaultAuthorizer: defaultAuthorizer ?? new aws_apigatewayv2_authorizers_1.HttpIamAuthorizer(),
131
+ });
132
+ // Create Route53 A record pointing to the API Gateway domain
133
+ new route53.ARecord(scope, `${apiName}-AliasRecord`, {
134
+ zone: hostedZone,
135
+ recordName: domainName.replace(`${hostedZone.zoneName}.`, ""),
136
+ target: route53.RecordTarget.fromAlias(new route53targets.ApiGatewayv2DomainProperties(apiDomainName.regionalDomainName, apiDomainName.regionalHostedZoneId)),
137
+ });
138
+ // Return the HTTP API Gateway
139
+ return api;
140
+ };
141
+ exports.createHttpApiGateway = createHttpApiGateway;
142
+ /**
143
+ * Gets the HTTP method for the given method
144
+ * @param method - The method to get the HTTP method for (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
145
+ * @returns The HTTP method
146
+ */
147
+ const getHttpMethod = (method) => {
148
+ switch (method) {
149
+ case "POST":
150
+ return apigatewayv2.HttpMethod.POST;
151
+ case "GET":
152
+ return apigatewayv2.HttpMethod.GET;
153
+ case "PUT":
154
+ return apigatewayv2.HttpMethod.PUT;
155
+ case "DELETE":
156
+ return apigatewayv2.HttpMethod.DELETE;
157
+ case "PATCH":
158
+ return apigatewayv2.HttpMethod.PATCH;
159
+ case "OPTIONS":
160
+ return apigatewayv2.HttpMethod.OPTIONS;
161
+ default:
162
+ return apigatewayv2.HttpMethod.HEAD;
163
+ }
164
+ };
165
+ /**
166
+ * Creates an HTTP API Gateway Lambda endpoint
167
+ * @param method - The HTTP method for the endpoint (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
168
+ * @param path - The path for the endpoint (e.g., "/messages")
169
+ * @param lambdaFunction - The Lambda function to integrate
170
+ * @param api - The HTTP API Gateway to add the endpoint to
171
+ * @returns The HTTP API Gateway Lambda endpoint
172
+ */
173
+ const createHttpApiGatewayLambdaEndpoint = (method, path, lambdaFunction, api) => {
174
+ // Create a unique ID by combining path and method to avoid construct ID conflicts
175
+ // when the same Lambda function is used for multiple routes
176
+ // Path and method are concrete strings (not tokens), so this is safe
177
+ // Handle path parameters like {userId} by removing curly braces
178
+ const sanitizedPath = path
179
+ .replace(/\{([^}]+)\}/g, "$1")
180
+ .replace(/\//g, "-")
181
+ .replace(/^-/, "")
182
+ .replace(/-$/, "") || "root";
183
+ return api.addRoutes({
184
+ path: path,
185
+ methods: [getHttpMethod(method)],
186
+ integration: new apigatewayv2_integrations.HttpLambdaIntegration(`${method}-${sanitizedPath}`, lambdaFunction),
187
+ });
188
+ };
189
+ exports.createHttpApiGatewayLambdaEndpoint = createHttpApiGatewayLambdaEndpoint;
190
+ /**
191
+ * Creates an HTTP API Gateway SQS endpoint
192
+ * @param method - The HTTP method for the endpoint (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
193
+ * @param path - The path for the endpoint (e.g., "/messages")
194
+ * @param queue - The SQS queue to integrate with
195
+ * @param api - The HTTP API Gateway to add the endpoint to
196
+ * @returns The HTTP API Gateway SQS endpoint
121
197
  */
122
- const importApiGateway = (scope, apiName) => {
123
- return apigateway.RestApi.fromRestApiAttributes(scope, `Imported-${apiName}-Api`, {
124
- restApiId: aws_cdk_lib_1.Fn.importValue(`CertenzaInfrastructureApiGatewayStack-${apiName}Id`),
125
- rootResourceId: aws_cdk_lib_1.Fn.importValue(`CertenzaInfrastructureApiGatewayStack-${apiName}RootResourceId`),
198
+ const createHttpApiGatewaySQSEndpoint = (method, path, queue, api) => {
199
+ // Grant API Gateway service permission to send messages to SQS
200
+ // This matches the behavior of createSQSApiGatewayIntegration
201
+ // For HTTP API Gateway v2, we grant permissions directly to the service principal
202
+ queue.grantSendMessages(new iam.ServicePrincipal("apigateway.amazonaws.com"));
203
+ // Create a unique ID by combining path and method to avoid construct ID conflicts
204
+ // when the same queue is used for multiple routes
205
+ // Path and method are concrete strings (not tokens), so this is safe
206
+ // Handle path parameters like {userId} by removing curly braces
207
+ const sanitizedPath = path
208
+ .replace(/\{([^}]+)\}/g, "$1")
209
+ .replace(/\//g, "-")
210
+ .replace(/^-/, "")
211
+ .replace(/-$/, "") || "root";
212
+ return api.addRoutes({
213
+ path: path,
214
+ methods: [getHttpMethod(method)],
215
+ integration: new apigatewayv2_integrations.HttpSqsIntegration(`${method}-${sanitizedPath}`, {
216
+ queue: queue,
217
+ }),
126
218
  });
127
219
  };
128
- exports.importApiGateway = importApiGateway;
220
+ exports.createHttpApiGatewaySQSEndpoint = createHttpApiGatewaySQSEndpoint;
@@ -26,7 +26,7 @@ type CreateLambdaFunctionProps = {
26
26
  */
27
27
  declare const createLambdaFunction: (scope: Construct, functionName: string, environment: string, props?: CreateLambdaFunctionProps) => nodejs.NodejsFunction;
28
28
  /**
29
- * Creates a lambda integration for a lambda function
29
+ * Creates a lambda integration for a REST API Gateway
30
30
  * @param lambdaFunction - The lambda function to create an integration for
31
31
  * @returns The lambda integration
32
32
  */
@@ -102,7 +102,7 @@ const createLambdaFunction = (scope, functionName, environment, props = {}) => {
102
102
  };
103
103
  exports.createLambdaFunction = createLambdaFunction;
104
104
  /**
105
- * Creates a lambda integration for a lambda function
105
+ * Creates a lambda integration for a REST API Gateway
106
106
  * @param lambdaFunction - The lambda function to create an integration for
107
107
  * @returns The lambda integration
108
108
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certenza/aws-cdk-infrastructure-commons",
3
- "version": "2.3.1",
3
+ "version": "2.4.1",
4
4
  "description": "Common infrastructure reusable utilities and resources for Certenza projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",