@@ -2,32 +2,49 @@ import * as ros from '@alicloud/ros-cdk-core';
22import * as fc from '@alicloud/ros-cdk-fc' ;
33import { ActionContext , ServerlessIac } from '../types' ;
44import { printer , rosStackDeploy } from '../common' ;
5+ import path from 'node:path' ;
6+ import * as fs from 'node:fs' ;
7+
8+ const resolveCode = ( location : string ) : string => {
9+ const filePath = path . resolve ( process . cwd ( ) , location ) ;
10+ const fileContent = fs . readFileSync ( filePath ) ;
11+
12+ return fileContent . toString ( 'base64' ) ;
13+ } ;
514
615export class IacStack extends ros . Stack {
716 constructor ( scope : ros . Construct , iac : ServerlessIac , props ?: ros . StackProps ) {
817 super ( scope , iac . service , props ) ;
918 new ros . RosInfo ( this , ros . RosInfo . description , 'This is the simple ros cdk app example.' ) ;
10- iac . functions . map (
11- ( fnc ) =>
12- new fc . RosFunction (
13- this ,
14- fnc . name ,
15- {
16- functionName : fnc . name ,
17- serviceName : `${ fnc . name } -service` ,
18- handler : fnc . handler ,
19- runtime : fnc . runtime ,
20- memorySize : fnc . memory ,
21- timeout : fnc . timeout ,
22- environmentVariables : fnc . environment ,
23- code : {
24- zipFile :
25- 'exports.handler = function(event, context, callback) { callback(null, "Hello World"); }' ,
26- } ,
27- } ,
28- false ,
29- ) ,
19+ const service = new fc . RosService (
20+ this ,
21+ `${ iac . service } -service` ,
22+ {
23+ serviceName : `${ iac . service } -service` ,
24+ } ,
25+ true ,
3026 ) ;
27+
28+ iac . functions . forEach ( ( fnc ) => {
29+ const func = new fc . RosFunction (
30+ this ,
31+ fnc . key ,
32+ {
33+ functionName : fnc . name ,
34+ serviceName : service . serviceName ,
35+ handler : fnc . handler ,
36+ runtime : fnc . runtime ,
37+ memorySize : fnc . memory ,
38+ timeout : fnc . timeout ,
39+ environmentVariables : fnc . environment ,
40+ code : {
41+ zipFile : resolveCode ( fnc . code ) ,
42+ } ,
43+ } ,
44+ true ,
45+ ) ;
46+ func . addDependsOn ( service ) ;
47+ } ) ;
3148 }
3249}
3350
0 commit comments