@@ -31,17 +31,25 @@ import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.
31
31
import { AttributionMetadataStorage } from '@aws-amplify/backend-output-storage' ;
32
32
import { fileURLToPath } from 'node:url' ;
33
33
import { AmplifyUserError , TagName } from '@aws-amplify/platform-core' ;
34
+ import * as lambda from 'aws-cdk-lib/aws-lambda' ;
34
35
35
36
const functionStackType = 'function-Lambda' ;
36
37
37
38
/**
38
39
* Entry point for defining a function in the Amplify ecosystem
39
40
*/
40
41
export const defineFunction = (
41
- props : FunctionProps = { }
42
+ props ? : FunctionProps | ( ( scope : Construct ) => lambda . Function )
42
43
) : ConstructFactory <
43
44
ResourceProvider < FunctionResources > & ResourceAccessAcceptorFactory
44
- > => new FunctionFactory ( props , new Error ( ) . stack ) ;
45
+ > => {
46
+ if ( props === undefined || props === null ) {
47
+ return new FunctionFactory ( { } , new Error ( ) . stack ) ;
48
+ } else if ( typeof props === 'function' ) {
49
+ return new FunctionFactory ( { } , new Error ( ) . stack , props ) ;
50
+ }
51
+ return new FunctionFactory ( props , new Error ( ) . stack ) ;
52
+ } ;
45
53
46
54
export type FunctionProps = {
47
55
/**
@@ -99,7 +107,8 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
99
107
*/
100
108
constructor (
101
109
private readonly props : FunctionProps ,
102
- private readonly callerStack ?: string
110
+ private readonly callerStack ?: string ,
111
+ private readonly callback ?: ( scope : Construct ) => lambda . Function
103
112
) { }
104
113
105
114
/**
@@ -113,7 +122,8 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
113
122
if ( ! this . generator ) {
114
123
this . generator = new FunctionGenerator (
115
124
this . hydrateDefaults ( resourceNameValidator ) ,
116
- outputStorageStrategy
125
+ outputStorageStrategy ,
126
+ this . callback as ( scope : Construct ) => lambda . Function
117
127
) ;
118
128
}
119
129
return constructContainer . getOrCompute ( this . generator ) as AmplifyFunction ;
@@ -229,7 +239,8 @@ class FunctionGenerator implements ConstructContainerEntryGenerator {
229
239
230
240
constructor (
231
241
private readonly props : HydratedFunctionProps ,
232
- private readonly outputStorageStrategy : BackendOutputStorageStrategy < FunctionOutput >
242
+ private readonly outputStorageStrategy : BackendOutputStorageStrategy < FunctionOutput > ,
243
+ private readonly callback : ( scope : Construct ) => lambda . Function
233
244
) { }
234
245
235
246
generateContainerEntry = ( {
@@ -241,7 +252,8 @@ class FunctionGenerator implements ConstructContainerEntryGenerator {
241
252
this . props . name ,
242
253
this . props ,
243
254
backendSecretResolver ,
244
- this . outputStorageStrategy
255
+ this . outputStorageStrategy ,
256
+ this . callback
245
257
) ;
246
258
} ;
247
259
}
@@ -257,7 +269,8 @@ class AmplifyFunction
257
269
id : string ,
258
270
props : HydratedFunctionProps ,
259
271
backendSecretResolver : BackendSecretResolver ,
260
- outputStorageStrategy : BackendOutputStorageStrategy < FunctionOutput >
272
+ outputStorageStrategy : BackendOutputStorageStrategy < FunctionOutput > ,
273
+ callback : ( scope : Construct ) => lambda . Function
261
274
) {
262
275
super ( scope , id ) ;
263
276
@@ -298,20 +311,24 @@ class AmplifyFunction
298
311
299
312
let functionLambda ;
300
313
try {
301
- functionLambda = new NodejsFunction ( scope , `${ id } -lambda` , {
302
- entry : props . entry ,
303
- timeout : Duration . seconds ( props . timeoutSeconds ) ,
304
- memorySize : props . memoryMB ,
305
- runtime : nodeVersionMap [ props . runtime ] ,
306
- bundling : {
307
- format : OutputFormat . ESM ,
308
- banner : bannerCode ,
309
- inject : shims ,
310
- loader : {
311
- '.node' : 'file' ,
314
+ if ( callback != null ) {
315
+ functionLambda = callback ( scope ) ;
316
+ } else {
317
+ functionLambda = new NodejsFunction ( scope , `${ id } -lambda` , {
318
+ entry : props . entry ,
319
+ timeout : Duration . seconds ( props . timeoutSeconds ) ,
320
+ memorySize : props . memoryMB ,
321
+ runtime : nodeVersionMap [ props . runtime ] ,
322
+ bundling : {
323
+ format : OutputFormat . ESM ,
324
+ banner : bannerCode ,
325
+ inject : shims ,
326
+ loader : {
327
+ '.node' : 'file' ,
328
+ } ,
312
329
} ,
313
- } ,
314
- } ) ;
330
+ } ) ;
331
+ }
315
332
} catch ( error ) {
316
333
throw new AmplifyUserError (
317
334
'NodeJSFunctionConstructInitializationError' ,
0 commit comments