@sylvesterllc/aws-constructs 1.1.26 → 1.1.29

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.
Files changed (178) hide show
  1. package/.github/workflows/publish.yml +56 -56
  2. package/__tests__/app-config.test.ts +32 -32
  3. package/__tests__/data/testConfig.ts +146 -146
  4. package/__tests__/mocks/ResourceMock.ts +9 -9
  5. package/__tests__/router.test.ts +194 -194
  6. package/dist/config/AppConfig.js +1 -1
  7. package/dist/config/Constants.js +1 -1
  8. package/dist/config/Environments.js +1 -1
  9. package/dist/config/MicroserviceAppConfig.js +1 -1
  10. package/dist/config/ServiceBundleConfig.js +1 -1
  11. package/dist/config/bishopConfig.js +1 -1
  12. package/dist/config/customConfigs/ApiAppConfig.js +1 -1
  13. package/dist/config/customConfigs/GlobalAppConfig.js +1 -1
  14. package/dist/config/customConfigs/IAppConfig.js +1 -1
  15. package/dist/config/customConfigs/ResourceAppConfig.js +1 -1
  16. package/dist/config/microServiceConfig.js +2 -2
  17. package/dist/config/types/LogDuration.js +1 -1
  18. package/dist/config/types/TsgAuthorizerProp.js +1 -1
  19. package/dist/config/types/TsgAuthorizerType.js +1 -1
  20. package/dist/config/types/TsgCdkConfigType.js +1 -1
  21. package/dist/config/types/TsgCognitoConfig.js +1 -1
  22. package/dist/config/types/TsgDnsConfig.js +1 -1
  23. package/dist/config/types/TsgDynamoDbProp.js +1 -1
  24. package/dist/config/types/TsgDynamoIndex.js +1 -1
  25. package/dist/config/types/TsgDynamoProp.js +1 -1
  26. package/dist/config/types/TsgDynamoTable.js +1 -1
  27. package/dist/config/types/TsgDynamoTableRef.js +1 -1
  28. package/dist/config/types/TsgEc2Prop.js +1 -1
  29. package/dist/config/types/TsgKeyPair.js +1 -1
  30. package/dist/config/types/TsgLambda.js +1 -1
  31. package/dist/config/types/TsgLambdaLayerProp.js +1 -1
  32. package/dist/config/types/TsgLambdaMap.js +1 -1
  33. package/dist/config/types/TsgLambdaName.js +1 -1
  34. package/dist/config/types/TsgLambdaNode.js +1 -1
  35. package/dist/config/types/TsgLambdaProp.js +1 -1
  36. package/dist/config/types/TsgLambdaProps.js +1 -1
  37. package/dist/config/types/TsgLambdaRoutable.js +1 -1
  38. package/dist/config/types/index.js +1 -1
  39. package/dist/constants/aws-service-principal-constants.js +1 -1
  40. package/dist/constructs/BasicLambda.js +1 -1
  41. package/dist/constructs/DynamoDbSingleTable.js +1 -1
  42. package/dist/constructs/MicroService.js +1 -1
  43. package/dist/constructs/index.js +1 -1
  44. package/dist/constructs/timer-job.js +1 -1
  45. package/dist/helpers/logger.js +1 -1
  46. package/dist/helpers/util-helper.js +1 -1
  47. package/dist/index.js +1 -1
  48. package/dist/interfaces/ApiLambdaResult.js +1 -1
  49. package/dist/interfaces/BaseResourceProps.js +1 -1
  50. package/dist/interfaces/CreateLambdaFunctionInput.js +1 -1
  51. package/dist/interfaces/MicroserviceProps.js +1 -1
  52. package/dist/interfaces/lambda/index.js +1 -1
  53. package/dist/interfaces/lambda/lambda-props.js +1 -1
  54. package/dist/interfaces/timer-job/timer-job-props.js +1 -1
  55. package/dist/resources/base/baseResource.js +1 -1
  56. package/dist/resources/base/tsgBaseResource.js +1 -1
  57. package/dist/resources/certificate/createCertificate.js +1 -1
  58. package/dist/resources/cognito/createCognito.js +1 -1
  59. package/dist/resources/cognito/createCognitoProps.js +1 -1
  60. package/dist/resources/customResource/createSeedDatabaseCustomResource.js +1 -1
  61. package/dist/resources/dynamodb/CreateDynamo.js +1 -1
  62. package/dist/resources/dynamodb/CreateDynamoSingleTableDesign.js +1 -1
  63. package/dist/resources/ec2/create-ec2-instance.js +3 -3
  64. package/dist/resources/ecr/CreateEcr.js +2 -2
  65. package/dist/resources/gateway/CreateApiAndAttachLambdas.js +1 -1
  66. package/dist/resources/gateway/createApi.js +1 -1
  67. package/dist/resources/gateway/createApiKey.js +1 -1
  68. package/dist/resources/helpers/createCommonLayer.js +1 -1
  69. package/dist/resources/helpers/createRoutes.js +1 -1
  70. package/dist/resources/index.js +1 -1
  71. package/dist/resources/lambda/create-basic-lambda-helper.js +31 -113
  72. package/dist/resources/lambda/create-basic-lambda.js +28 -110
  73. package/dist/resources/lambda/createLambda.js +1 -1
  74. package/dist/resources/lambda/lambdaHelper.js +1 -1
  75. package/dist/resources/lambda-authorizer/TsgJwtTokenAuthorizer.js +1 -1
  76. package/dist/resources/lambda-authorizer/TsgRequestAuthorizer.js +1 -1
  77. package/dist/resources/lambda-authorizer/createAuthorizerHelpers.js +1 -1
  78. package/dist/resources/lambda-layer/createLambdaLayer.js +1 -1
  79. package/dist/resources/layers/common/src/nodejs/api-response/responses.js +1 -1
  80. package/dist/resources/layers/common/src/nodejs/index.js +1 -1
  81. package/dist/resources/layers/common/src/nodejs/logger/IContext.js +1 -1
  82. package/dist/resources/layers/common/src/nodejs/logger/index.js +1 -1
  83. package/dist/resources/layers/common/src/nodejs/logger/logger.js +1 -1
  84. package/dist/resources/resources/iam/policyDocuments/cognitoPolicy.js +1 -1
  85. package/dist/resources/resources/iam/roles/createRole.js +1 -1
  86. package/dist/resources/route53/CreateZone.js +1 -1
  87. package/dist/resources/route53/create-zones.js +1 -1
  88. package/dist/resources/securityManager/index.js +1 -1
  89. package/jest.config.js +8 -8
  90. package/jest.config.ts.old +20 -20
  91. package/package.json +55 -55
  92. package/readme.md +147 -147
  93. package/src/config/AppConfig.ts +106 -106
  94. package/src/config/Constants.ts +26 -26
  95. package/src/config/Environments.ts +1 -1
  96. package/src/config/MicroserviceAppConfig.ts +8 -8
  97. package/src/config/ServiceBundleConfig.ts +17 -17
  98. package/src/config/bishopConfig.ts +82 -82
  99. package/src/config/customConfigs/ApiAppConfig.ts +5 -5
  100. package/src/config/customConfigs/GlobalAppConfig.ts +10 -10
  101. package/src/config/customConfigs/IAppConfig.ts +12 -12
  102. package/src/config/customConfigs/ResourceAppConfig.ts +15 -15
  103. package/src/config/microServiceConfig.ts +83 -83
  104. package/src/config/types/LogDuration.ts +7 -7
  105. package/src/config/types/TsgAuthorizerProp.ts +8 -8
  106. package/src/config/types/TsgAuthorizerType.ts +4 -4
  107. package/src/config/types/TsgCdkConfigType.ts +2 -2
  108. package/src/config/types/TsgCognitoConfig.ts +5 -5
  109. package/src/config/types/TsgDnsConfig.ts +10 -10
  110. package/src/config/types/TsgDynamoDbProp.ts +9 -9
  111. package/src/config/types/TsgDynamoIndex.ts +15 -15
  112. package/src/config/types/TsgDynamoProp.ts +16 -16
  113. package/src/config/types/TsgDynamoTable.ts +4 -4
  114. package/src/config/types/TsgDynamoTableRef.ts +3 -3
  115. package/src/config/types/TsgEc2Prop.ts +8 -8
  116. package/src/config/types/TsgKeyPair.ts +5 -5
  117. package/src/config/types/TsgLambda.ts +5 -5
  118. package/src/config/types/TsgLambdaLayerProp.ts +4 -4
  119. package/src/config/types/TsgLambdaMap.ts +3 -3
  120. package/src/config/types/TsgLambdaName.ts +3 -3
  121. package/src/config/types/TsgLambdaNode.ts +3 -3
  122. package/src/config/types/TsgLambdaProp.ts +19 -19
  123. package/src/config/types/TsgLambdaProps.ts +17 -17
  124. package/src/config/types/TsgLambdaRoutable.ts +8 -8
  125. package/src/config/types/index.ts +11 -11
  126. package/src/constants/aws-service-principal-constants.ts +2 -2
  127. package/src/constructs/BasicLambda.ts +19 -19
  128. package/src/constructs/DynamoDbSingleTable.ts +12 -12
  129. package/src/constructs/MicroService.ts +131 -131
  130. package/src/constructs/index.ts +5 -5
  131. package/src/constructs/timer-job.ts +19 -19
  132. package/src/helpers/logger.ts +12 -12
  133. package/src/helpers/util-helper.ts +19 -19
  134. package/src/index.ts +8 -8
  135. package/src/interfaces/ApiLambdaResult.ts +6 -6
  136. package/src/interfaces/BaseResourceProps.ts +6 -6
  137. package/src/interfaces/CreateLambdaFunctionInput.ts +10 -10
  138. package/src/interfaces/MicroserviceProps.ts +10 -10
  139. package/src/interfaces/lambda/index.ts +1 -1
  140. package/src/interfaces/lambda/lambda-props.ts +15 -15
  141. package/src/interfaces/timer-job/timer-job-props.ts +6 -6
  142. package/src/resources/base/baseResource.ts +14 -14
  143. package/src/resources/base/tsgBaseResource.ts +21 -21
  144. package/src/resources/certificate/createCertificate.ts +57 -57
  145. package/src/resources/cognito/createCognito.ts +47 -47
  146. package/src/resources/cognito/createCognitoProps.ts +46 -46
  147. package/src/resources/customResource/createSeedDatabaseCustomResource.ts +36 -36
  148. package/src/resources/dynamodb/CreateDynamo.ts +110 -110
  149. package/src/resources/dynamodb/CreateDynamoSingleTableDesign.ts +111 -111
  150. package/src/resources/ec2/create-ec2-instance.ts +52 -52
  151. package/src/resources/ecr/CreateEcr.ts +57 -57
  152. package/src/resources/gateway/CreateApiAndAttachLambdas.ts +161 -161
  153. package/src/resources/gateway/createApi.ts +173 -173
  154. package/src/resources/gateway/createApiKey.ts +88 -88
  155. package/src/resources/helpers/createCommonLayer.ts +18 -18
  156. package/src/resources/helpers/createRoutes.ts +74 -74
  157. package/src/resources/index.ts +1 -1
  158. package/src/resources/lambda/create-basic-lambda-helper.ts +128 -243
  159. package/src/resources/lambda/create-basic-lambda.ts +161 -279
  160. package/src/resources/lambda/createLambda.ts +203 -203
  161. package/src/resources/lambda/lambdaHelper.ts +133 -133
  162. package/src/resources/lambda-authorizer/TsgJwtTokenAuthorizer.ts +107 -107
  163. package/src/resources/lambda-authorizer/TsgRequestAuthorizer.ts +44 -44
  164. package/src/resources/lambda-authorizer/createAuthorizerHelpers.ts +76 -76
  165. package/src/resources/lambda-layer/createLambdaLayer.ts +39 -39
  166. package/src/resources/layers/common/package.json +22 -22
  167. package/src/resources/layers/common/src/nodejs/api-response/responses.ts +41 -41
  168. package/src/resources/layers/common/src/nodejs/index.ts +3 -3
  169. package/src/resources/layers/common/src/nodejs/logger/IContext.ts +6 -6
  170. package/src/resources/layers/common/src/nodejs/logger/index.ts +1 -1
  171. package/src/resources/layers/common/src/nodejs/logger/logger.ts +10 -10
  172. package/src/resources/layers/common/tsconfig.json +7 -7
  173. package/src/resources/resources/iam/policyDocuments/cognitoPolicy.ts +54 -54
  174. package/src/resources/resources/iam/roles/createRole.ts +29 -29
  175. package/src/resources/route53/CreateZone.ts +61 -61
  176. package/src/resources/route53/create-zones.ts +40 -40
  177. package/src/resources/securityManager/index.ts +23 -23
  178. package/tsconfig.json +44 -44
