1
1
import { Construct } from 'constructs' ;
2
2
import * as lambda from 'aws-cdk-lib/aws-lambda' ;
3
3
import * as apigateway from 'aws-cdk-lib/aws-apigateway' ;
4
- import { RestApiConstructProps } from './types.js' ;
4
+ import {
5
+ ExistingDirectory ,
6
+ ExistingLambda ,
7
+ NewFromCode ,
8
+ NewFromTemplate ,
9
+ RestApiConstructProps ,
10
+ } from './types.js' ;
5
11
6
12
/**
7
13
* Rest API construct for Amplify Backend
@@ -14,12 +20,35 @@ export class RestApiConstruct extends Construct {
14
20
constructor ( scope : Construct , id : string , props : RestApiConstructProps ) {
15
21
super ( scope , id ) ;
16
22
17
- // Create a new Lambda function for the API Gateway
18
- const handler = new lambda . Function ( this , 'handler' , {
19
- runtime : lambda . Runtime . NODEJS_18_X ,
20
- handler : 'index.handler' ,
21
- code : lambda . Code . fromAsset ( props . lambdaEntry ) ,
22
- } ) ;
23
+ let code : lambda . AssetCode | lambda . InlineCode = lambda . Code . fromInline ( '' ) ;
24
+ const src = props . lambdaEntry . source ;
25
+ if ( 'path' in src ) {
26
+ const lamb = src as ExistingDirectory ;
27
+ code = lambda . Code . fromAsset ( lamb . path ) ;
28
+ } else if ( 'code' in src ) {
29
+ const lamb = src as NewFromCode ;
30
+ code = lambda . Code . fromInline ( lamb . code ) ;
31
+ } else if ( 'template' in src ) {
32
+ //TODO: Implement use of templates (which ones to support, and how - cli version is complex). The available templates depend on the runtime
33
+ const lamb = src as NewFromTemplate ;
34
+ if ( lamb . template === 'Hello World' ) {
35
+ code = lambda . Code . fromInline (
36
+ "function handler() {console.log('Hello World!');}" ,
37
+ ) ;
38
+ }
39
+ }
40
+
41
+ let handler : lambda . IFunction ;
42
+ if ( 'id' in src ) {
43
+ const lamb = src as ExistingLambda ;
44
+ handler = lambda . Function . fromFunctionName ( this , lamb . id , lamb . name ) ;
45
+ } else {
46
+ handler = new lambda . Function ( this , 'handler' , {
47
+ runtime : props . lambdaEntry . runtime ,
48
+ handler : 'index.handler' ,
49
+ code : code ,
50
+ } ) ;
51
+ }
23
52
24
53
// Create a new API Gateway REST API with the specified name
25
54
this . api = new apigateway . RestApi ( this , 'RestApi' , {
0 commit comments