@certenza/aws-cdk-infrastructure-commons 2.3.1 → 2.4.0
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.
- package/dist/src/apigateway.d.ts +24 -7
- package/dist/src/apigateway.js +94 -28
- package/dist/src/lambda.d.ts +1 -1
- package/dist/src/lambda.js +1 -1
- package/package.json +1 -1
package/dist/src/apigateway.d.ts
CHANGED
|
@@ -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,34 @@ import { Construct } from "constructs";
|
|
|
8
11
|
*/
|
|
9
12
|
declare const getApiGatewayDomainName: (service: string, domainName: string, environment: string) => string;
|
|
10
13
|
/**
|
|
11
|
-
* Creates
|
|
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
|
-
*
|
|
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
|
|
24
|
-
* @
|
|
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
|
+
declare const createHttpApiGatewayLambdaEndpoint: (method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD", path: string, lambdaFunction: lambda.IFunction, api: apigatewayv2.HttpApi) => apigatewayv2.IHttpRoute[];
|
|
35
|
+
/**
|
|
36
|
+
* Creates an HTTP API Gateway SQS endpoint
|
|
37
|
+
* @param method - The HTTP method for the endpoint (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
|
|
38
|
+
* @param path - The path for the endpoint (e.g., "/messages")
|
|
39
|
+
* @param queue - The SQS queue to integrate with
|
|
40
|
+
* @param api - The HTTP API Gateway to add the endpoint to
|
|
41
|
+
* @returns The route configuration
|
|
25
42
|
*/
|
|
26
|
-
declare const
|
|
27
|
-
export { createApiGateway, getApiGatewayDomainName,
|
|
43
|
+
declare const createHttpApiGatewaySQSEndpoint: (method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD", path: string, queue: sqs.IQueue, api: apigatewayv2.HttpApi) => apigatewayv2.IHttpRoute[];
|
|
44
|
+
export { createApiGateway, createHttpApiGateway, createHttpApiGatewayLambdaEndpoint, createHttpApiGatewaySQSEndpoint, getApiGatewayDomainName, };
|
package/dist/src/apigateway.js
CHANGED
|
@@ -33,15 +33,18 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.
|
|
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
|
|
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,99 @@ 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
|
-
*
|
|
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
|
|
120
|
-
* @
|
|
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
|
+
const createHttpApiGatewayLambdaEndpoint = (method, path, lambdaFunction, api) => {
|
|
166
|
+
return api.addRoutes({
|
|
167
|
+
path: path,
|
|
168
|
+
methods: [getHttpMethod(method)],
|
|
169
|
+
integration: new apigatewayv2_integrations.HttpLambdaIntegration(lambdaFunction.functionName, lambdaFunction),
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
exports.createHttpApiGatewayLambdaEndpoint = createHttpApiGatewayLambdaEndpoint;
|
|
173
|
+
/**
|
|
174
|
+
* Creates an HTTP API Gateway SQS endpoint
|
|
175
|
+
* @param method - The HTTP method for the endpoint (POST, GET, PUT, DELETE, PATCH, OPTIONS, HEAD)
|
|
176
|
+
* @param path - The path for the endpoint (e.g., "/messages")
|
|
177
|
+
* @param queue - The SQS queue to integrate with
|
|
178
|
+
* @param api - The HTTP API Gateway to add the endpoint to
|
|
179
|
+
* @returns The route configuration
|
|
121
180
|
*/
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
181
|
+
const createHttpApiGatewaySQSEndpoint = (method, path, queue, api) => {
|
|
182
|
+
// Grant API Gateway service permission to send messages to SQS
|
|
183
|
+
// This matches the behavior of createSQSApiGatewayIntegration
|
|
184
|
+
// For HTTP API Gateway v2, we grant permissions directly to the service principal
|
|
185
|
+
queue.grantSendMessages(new iam.ServicePrincipal("apigateway.amazonaws.com"));
|
|
186
|
+
return api.addRoutes({
|
|
187
|
+
path: path,
|
|
188
|
+
methods: [getHttpMethod(method)],
|
|
189
|
+
integration: new apigatewayv2_integrations.HttpSqsIntegration(queue.queueName, {
|
|
190
|
+
queue: queue,
|
|
191
|
+
}),
|
|
126
192
|
});
|
|
127
193
|
};
|
|
128
|
-
exports.
|
|
194
|
+
exports.createHttpApiGatewaySQSEndpoint = createHttpApiGatewaySQSEndpoint;
|
package/dist/src/lambda.d.ts
CHANGED
|
@@ -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
|
|
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
|
*/
|
package/dist/src/lambda.js
CHANGED
|
@@ -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
|
|
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