11import * as ros from '@alicloud/ros-cdk-core' ;
22import * as fc from '@alicloud/ros-cdk-fc' ;
3- import { ActionContext , ServerlessIac } from '../types' ;
3+ import * as agw from '@alicloud/ros-cdk-apigateway' ;
4+ import * as ram from '@alicloud/ros-cdk-ram' ;
5+
6+ import { ActionContext , EventTypes , ServerlessIac } from '../types' ;
47import { printer , rosStackDeploy } from '../common' ;
58import path from 'node:path' ;
69import * as fs from 'node:fs' ;
@@ -13,9 +16,10 @@ const resolveCode = (location: string): string => {
1316} ;
1417
1518export class IacStack extends ros . Stack {
16- constructor ( scope : ros . Construct , iac : ServerlessIac , props ?: ros . StackProps ) {
17- super ( scope , iac . service , props ) ;
18- new ros . RosInfo ( this , ros . RosInfo . description , 'This is the simple ros cdk app example.' ) ;
19+ constructor ( scope : ros . Construct , iac : ServerlessIac , context : ActionContext ) {
20+ super ( scope , iac . service ) ;
21+ new ros . RosInfo ( this , ros . RosInfo . description , `${ iac . service } stack` ) ;
22+
1923 const service = new fc . RosService (
2024 this ,
2125 `${ iac . service } -service` ,
@@ -45,12 +49,98 @@ export class IacStack extends ros.Stack {
4549 ) ;
4650 func . addDependsOn ( service ) ;
4751 } ) ;
52+
53+ const apiGateway = iac . events . find ( ( event ) => event . type === EventTypes . API_GATEWAY ) ;
54+ if ( apiGateway ) {
55+ const gatewayAccessRole = new ram . RosRole (
56+ this ,
57+ `${ iac . service } _role` ,
58+ {
59+ roleName : `${ iac . service } -gateway-access-role` ,
60+ description : `${ iac . service } role` ,
61+ assumeRolePolicyDocument : {
62+ version : '1' ,
63+ statement : [
64+ {
65+ action : 'sts:AssumeRole' ,
66+ effect : 'Allow' ,
67+ principal : {
68+ service : [ 'apigateway.aliyuncs.com' ] ,
69+ } ,
70+ } ,
71+ ] ,
72+ } ,
73+ policies : [
74+ {
75+ policyName : `${ iac . service } -policy` ,
76+ policyDocument : {
77+ version : '1' ,
78+ statement : [
79+ {
80+ action : [ 'fc:InvokeFunction' ] ,
81+ effect : 'Allow' ,
82+ // @TODO implement at least permission granting
83+ resource : [ '*' ] ,
84+ } ,
85+ ] ,
86+ } ,
87+ } ,
88+ ] ,
89+ } ,
90+ true ,
91+ ) ;
92+
93+ const apiGatewayGroup = new agw . RosGroup (
94+ this ,
95+ `${ iac . service } _apigroup` ,
96+ {
97+ groupName : `${ iac . service } _apigroup` ,
98+ } ,
99+ true ,
100+ ) ;
101+
102+ iac . events
103+ . filter ( ( event ) => event . type === EventTypes . API_GATEWAY )
104+ . forEach ( ( event ) => {
105+ event . triggers . forEach ( ( trigger ) => {
106+ const key = `${ trigger . method } _${ trigger . path } ` . toLowerCase ( ) . replace ( / \/ / g, '_' ) ;
107+ const api = new agw . RosApi (
108+ this ,
109+ `${ event . key } _api_${ key } ` ,
110+ {
111+ apiName : `${ event . name } _api_${ key } ` ,
112+ groupId : apiGatewayGroup . attrGroupId ,
113+ visibility : 'PRIVATE' ,
114+ requestConfig : {
115+ requestProtocol : 'HTTP' ,
116+ requestHttpMethod : trigger . method ,
117+ requestPath : trigger . path ,
118+ requestMode : 'PASSTHROUGH' ,
119+ } ,
120+ serviceConfig : {
121+ serviceProtocol : 'FunctionCompute' ,
122+ functionComputeConfig : {
123+ fcRegionId : context . region ,
124+ serviceName : service . serviceName ,
125+ functionName : trigger . backend ,
126+ roleArn : gatewayAccessRole . attrArn ,
127+ } ,
128+ } ,
129+ resultSample : 'ServerlessInsight resultSample' ,
130+ resultType : 'JSON' ,
131+ } ,
132+ true ,
133+ ) ;
134+ api . addDependsOn ( apiGatewayGroup ) ;
135+ } ) ;
136+ } ) ;
137+ }
48138 }
49139}
50140
51- const generateStackTemplate = ( stackName : string , iac : ServerlessIac ) => {
141+ const generateStackTemplate = ( stackName : string , iac : ServerlessIac , context : ActionContext ) => {
52142 const app = new ros . App ( ) ;
53- new IacStack ( app , iac ) ;
143+ new IacStack ( app , iac , context ) ;
54144
55145 const assembly = app . synth ( ) ;
56146 const stackArtifact = assembly . getStackByName ( stackName ) ;
@@ -69,7 +159,7 @@ export const deployStack = async (
69159) => {
70160 printer . info ( `Deploying stack... ${ JSON . stringify ( iac ) } ` ) ;
71161
72- const { template, parameters } = generateStackTemplate ( stackName , iac ) ;
162+ const { template, parameters } = generateStackTemplate ( stackName , iac , context ) ;
73163 console . log ( 'Generated ROS YAML:' , JSON . stringify ( { template, parameters } ) ) ;
74164 await rosStackDeploy ( stackName , template , { ...context , parameters } ) ;
75165 printer . info ( `Stack deployed! 🎉` ) ;
0 commit comments