@@ -14,26 +14,39 @@ const resolveCode = (location: string): string => {
1414 return fileContent . toString ( 'base64' ) ;
1515} ;
1616
17- const replaceVars = < T > ( value : T ) : T => {
17+ const replaceVars = < T > ( value : T , stage : string ) : T => {
1818 if ( typeof value === 'string' ) {
1919 const matchVar = value . match ( / ^ \$ \{ v a r s \. ( \w + ) } $ / ) ;
2020 const containsVar = value . match ( / \$ \{ v a r s \. ( \w + ) } / ) ;
21+ const matchMap = value . match ( / ^ \$ \{ s t a g e s \. ( \w + ) } $ / ) ;
22+ const containsMap = value . match ( / \$ \{ s t a g e s \. ( \w + ) } / ) ;
2123 if ( matchVar ?. length ) {
2224 return ros . Fn . ref ( matchVar [ 1 ] ) as T ;
2325 }
26+ if ( matchMap ?. length ) {
27+ return ros . Fn . findInMap ( 'stages' , '' , matchMap [ 1 ] ) as T ;
28+ }
29+ if ( containsMap ?. length && containsVar ?. length ) {
30+ return ros . Fn . sub (
31+ value . replace ( / \$ \{ s t a g e s \. ( \w + ) } / g, '${$1}' ) . replace ( / \$ \{ v a r s \. ( \w + ) } / g, '${$1}' ) ,
32+ ) as T ;
33+ }
2434 if ( containsVar ?. length ) {
2535 return ros . Fn . sub ( value . replace ( / \$ \{ v a r s \. ( \w + ) } / g, '${$1}' ) ) as T ;
2636 }
37+ if ( containsMap ?. length ) {
38+ return ros . Fn . sub ( value . replace ( / \$ \{ s t a g e s \. ( \w + ) } / g, '${$1}' ) ) as T ;
39+ }
2740 return value ;
2841 }
2942
3043 if ( Array . isArray ( value ) ) {
31- return value . map ( replaceVars ) as T ;
44+ return value . map ( ( item ) => replaceVars ( item , stage ) ) as T ;
3245 }
3346
3447 if ( typeof value === 'object' && value !== null ) {
3548 return Object . fromEntries (
36- Object . entries ( value ) . map ( ( [ key , val ] ) => [ key , replaceVars ( val ) ] ) ,
49+ Object . entries ( value ) . map ( ( [ key , val ] ) => [ key , replaceVars ( val , stage ) ] ) ,
3750 ) as T ;
3851 }
3952
@@ -44,27 +57,35 @@ export class IacStack extends ros.Stack {
4457 constructor ( scope : ros . Construct , iac : ServerlessIac , context : ActionContext ) {
4558 super ( scope , iac . service , {
4659 tags : iac . tags . reduce ( ( acc : { [ key : string ] : string } , tag ) => {
47- acc [ tag . key ] = replaceVars ( tag . value ) ;
60+ acc [ tag . key ] = replaceVars ( tag . value , context . stage ) ;
4861 return acc ;
4962 } , { } ) ,
5063 } ) ;
5164
65+ // Define Parameters
5266 Object . entries ( iac . vars ) . map (
5367 ( [ key , value ] ) =>
5468 new ros . RosParameter ( this , key , {
5569 type : RosParameterType . STRING ,
5670 defaultValue : value ,
5771 } ) ,
5872 ) ;
73+ console . log ( 'stages:' , iac . stages ) ;
74+ // Define Mappings
75+ new ros . RosMapping ( this , 'stages' , { mapping : replaceVars ( iac . stages , context . stage ) } ) ;
5976
60- new ros . RosInfo ( this , ros . RosInfo . description , replaceVars ( `${ iac . service } stack` ) ) ;
77+ new ros . RosInfo (
78+ this ,
79+ ros . RosInfo . description ,
80+ replaceVars ( `${ iac . service } stack` , context . stage ) ,
81+ ) ;
6182
6283 const service = new fc . RosService (
6384 this ,
64- replaceVars ( `${ iac . service } -service` ) ,
85+ replaceVars ( `${ iac . service } -service` , context . stage ) ,
6586 {
66- serviceName : replaceVars ( `${ iac . service } -service` ) ,
67- tags : replaceVars ( iac . tags ) ,
87+ serviceName : replaceVars ( `${ iac . service } -service` , context . stage ) ,
88+ tags : replaceVars ( iac . tags , context . stage ) ,
6889 } ,
6990 true ,
7091 ) ;
@@ -74,13 +95,13 @@ export class IacStack extends ros.Stack {
7495 this ,
7596 fnc . key ,
7697 {
77- functionName : replaceVars ( fnc . name ) ,
78- serviceName : replaceVars ( service . serviceName ) ,
79- handler : replaceVars ( fnc . handler ) ,
80- runtime : replaceVars ( fnc . runtime ) ,
81- memorySize : replaceVars ( fnc . memory ) ,
82- timeout : replaceVars ( fnc . timeout ) ,
83- environmentVariables : replaceVars ( fnc . environment ) ,
98+ functionName : replaceVars ( fnc . name , context . stage ) ,
99+ serviceName : replaceVars ( service . serviceName , context . stage ) ,
100+ handler : replaceVars ( fnc . handler , context . stage ) ,
101+ runtime : replaceVars ( fnc . runtime , context . stage ) ,
102+ memorySize : replaceVars ( fnc . memory , context . stage ) ,
103+ timeout : replaceVars ( fnc . timeout , context . stage ) ,
104+ environmentVariables : replaceVars ( fnc . environment , context . stage ) ,
84105 code : {
85106 zipFile : resolveCode ( fnc . code ) ,
86107 } ,
@@ -94,10 +115,10 @@ export class IacStack extends ros.Stack {
94115 if ( apiGateway ) {
95116 const gatewayAccessRole = new ram . RosRole (
96117 this ,
97- replaceVars ( `${ iac . service } _role` ) ,
118+ replaceVars ( `${ iac . service } _role` , context . stage ) ,
98119 {
99- roleName : replaceVars ( `${ iac . service } -gateway-access-role` ) ,
100- description : replaceVars ( `${ iac . service } role` ) ,
120+ roleName : replaceVars ( `${ iac . service } -gateway-access-role` , context . stage ) ,
121+ description : replaceVars ( `${ iac . service } role` , context . stage ) ,
101122 assumeRolePolicyDocument : {
102123 version : '1' ,
103124 statement : [
@@ -112,7 +133,7 @@ export class IacStack extends ros.Stack {
112133 } ,
113134 policies : [
114135 {
115- policyName : replaceVars ( `${ iac . service } -policy` ) ,
136+ policyName : replaceVars ( `${ iac . service } -policy` , context . stage ) ,
116137 policyDocument : {
117138 version : '1' ,
118139 statement : [
@@ -132,10 +153,10 @@ export class IacStack extends ros.Stack {
132153
133154 const apiGatewayGroup = new agw . RosGroup (
134155 this ,
135- replaceVars ( `${ iac . service } _apigroup` ) ,
156+ replaceVars ( `${ iac . service } _apigroup` , context . stage ) ,
136157 {
137- groupName : replaceVars ( `${ iac . service } _apigroup` ) ,
138- tags : replaceVars ( iac . tags ) ,
158+ groupName : replaceVars ( `${ iac . service } _apigroup` , context . stage ) ,
159+ tags : replaceVars ( iac . tags , context . stage ) ,
139160 } ,
140161 true ,
141162 ) ;
@@ -148,15 +169,15 @@ export class IacStack extends ros.Stack {
148169
149170 const api = new agw . RosApi (
150171 this ,
151- replaceVars ( `${ event . key } _api_${ key } ` ) ,
172+ replaceVars ( `${ event . key } _api_${ key } ` , context . stage ) ,
152173 {
153- apiName : replaceVars ( `${ event . name } _api_${ key } ` ) ,
174+ apiName : replaceVars ( `${ event . name } _api_${ key } ` , context . stage ) ,
154175 groupId : apiGatewayGroup . attrGroupId ,
155176 visibility : 'PRIVATE' ,
156177 requestConfig : {
157178 requestProtocol : 'HTTP' ,
158- requestHttpMethod : replaceVars ( trigger . method ) ,
159- requestPath : replaceVars ( trigger . path ) ,
179+ requestHttpMethod : replaceVars ( trigger . method , context . stage ) ,
180+ requestPath : replaceVars ( trigger . path , context . stage ) ,
160181 requestMode : 'PASSTHROUGH' ,
161182 } ,
162183 serviceConfig : {
@@ -170,7 +191,7 @@ export class IacStack extends ros.Stack {
170191 } ,
171192 resultSample : 'ServerlessInsight resultSample' ,
172193 resultType : 'JSON' ,
173- tags : replaceVars ( iac . tags ) ,
194+ tags : replaceVars ( iac . tags , context . stage ) ,
174195 } ,
175196 true ,
176197 ) ;
0 commit comments