@@ -19,6 +19,7 @@ import { Runtime } from 'aws-cdk-lib/aws-lambda';
19
19
import { Policy , PolicyStatement } from 'aws-cdk-lib/aws-iam' ;
20
20
import fsp from 'fs/promises' ;
21
21
import path from 'node:path' ;
22
+ import { AmplifyUserError } from '@aws-amplify/platform-core' ;
22
23
23
24
const createStackAndSetContext = ( ) : Stack => {
24
25
const app = new App ( ) ;
@@ -207,9 +208,10 @@ void describe('AmplifyFunctionFactory', () => {
207
208
entry : './test-assets/default-lambda/handler.ts' ,
208
209
timeoutSeconds : 0 ,
209
210
} ) . getInstance ( getInstanceProps ) ,
210
- new Error (
211
- 'timeoutSeconds must be a whole number between 1 and 900 inclusive'
212
- )
211
+ new AmplifyUserError ( 'InvalidTimeoutError' , {
212
+ message : `Invalid function timeout of 0` ,
213
+ resolution : `timeoutSeconds must be a whole number between 1 and 900 inclusive` ,
214
+ } )
213
215
) ;
214
216
} ) ;
215
217
@@ -220,9 +222,10 @@ void describe('AmplifyFunctionFactory', () => {
220
222
entry : './test-assets/default-lambda/handler.ts' ,
221
223
timeoutSeconds : 901 ,
222
224
} ) . getInstance ( getInstanceProps ) ,
223
- new Error (
224
- 'timeoutSeconds must be a whole number between 1 and 900 inclusive'
225
- )
225
+ new AmplifyUserError ( 'InvalidTimeoutError' , {
226
+ message : `Invalid function timeout of 901` ,
227
+ resolution : `timeoutSeconds must be a whole number between 1 and 900 inclusive` ,
228
+ } )
226
229
) ;
227
230
} ) ;
228
231
@@ -233,9 +236,10 @@ void describe('AmplifyFunctionFactory', () => {
233
236
entry : './test-assets/default-lambda/handler.ts' ,
234
237
timeoutSeconds : 10.5 ,
235
238
} ) . getInstance ( getInstanceProps ) ,
236
- new Error (
237
- 'timeoutSeconds must be a whole number between 1 and 900 inclusive'
238
- )
239
+ new AmplifyUserError ( 'InvalidTimeoutError' , {
240
+ message : `Invalid function timeout of 10.5` ,
241
+ resolution : `timeoutSeconds must be a whole number between 1 and 900 inclusive` ,
242
+ } )
239
243
) ;
240
244
} ) ;
241
245
} ) ;
@@ -271,9 +275,10 @@ void describe('AmplifyFunctionFactory', () => {
271
275
entry : './test-assets/default-lambda/handler.ts' ,
272
276
memoryMB : 127 ,
273
277
} ) . getInstance ( getInstanceProps ) ,
274
- new Error (
275
- 'memoryMB must be a whole number between 128 and 10240 inclusive'
276
- )
278
+ new AmplifyUserError ( 'InvalidMemoryMBError' , {
279
+ message : `Invalid function memoryMB of 127` ,
280
+ resolution : `memoryMB must be a whole number between 128 and 10240 inclusive` ,
281
+ } )
277
282
) ;
278
283
} ) ;
279
284
@@ -284,9 +289,10 @@ void describe('AmplifyFunctionFactory', () => {
284
289
entry : './test-assets/default-lambda/handler.ts' ,
285
290
memoryMB : 10241 ,
286
291
} ) . getInstance ( getInstanceProps ) ,
287
- new Error (
288
- 'memoryMB must be a whole number between 128 and 10240 inclusive'
289
- )
292
+ new AmplifyUserError ( 'InvalidMemoryMBError' , {
293
+ message : `Invalid function memoryMB of 10241` ,
294
+ resolution : `memoryMB must be a whole number between 128 and 10240 inclusive` ,
295
+ } )
290
296
) ;
291
297
} ) ;
292
298
@@ -297,9 +303,103 @@ void describe('AmplifyFunctionFactory', () => {
297
303
entry : './test-assets/default-lambda/handler.ts' ,
298
304
memoryMB : 256.2 ,
299
305
} ) . getInstance ( getInstanceProps ) ,
300
- new Error (
301
- 'memoryMB must be a whole number between 128 and 10240 inclusive'
302
- )
306
+ new AmplifyUserError ( 'InvalidMemoryMBError' , {
307
+ message : `Invalid function memoryMB of 256.2` ,
308
+ resolution : `memoryMB must be a whole number between 128 and 10240 inclusive` ,
309
+ } )
310
+ ) ;
311
+ } ) ;
312
+ } ) ;
313
+
314
+ void describe ( 'environment property' , ( ) => {
315
+ void it ( 'sets valid environment' , ( ) => {
316
+ const functionFactory = defineFunction ( {
317
+ entry : './test-assets/default-lambda/handler.ts' ,
318
+ name : 'myCoolLambda' ,
319
+ environment : {
320
+ TEST_ENV : 'testValue' ,
321
+ } ,
322
+ } ) ;
323
+ const lambda = functionFactory . getInstance ( getInstanceProps ) ;
324
+ const stack = lambda . stack ;
325
+ const template = Template . fromStack ( stack ) ;
326
+ template . resourceCountIs ( 'AWS::Lambda::Function' , 1 ) ;
327
+ template . hasResourceProperties ( 'AWS::Lambda::Function' , {
328
+ Environment : {
329
+ Variables : {
330
+ TEST_ENV : 'testValue' ,
331
+ } ,
332
+ } ,
333
+ } ) ;
334
+ } ) ;
335
+
336
+ void it ( 'sets default environment' , ( ) => {
337
+ const functionFactory = defineFunction ( {
338
+ entry : './test-assets/default-lambda/handler.ts' ,
339
+ name : 'myCoolLambda' ,
340
+ } ) ;
341
+ const lambda = functionFactory . getInstance ( getInstanceProps ) ;
342
+ const stack = lambda . stack ;
343
+ const template = Template . fromStack ( stack ) ;
344
+ template . resourceCountIs ( 'AWS::Lambda::Function' , 1 ) ;
345
+ template . hasResourceProperties ( 'AWS::Lambda::Function' , {
346
+ Environment : { } ,
347
+ } ) ;
348
+ } ) ;
349
+
350
+ void it ( 'throws when adding environment variables with invalid key' , ( ) => {
351
+ assert . throws (
352
+ ( ) =>
353
+ defineFunction ( {
354
+ entry : './test-assets/default-lambda/handler.ts' ,
355
+ name : 'myCoolLambda' ,
356
+ environment : {
357
+ 'this.is.wrong' : 'testValue' ,
358
+ } ,
359
+ } ) . getInstance ( getInstanceProps ) ,
360
+ new AmplifyUserError ( 'InvalidEnvironmentKeyError' , {
361
+ message : `Invalid function environment key(s): this.is.wrong` ,
362
+ resolution :
363
+ 'Environment keys must match [a-zA-Z]([a-zA-Z0-9_])+ and be at least 2 characters' ,
364
+ } )
365
+ ) ;
366
+ } ) ;
367
+
368
+ void it ( 'throws when adding environment variables with key less than 2 characters' , ( ) => {
369
+ assert . throws (
370
+ ( ) =>
371
+ defineFunction ( {
372
+ entry : './test-assets/default-lambda/handler.ts' ,
373
+ name : 'myCoolLambda' ,
374
+ environment : {
375
+ A : 'testValue' ,
376
+ } ,
377
+ } ) . getInstance ( getInstanceProps ) ,
378
+ new AmplifyUserError ( 'InvalidEnvironmentKeyError' , {
379
+ message : `Invalid function environment key(s): A` ,
380
+ resolution :
381
+ 'Environment keys must match [a-zA-Z]([a-zA-Z0-9_])+ and be at least 2 characters' ,
382
+ } )
383
+ ) ;
384
+ } ) ;
385
+
386
+ void it ( 'throws when multiple environment variables are invalid' , ( ) => {
387
+ assert . throws (
388
+ ( ) =>
389
+ defineFunction ( {
390
+ entry : './test-assets/default-lambda/handler.ts' ,
391
+ name : 'lambdaWithMultipleEnvVars' ,
392
+ environment : {
393
+ A : 'testValueA' ,
394
+ TEST_ENV : 'envValue' ,
395
+ 'this.is.wrong' : 'testValue' ,
396
+ } ,
397
+ } ) . getInstance ( getInstanceProps ) ,
398
+ new AmplifyUserError ( 'InvalidEnvironmentKeyError' , {
399
+ message : `Invalid function environment key(s): A, this.is.wrong` ,
400
+ resolution :
401
+ 'Environment keys must match [a-zA-Z]([a-zA-Z0-9_])+ and be at least 2 characters' ,
402
+ } )
303
403
) ;
304
404
} ) ;
305
405
} ) ;
@@ -335,7 +435,10 @@ void describe('AmplifyFunctionFactory', () => {
335
435
entry : './test-assets/default-lambda/handler.ts' ,
336
436
runtime : 14 as NodeVersion ,
337
437
} ) . getInstance ( getInstanceProps ) ,
338
- new Error ( 'runtime must be one of the following: 16, 18, 20, 22' )
438
+ new AmplifyUserError ( 'InvalidRuntimeError' , {
439
+ message : `Invalid function runtime of 14` ,
440
+ resolution : 'runtime must be one of the following: 16, 18, 20, 22' ,
441
+ } )
339
442
) ;
340
443
} ) ;
341
444
0 commit comments