-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathfactory.ts
414 lines (366 loc) · 12.2 KB
/
factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import {
BackendOutputStorageStrategy,
BackendSecret,
BackendSecretResolver,
ConstructContainerEntryGenerator,
ConstructFactory,
ConstructFactoryGetInstanceProps,
FunctionResources,
GenerateContainerEntryProps,
ResourceAccessAcceptorFactory,
ResourceNameValidator,
ResourceProvider,
SsmEnvironmentEntry,
} from '@aws-amplify/plugin-types';
import { Construct } from 'constructs';
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
import * as path from 'path';
import { getCallerDirectory } from './get_caller_directory.js';
import { Duration, Stack, Tags } from 'aws-cdk-lib';
import { CfnFunction, Runtime } from 'aws-cdk-lib/aws-lambda';
import { createRequire } from 'module';
import { FunctionEnvironmentTranslator } from './function_env_translator.js';
import { Policy } from 'aws-cdk-lib/aws-iam';
import { readFileSync } from 'fs';
import { EOL } from 'os';
import {
FunctionOutput,
functionOutputKey,
} from '@aws-amplify/backend-output-schemas';
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';
import { AttributionMetadataStorage } from '@aws-amplify/backend-output-storage';
import { fileURLToPath } from 'node:url';
import { AmplifyUserError, TagName } from '@aws-amplify/platform-core';
const functionStackType = 'function-Lambda';
export type AddEnvironmentFactory = {
addEnvironment: (key: string, value: string | BackendSecret) => void;
};
/**
* Entry point for defining a function in the Amplify ecosystem
*/
export const defineFunction = (
props: FunctionProps = {}
): ConstructFactory<
ResourceProvider<FunctionResources> &
ResourceAccessAcceptorFactory &
AddEnvironmentFactory
> => new FunctionFactory(props, new Error().stack);
export type FunctionProps = {
/**
* A name for the function.
* Defaults to the basename of the entry path if specified.
* If no entry is specified, defaults to the directory name in which this function is defined.
*
* Example:
* If entry is `./scheduled-db-backup.ts` the name will default to "scheduled-db-backup"
* If entry is not set and the function is defined in `amplify/functions/db-backup/resource.ts` the name will default to "db-backup"
*/
name?: string;
/**
* The path to the file that contains the function entry point.
* If this is a relative path, it is computed relative to the file where this function is defined
*
* Defaults to './handler.ts'
*/
entry?: string;
/**
* An amount of time in seconds between 1 second and 15 minutes.
* Must be a whole number.
* Default is 3 seconds.
*/
timeoutSeconds?: number;
/**
* An amount of memory (RAM) to allocate to the function between 128 and 10240 MB.
* Must be a whole number.
* Default is 128MB.
*/
memoryMB?: number;
/**
* Environment variables that will be available during function execution
*/
environment?: Record<string, string | BackendSecret>;
/**
* Node runtime version for the lambda environment.
*
* Defaults to the oldest NodeJS LTS version. See https://nodejs.org/en/about/previous-releases
*/
runtime?: NodeVersion;
};
/**
* Create Lambda functions in the context of an Amplify backend definition
*/
class FunctionFactory implements ConstructFactory<AmplifyFunction> {
private generator: ConstructContainerEntryGenerator;
/**
* Create a new AmplifyFunctionFactory
*/
constructor(
private readonly props: FunctionProps,
private readonly callerStack?: string
) {}
/**
* Creates an instance of AmplifyFunction within the provided Amplify context
*/
getInstance = ({
constructContainer,
outputStorageStrategy,
resourceNameValidator,
}: ConstructFactoryGetInstanceProps): AmplifyFunction => {
if (!this.generator) {
this.generator = new FunctionGenerator(
this.hydrateDefaults(resourceNameValidator),
outputStorageStrategy
);
}
return constructContainer.getOrCompute(this.generator) as AmplifyFunction;
};
private hydrateDefaults = (
resourceNameValidator?: ResourceNameValidator
): HydratedFunctionProps => {
const name = this.resolveName();
resourceNameValidator?.validate(name);
return {
name,
entry: this.resolveEntry(),
timeoutSeconds: this.resolveTimeout(),
memoryMB: this.resolveMemory(),
environment: this.props.environment ?? {},
runtime: this.resolveRuntime(),
};
};
private resolveName = () => {
// If name is set explicitly, use that
if (this.props.name) {
return this.props.name;
}
// If entry is set, use the basename of the entry path
if (this.props.entry) {
return path.parse(this.props.entry).name;
}
// Otherwise, use the directory name where the function is defined
return path.basename(getCallerDirectory(this.callerStack));
};
private resolveEntry = () => {
// if entry is not set, default to handler.ts
if (!this.props.entry) {
return path.join(getCallerDirectory(this.callerStack), 'handler.ts');
}
// if entry is absolute use that
if (path.isAbsolute(this.props.entry)) {
return this.props.entry;
}
// if entry is relative, compute with respect to the caller directory
return path.join(getCallerDirectory(this.callerStack), this.props.entry);
};
private resolveTimeout = () => {
const timeoutMin = 1;
const timeoutMax = 60 * 15; // 15 minutes in seconds
const timeoutDefault = 3;
if (this.props.timeoutSeconds === undefined) {
return timeoutDefault;
}
if (
!isWholeNumberBetweenInclusive(
this.props.timeoutSeconds,
timeoutMin,
timeoutMax
)
) {
throw new Error(
`timeoutSeconds must be a whole number between ${timeoutMin} and ${timeoutMax} inclusive`
);
}
return this.props.timeoutSeconds;
};
private resolveMemory = () => {
const memoryMin = 128;
const memoryMax = 10240;
const memoryDefault = 512;
if (this.props.memoryMB === undefined) {
return memoryDefault;
}
if (
!isWholeNumberBetweenInclusive(this.props.memoryMB, memoryMin, memoryMax)
) {
throw new Error(
`memoryMB must be a whole number between ${memoryMin} and ${memoryMax} inclusive`
);
}
return this.props.memoryMB;
};
private resolveRuntime = () => {
const runtimeDefault = 18;
// if runtime is not set, default to the oldest LTS
if (!this.props.runtime) {
return runtimeDefault;
}
if (!(this.props.runtime in nodeVersionMap)) {
throw new Error(
`runtime must be one of the following: ${Object.keys(
nodeVersionMap
).join(', ')}`
);
}
return this.props.runtime;
};
}
type HydratedFunctionProps = Required<FunctionProps>;
class FunctionGenerator implements ConstructContainerEntryGenerator {
readonly resourceGroupName = 'function';
constructor(
private readonly props: HydratedFunctionProps,
private readonly outputStorageStrategy: BackendOutputStorageStrategy<FunctionOutput>
) {}
generateContainerEntry = ({
scope,
backendSecretResolver,
}: GenerateContainerEntryProps) => {
return new AmplifyFunction(
scope,
this.props.name,
this.props,
backendSecretResolver,
this.outputStorageStrategy
);
};
}
class AmplifyFunction
extends Construct
implements
ResourceProvider<FunctionResources>,
ResourceAccessAcceptorFactory,
AddEnvironmentFactory
{
readonly resources: FunctionResources;
private readonly functionEnvironmentTranslator: FunctionEnvironmentTranslator;
constructor(
scope: Construct,
id: string,
props: HydratedFunctionProps,
backendSecretResolver: BackendSecretResolver,
outputStorageStrategy: BackendOutputStorageStrategy<FunctionOutput>
) {
super(scope, id);
const runtime = nodeVersionMap[props.runtime];
const require = createRequire(import.meta.url);
const shims =
runtime === Runtime.NODEJS_16_X
? []
: [require.resolve('./lambda-shims/cjs_shim')];
const ssmResolverFile =
runtime === Runtime.NODEJS_16_X
? require.resolve('./lambda-shims/resolve_ssm_params_sdk_v2') // use aws cdk v2 in node 16
: require.resolve('./lambda-shims/resolve_ssm_params');
const invokeSsmResolverFile = require.resolve(
'./lambda-shims/invoke_ssm_shim'
);
/**
* This code concatenates the contents of the ssm resolver and invoker into a single line that can be used as the esbuild banner content
* This banner is responsible for resolving the customer's SSM parameters at runtime
*/
const bannerCode = readFileSync(ssmResolverFile, 'utf-8')
.concat(readFileSync(invokeSsmResolverFile, 'utf-8'))
.split(new RegExp(`${EOL}|\n|\r`, 'g'))
.map((line) => line.replace(/\/\/.*$/, '')) // strip out inline comments because the banner is going to be flattened into a single line
.join('');
const functionEnvironmentTypeGenerator =
new FunctionEnvironmentTypeGenerator(id);
// esbuild runs as part of the NodejsFunction constructor, so we eagerly generate the process env shim without types so it can be included in the function bundle.
// This will be overwritten with the typed file at the end of synthesis
functionEnvironmentTypeGenerator.generateProcessEnvShim();
let functionLambda;
try {
functionLambda = new NodejsFunction(scope, `${id}-lambda`, {
entry: props.entry,
timeout: Duration.seconds(props.timeoutSeconds),
memorySize: props.memoryMB,
runtime: nodeVersionMap[props.runtime],
bundling: {
format: OutputFormat.ESM,
banner: bannerCode,
bundleAwsSDK: true,
inject: shims,
loader: {
'.node': 'file',
},
minify: true,
sourceMap: true,
},
});
} catch (error) {
throw new AmplifyUserError(
'NodeJSFunctionConstructInitializationError',
{
message: 'Failed to instantiate nodejs function construct',
resolution: 'See the underlying error message for more details.',
},
error as Error
);
}
Tags.of(functionLambda).add(TagName.FRIENDLY_NAME, id);
this.functionEnvironmentTranslator = new FunctionEnvironmentTranslator(
functionLambda,
props.environment,
backendSecretResolver,
functionEnvironmentTypeGenerator
);
this.resources = {
lambda: functionLambda,
cfnResources: {
cfnFunction: functionLambda.node.findChild('Resource') as CfnFunction,
},
};
this.storeOutput(outputStorageStrategy);
new AttributionMetadataStorage().storeAttributionMetadata(
Stack.of(this),
functionStackType,
fileURLToPath(new URL('../package.json', import.meta.url))
);
}
addEnvironment = (key: string, value: string | BackendSecret) => {
this.functionEnvironmentTranslator.addEnvironmentEntry(key, value);
};
getResourceAccessAcceptor = () => ({
identifier: `${this.node.id}LambdaResourceAccessAcceptor`,
acceptResourceAccess: (
policy: Policy,
ssmEnvironmentEntries: SsmEnvironmentEntry[]
) => {
const role = this.resources.lambda.role;
if (!role) {
// This should never happen since we are using the Function L2 construct
throw new Error(
'No execution role found to attach lambda permissions to'
);
}
policy.attachToRole(role);
ssmEnvironmentEntries.forEach(({ name, path }) => {
this.functionEnvironmentTranslator.addSsmEnvironmentEntry(name, path);
});
},
});
/**
* Store storage outputs using provided strategy
*/
private storeOutput = (
outputStorageStrategy: BackendOutputStorageStrategy<FunctionOutput>
): void => {
outputStorageStrategy.appendToBackendOutputList(functionOutputKey, {
version: '1',
payload: {
definedFunctions: this.resources.lambda.functionName,
},
});
};
}
const isWholeNumberBetweenInclusive = (
test: number,
min: number,
max: number
) => min <= test && test <= max && test % 1 === 0;
export type NodeVersion = 16 | 18 | 20;
const nodeVersionMap: Record<NodeVersion, Runtime> = {
16: Runtime.NODEJS_16_X,
18: Runtime.NODEJS_18_X,
20: Runtime.NODEJS_20_X,
};