@@ -1,279 +1,161 @@
1
- import { Duration } from "aws-cdk-lib";
2
- import { ServicePrincipal } from "aws-cdk-lib/aws-iam";
3
- import { LayerVersion, Runtime } from "aws-cdk-lib/aws-lambda";
4
- import {
5
- NodejsFunction,
6
- NodejsFunctionProps,
7
- SourceMapMode,
8
- } from "aws-cdk-lib/aws-lambda-nodejs";
9
- import { RetentionDays } from "aws-cdk-lib/aws-logs";
10
- import fs = require("fs");
11
- import path = require("path");
12
- import { TimerJobProps } from "../../interfaces/timer-job";
13
- import { Construct } from "constructs";
14
- import { SERVICE_PRINCIPAL } from "../../constants/aws-service-principal-constants";
15
- import { CronOptions, Rule, Schedule } from "aws-cdk-lib/aws-events";
16
- import { LambdaFunction } from "aws-cdk-lib/aws-events-targets";
17
- import { Table } from "aws-cdk-lib/aws-dynamodb";
18
-
19
- const isSubPath = (root: string, target: string): boolean => {
20
- const relative = path.relative(path.resolve(root), path.resolve(target));
21
- return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
22
- };
23
-
24
- const LOCK_FILE_NAMES = ["pnpm-lock.yaml", "yarn.lock", "package-lock.json"];
25
-
26
- const resolveProjectRoot = (projectRoot?: string): string | undefined => {
27
- if (!projectRoot) {
28
- return undefined;
29
- }
30
-
31
- return path.isAbsolute(projectRoot)
32
- ? path.resolve(projectRoot)
33
- : path.resolve(process.cwd(), projectRoot);
34
- };
35
-
36
- const findNearestLockFile = (startDir: string): string | undefined => {
37
- let current = path.resolve(startDir);
38
- const { root } = path.parse(current);
39
-
40
- while (true) {
41
- for (const candidate of LOCK_FILE_NAMES) {
42
- const resolvedCandidate = path.join(current, candidate);
43
- if (fs.existsSync(resolvedCandidate) && fs.statSync(resolvedCandidate).isFile()) {
44
- return resolvedCandidate;
45
- }
46
- }
47
-
48
- if (current === root) {
49
- return undefined;
50
- }
51
-
52
- current = path.dirname(current);
53
- }
54
- };
55
-
56
- const findCommonAncestor = (paths: string[]): string | undefined => {
57
- if (paths.length === 0) {
58
- return undefined;
59
- }
60
-
61
- let ancestor = path.resolve(paths[0]);
62
-
63
- for (const currentPath of paths.slice(1)) {
64
- const target = path.resolve(currentPath);
65
- const ancestorRoot = path.parse(ancestor).root;
66
-
67
- if (path.parse(ancestor).root.toLowerCase() !== path.parse(target).root.toLowerCase()) {
68
- return undefined;
69
- }
70
-
71
- let candidate = ancestor;
72
- while (!isSubPath(candidate, target)) {
73
- if (candidate === ancestorRoot) {
74
- candidate = ancestorRoot;
75
- break;
76
- }
77
- candidate = path.dirname(candidate);
78
- }
79
-
80
- ancestor = candidate;
81
- }
82
-
83
- return ancestor;
84
- };
85
-
86
- const resolvePath = (inputPath: string, projectRoot?: string): string => {
87
- if (path.isAbsolute(inputPath)) {
88
- return path.resolve(inputPath);
89
- }
90
-
91
- if (projectRoot) {
92
- const candidateFromProjectRoot = path.resolve(projectRoot, inputPath);
93
- if (isSubPath(projectRoot, candidateFromProjectRoot)) {
94
- return candidateFromProjectRoot;
95
- }
96
- }
97
-
98
- return path.resolve(process.cwd(), inputPath);
99
- };
100
-
101
- export const createBasicLambdaTimerJob = (
102
- scope: Construct,
103
- props: TimerJobProps
104
- ): NodejsFunction => {
105
- const lambdaProps = createBasicLambdaProps(props);
106
-
107
- let lambdaFunction = new NodejsFunction(
108
- scope,
109
- `${props.appPrefix}${props.functionName}`,
110
- lambdaProps
111
- );
112
-
113
- addInvokePermissionToLambdaForEvents(lambdaFunction);
114
-
115
- const eventRule = createEventRuleForLambda(
116
- scope,
117
- lambdaFunction,
118
- props.cronOptions
119
- );
120
-
121
- eventRule.addTarget(new LambdaFunction(lambdaFunction));
122
-
123
- grantAccessToDynamoTables(scope, lambdaFunction, props.dynamoTableNames);
124
-
125
- addLambdaLayers(scope, lambdaFunction, props.lambdaLayerArn);
126
-
127
- return lambdaFunction;
128
- };
129
-
130
- const createBasicLambdaProps = (props: TimerJobProps): NodejsFunctionProps => {
131
- const resolvedProjectRoot = resolveProjectRoot(props.projectRoot);
132
- const defaultRelativeEntry = path.join(
133
- "resources",
134
- "lambdas",
135
- "timer-jobs",
136
- props.functionName,
137
- "main.mts"
138
- );
139
- const entryPath = props.codePath ?? defaultRelativeEntry;
140
- const resolvedEntry = resolvePath(entryPath, resolvedProjectRoot);
141
-
142
- const cwd = path.resolve(process.cwd());
143
- const entryDir = path.dirname(resolvedEntry);
144
-
145
- let effectiveProjectRoot = resolvedProjectRoot ?? cwd;
146
- if (!isSubPath(effectiveProjectRoot, resolvedEntry)) {
147
- const ancestor = findCommonAncestor([effectiveProjectRoot, entryDir]);
148
- if (!ancestor) {
149
- throw new Error(
150
- `Unable to determine a projectRoot that contains both ${effectiveProjectRoot} and ${resolvedEntry}.`
151
- );
152
- }
153
- effectiveProjectRoot = ancestor;
154
- }
155
-
156
- if (!isSubPath(effectiveProjectRoot, resolvedEntry)) {
157
- throw new Error(
158
- `Resolved lambda entry ${resolvedEntry} must be located within projectRoot ${effectiveProjectRoot}.`
159
- );
160
- }
161
-
162
- let resolvedDepsLockFilePath = props.depsLockFilePath
163
- ? resolvePath(props.depsLockFilePath, resolvedProjectRoot)
164
- : undefined;
165
-
166
- if (!resolvedDepsLockFilePath) {
167
- const potentialLockFiles = [
168
- findNearestLockFile(entryDir),
169
- findNearestLockFile(cwd),
170
- effectiveProjectRoot !== cwd ? findNearestLockFile(effectiveProjectRoot) : undefined,
171
- ];
172
-
173
- for (const lockFile of potentialLockFiles) {
174
- if (lockFile && isSubPath(effectiveProjectRoot, lockFile)) {
175
- resolvedDepsLockFilePath = lockFile;
176
- break;
177
- }
178
- }
179
- }
180
-
181
- if (
182
- resolvedDepsLockFilePath &&
183
- !isSubPath(effectiveProjectRoot, resolvedDepsLockFilePath)
184
- ) {
185
- throw new Error(
186
- `Resolved depsLockFilePath ${resolvedDepsLockFilePath} must be located within projectRoot ${effectiveProjectRoot}.`
187
- );
188
- }
189
-
190
- const environmentVars = props.envs ?? {};
191
-
192
- const lambdaProp: NodejsFunctionProps = {
193
- entry: resolvedEntry,
194
- functionName: `${props.appPrefix ? `${props.appPrefix}-` : ""}${
195
- props.functionName
196
- }`,
197
- handler: "main.ts",
198
- logRetention: RetentionDays.TWO_WEEKS,
199
- runtime: Runtime.NODEJS_LATEST,
200
- timeout: Duration.minutes(
201
- props.timeoutInMinutes ? props.timeoutInMinutes : 1
202
- ),
203
- memorySize: props.memory,
204
- environment: {
205
- ...environmentVars,
206
- },
207
- bundling: {
208
- minify: true,
209
- target: `esnext`,
210
- sourceMap: true,
211
- sourceMapMode: SourceMapMode.EXTERNAL,
212
- environment: {
213
- ...environmentVars,
214
- },
215
- },
216
- role: props.role,
217
- layers: undefined,
218
- ...(effectiveProjectRoot && { projectRoot: effectiveProjectRoot }),
219
- ...(resolvedDepsLockFilePath && {
220
- depsLockFilePath: resolvedDepsLockFilePath,
221
- }),
222
- };
223
-
224
- return lambdaProp;
225
- };
226
-
227
- const addInvokePermissionToLambdaForEvents = (lambda: NodejsFunction) => {
228
- lambda.addPermission(`InvokePermission-${lambda.functionName}`, {
229
- principal: new ServicePrincipal(SERVICE_PRINCIPAL.EVENTS),
230
- });
231
- };
232
-
233
- const createEventRuleForLambda = (
234
- scope: Construct,
235
- lambda: NodejsFunction,
236
- options: CronOptions
237
- ) => {
238
- const eventRule = new Rule(
239
- scope,
240
- `scheduleRule-${lambda?.node.id || "010"}`,
241
- {
242
- schedule: Schedule.cron(options),
243
- }
244
- );
245
-
246
- return eventRule;
247
- };
248
-
249
- const grantAccessToDynamoTables = (
250
- scope: Construct,
251
- lambda: NodejsFunction,
252
- tableNames?: string[]
253
- ) => {
254
- if (tableNames && tableNames.length > 0) {
255
- tableNames.forEach((tableName) => {
256
- const table = Table.fromTableName(scope, `${tableName}-table`, tableName);
257
-
258
- table.grantReadWriteData(lambda);
259
- });
260
- }
261
- };
262
-
263
- const addLambdaLayers = (
264
- scope: Construct,
265
- lambda: NodejsFunction,
266
- layerArns?: string[]
267
- ) => {
268
- if (layerArns && layerArns.length > 0) {
269
- layerArns.forEach((arn: string, idx: number) => {
270
- const layer = LayerVersion.fromLayerVersionArn(
271
- scope,
272
- `common-layer-${idx}`,
273
- arn
274
- );
275
-
276
- lambda.addLayers(layer);
277
- });
278
- }
279
- };
1
+ import { Duration } from "aws-cdk-lib";
2
+ import { ServicePrincipal } from "aws-cdk-lib/aws-iam";
3
+ import { LayerVersion, Runtime } from "aws-cdk-lib/aws-lambda";
4
+ import {
5
+ NodejsFunction,
6
+ NodejsFunctionProps,
7
+ SourceMapMode,
8
+ } from "aws-cdk-lib/aws-lambda-nodejs";
9
+ import { RetentionDays } from "aws-cdk-lib/aws-logs";
10
+ import path = require("path");
11
+ import { TimerJobProps } from "../../interfaces/timer-job";
12
+ import { Construct } from "constructs";
13
+ import { SERVICE_PRINCIPAL } from "../../constants/aws-service-principal-constants";
14
+ import { CronOptions, Rule, Schedule } from "aws-cdk-lib/aws-events";
15
+ import { LambdaFunction } from "aws-cdk-lib/aws-events-targets";
16
+ import { Table } from "aws-cdk-lib/aws-dynamodb";
17
+
18
+ export const createBasicLambdaTimerJob = (
19
+ scope: Construct,
20
+ props: TimerJobProps
21
+ ): NodejsFunction => {
22
+ const lambdaProps = createBasicLambdaProps(props);
23
+
24
+ const functionName = `${props.appPrefix}${props.functionName}`;
25
+
26
+ let lambdaFunction = new NodejsFunction(
27
+ scope,
28
+ functionName,
29
+ lambdaProps
30
+ );
31
+
32
+ addInvokePermissionToLambdaForEvents(lambdaFunction, functionName);
33
+
34
+ const eventRule = createEventRuleForLambda(
35
+ scope,
36
+ lambdaFunction,
37
+ props.cronOptions
38
+ );
39
+
40
+ eventRule.addTarget(new LambdaFunction(lambdaFunction));
41
+
42
+ grantAccessToDynamoTables(scope, lambdaFunction, props.dynamoTableNames);
43
+
44
+ addLambdaLayers(scope, lambdaFunction, props.lambdaLayerArn);
45
+
46
+ return lambdaFunction;
47
+ };
48
+
49
+ const createBasicLambdaProps = (props: TimerJobProps): NodejsFunctionProps => {
50
+ let resolvedEntry: string;
51
+
52
+ if (props.codePath && path.isAbsolute(props.codePath)) {
53
+ resolvedEntry = props.codePath;
54
+ } else if (props.codePath && props.projectRoot) {
55
+ // codePath is relative, resolve from current directory + projectRoot
56
+ resolvedEntry = path.resolve(process.cwd(), props.codePath);
57
+ } else if (props.projectRoot) {
58
+ // No codePath, use default path with projectRoot
59
+ resolvedEntry = path.resolve(
60
+ process.cwd(),
61
+ props.projectRoot,
62
+ `resources/lambdas/timer-jobs/${props.functionName}/main.mts`
63
+ );
64
+ } else if (props.codePath) {
65
+ // codePath without projectRoot
66
+ resolvedEntry = path.resolve(process.cwd(), props.codePath);
67
+ } else {
68
+ // Default path without projectRoot
69
+ resolvedEntry = path.join(
70
+ `./resources/lambdas/timer-jobs/${props.functionName}/main.mts`
71
+ );
72
+ }
73
+
74
+ const lambdaProp: NodejsFunctionProps = {
75
+ entry: resolvedEntry,
76
+ functionName: `${props.appPrefix ? `${props.appPrefix}-` : ""}${
77
+ props.functionName
78
+ }`,
79
+ handler: "main.ts",
80
+ logRetention: RetentionDays.TWO_WEEKS,
81
+ runtime: Runtime.NODEJS_LATEST,
82
+ timeout: Duration.minutes(
83
+ props.timeoutInMinutes ? props.timeoutInMinutes : 1
84
+ ),
85
+ memorySize: props.memory,
86
+ environment: {
87
+ ...props.envs,
88
+ },
89
+ bundling: {
90
+ minify: true,
91
+ target: `esnext`,
92
+ sourceMap: true,
93
+ sourceMapMode: SourceMapMode.EXTERNAL,
94
+ environment: {
95
+ ...props.envs,
96
+ },
97
+ ...(props.projectRoot && { projectRoot: props.projectRoot }),
98
+ ...(props.depsLockFilePath && {
99
+ depsLockFilePath: props.depsLockFilePath,
100
+ }),
101
+ },
102
+ role: props.role,
103
+ layers: undefined,
104
+ };
105
+
106
+ return lambdaProp;
107
+ };
108
+
109
+ const addInvokePermissionToLambdaForEvents = (lambda: NodejsFunction, functionName: string) => {
110
+ lambda.addPermission(`InvokePermission-${functionName}`, {
111
+ principal: new ServicePrincipal(SERVICE_PRINCIPAL.EVENTS),
112
+ });
113
+ };
114
+
115
+ const createEventRuleForLambda = (
116
+ scope: Construct,
117
+ lambda: NodejsFunction,
118
+ options: CronOptions
119
+ ) => {
120
+ const eventRule = new Rule(
121
+ scope,
122
+ `scheduleRule-${lambda?.node.id || "010"}`,
123
+ {
124
+ schedule: Schedule.cron(options),
125
+ }
126
+ );
127
+
128
+ return eventRule;
129
+ };
130
+
131
+ const grantAccessToDynamoTables = (
132
+ scope: Construct,
133
+ lambda: NodejsFunction,
134
+ tableNames?: string[]
135
+ ) => {
136
+ if (tableNames && tableNames.length > 0) {
137
+ tableNames.forEach((tableName) => {
138
+ const table = Table.fromTableName(scope, `${tableName}-table`, tableName);
139
+
140
+ table.grantReadWriteData(lambda);
141
+ });
142
+ }
143
+ };
144
+
145
+ const addLambdaLayers = (
146
+ scope: Construct,
147
+ lambda: NodejsFunction,
148
+ layerArns?: string[]
149
+ ) => {
150
+ if (layerArns && layerArns.length > 0) {
151
+ layerArns.forEach((arn: string, idx: number) => {
152
+ const layer = LayerVersion.fromLayerVersionArn(
153
+ scope,
154
+ `common-layer-${idx}`,
155
+ arn
156
+ );
157
+
158
+ lambda.addLayers(layer);
159
+ });
160
+ }
161
+ };