|
| 1 | +import * as ros from '@alicloud/ros-cdk-core'; |
| 2 | +import { ActionContext, EventTypes, ServerlessIac } from '../types'; |
| 3 | +import { RosParameterType } from '@alicloud/ros-cdk-core'; |
| 4 | +import * as fc from '@alicloud/ros-cdk-fc'; |
| 5 | +import * as ram from '@alicloud/ros-cdk-ram'; |
| 6 | +import * as agw from '@alicloud/ros-cdk-apigateway'; |
| 7 | +import path from 'node:path'; |
| 8 | +import fs from 'node:fs'; |
| 9 | + |
| 10 | +const resolveCode = (location: string): string => { |
| 11 | + const filePath = path.resolve(process.cwd(), location); |
| 12 | + const fileContent = fs.readFileSync(filePath); |
| 13 | + |
| 14 | + return fileContent.toString('base64'); |
| 15 | +}; |
| 16 | + |
| 17 | +const replaceVars = ( |
| 18 | + value: { [key: string]: unknown } | string | number | unknown, |
| 19 | +): { [key: string]: unknown } | string | number | unknown => { |
| 20 | + if (typeof value === 'string') { |
| 21 | + const matchVar = value.match(/^\$\{vars\.(\w+)}$/); |
| 22 | + const containsVar = value.match(/\$\{vars\.(\w+)}/); |
| 23 | + if (matchVar?.length) { |
| 24 | + return ros.Fn.ref(matchVar[1]); |
| 25 | + } |
| 26 | + if (containsVar?.length) { |
| 27 | + return ros.Fn.sub(value.replace(/\$\{vars\.(\w+)}/g, '${$1}')); |
| 28 | + } |
| 29 | + return value; |
| 30 | + } |
| 31 | + |
| 32 | + if (Array.isArray(value)) { |
| 33 | + return value.map(replaceVars); |
| 34 | + } |
| 35 | + |
| 36 | + if (typeof value === 'object' && value !== null) { |
| 37 | + return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, replaceVars(val)])); |
| 38 | + } |
| 39 | + |
| 40 | + return value; |
| 41 | +}; |
| 42 | + |
| 43 | +export class IacStack extends ros.Stack { |
| 44 | + private readonly iac: ServerlessIac; |
| 45 | + |
| 46 | + constructor(scope: ros.Construct, iac: ServerlessIac, context: ActionContext) { |
| 47 | + super(scope, iac.service, { |
| 48 | + tags: iac.tags.reduce((acc: { [key: string]: string }, tag) => { |
| 49 | + acc[tag.key] = tag.value; |
| 50 | + return acc; |
| 51 | + }, {}), |
| 52 | + }); |
| 53 | + |
| 54 | + this.iac = replaceVars(iac) as ServerlessIac; |
| 55 | + console.log('IAC:', JSON.stringify(this.iac)); |
| 56 | + |
| 57 | + Object.entries(this.iac.vars).map( |
| 58 | + ([key, value]) => |
| 59 | + new ros.RosParameter(this, key, { |
| 60 | + type: RosParameterType.STRING, |
| 61 | + defaultValue: value, |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + new ros.RosInfo(this, ros.RosInfo.description, `${this.iac.service} stack`); |
| 66 | + |
| 67 | + const service = new fc.RosService( |
| 68 | + this, |
| 69 | + `${this.iac.service}-service`, |
| 70 | + { |
| 71 | + serviceName: `${this.iac.service}-service`, |
| 72 | + tags: this.iac.tags, |
| 73 | + }, |
| 74 | + true, |
| 75 | + ); |
| 76 | + |
| 77 | + this.iac.functions.forEach((fnc) => { |
| 78 | + const func = new fc.RosFunction( |
| 79 | + this, |
| 80 | + fnc.key, |
| 81 | + { |
| 82 | + functionName: fnc.name, |
| 83 | + serviceName: service.serviceName, |
| 84 | + handler: fnc.handler, |
| 85 | + runtime: fnc.runtime, |
| 86 | + memorySize: fnc.memory, |
| 87 | + timeout: fnc.timeout, |
| 88 | + environmentVariables: fnc.environment, |
| 89 | + code: { |
| 90 | + zipFile: resolveCode(fnc.code), |
| 91 | + }, |
| 92 | + }, |
| 93 | + true, |
| 94 | + ); |
| 95 | + func.addDependsOn(service); |
| 96 | + }); |
| 97 | + |
| 98 | + const apiGateway = this.iac.events.find((event) => event.type === EventTypes.API_GATEWAY); |
| 99 | + if (apiGateway) { |
| 100 | + const gatewayAccessRole = new ram.RosRole( |
| 101 | + this, |
| 102 | + `${this.iac.service}_role`, |
| 103 | + { |
| 104 | + roleName: `${this.iac.service}-gateway-access-role`, |
| 105 | + description: `${this.iac.service} role`, |
| 106 | + assumeRolePolicyDocument: { |
| 107 | + version: '1', |
| 108 | + statement: [ |
| 109 | + { |
| 110 | + action: 'sts:AssumeRole', |
| 111 | + effect: 'Allow', |
| 112 | + principal: { |
| 113 | + service: ['apigateway.aliyuncs.com'], |
| 114 | + }, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + policies: [ |
| 119 | + { |
| 120 | + policyName: `${iac.service}-policy`, |
| 121 | + policyDocument: { |
| 122 | + version: '1', |
| 123 | + statement: [ |
| 124 | + { |
| 125 | + action: ['fc:InvokeFunction'], |
| 126 | + effect: 'Allow', |
| 127 | + // @TODO implement at least permission granting |
| 128 | + resource: ['*'], |
| 129 | + }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + true, |
| 136 | + ); |
| 137 | + |
| 138 | + const apiGatewayGroup = new agw.RosGroup( |
| 139 | + this, |
| 140 | + `${this.iac.service}_apigroup`, |
| 141 | + { |
| 142 | + groupName: `${this.iac.service}_apigroup`, |
| 143 | + tags: this.iac.tags, |
| 144 | + }, |
| 145 | + true, |
| 146 | + ); |
| 147 | + |
| 148 | + this.iac.events |
| 149 | + .filter((event) => event.type === EventTypes.API_GATEWAY) |
| 150 | + .forEach((event) => { |
| 151 | + event.triggers.forEach((trigger) => { |
| 152 | + const key = `${trigger.method}_${trigger.path}`.toLowerCase().replace(/\//g, '_'); |
| 153 | + const api = new agw.RosApi( |
| 154 | + this, |
| 155 | + `${event.key}_api_${key}`, |
| 156 | + { |
| 157 | + apiName: `${event.name}_api_${key}`, |
| 158 | + groupId: apiGatewayGroup.attrGroupId, |
| 159 | + visibility: 'PRIVATE', |
| 160 | + requestConfig: { |
| 161 | + requestProtocol: 'HTTP', |
| 162 | + requestHttpMethod: trigger.method, |
| 163 | + requestPath: trigger.path, |
| 164 | + requestMode: 'PASSTHROUGH', |
| 165 | + }, |
| 166 | + serviceConfig: { |
| 167 | + serviceProtocol: 'FunctionCompute', |
| 168 | + functionComputeConfig: { |
| 169 | + fcRegionId: context.region, |
| 170 | + serviceName: service.serviceName, |
| 171 | + functionName: trigger.backend, |
| 172 | + roleArn: gatewayAccessRole.attrArn, |
| 173 | + }, |
| 174 | + }, |
| 175 | + resultSample: 'ServerlessInsight resultSample', |
| 176 | + resultType: 'JSON', |
| 177 | + tags: iac.tags, |
| 178 | + }, |
| 179 | + true, |
| 180 | + ); |
| 181 | + api.addDependsOn(apiGatewayGroup); |
| 182 | + }); |
| 183 | + }); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments