diff --git a/package-lock.json b/package-lock.json index 24fc71b..88d7309 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@alicloud/ros-cdk-oss": "^1.4.0", "@alicloud/ros-cdk-ossdeployment": "^1.4.0", "@alicloud/ros-cdk-ram": "^1.4.0", + "@alicloud/ros-cdk-sls": "^1.5.0", "@alicloud/ros20190910": "^3.5.2", "ajv": "^8.17.1", "ali-oss": "^6.22.0", @@ -2129,9 +2130,9 @@ } }, "node_modules/@alicloud/ros-cdk-sls": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-sls/-/ros-cdk-sls-1.4.0.tgz", - "integrity": "sha512-+9v07o2uLyOksRdZZF3FPa3fYO1XilwPl6YDDdwM8TYhdhhWGFn0RNfW2xAM5Hrwfmi9PoFyHbyIn+yMHtQiYg==", + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-sls/-/ros-cdk-sls-1.5.0.tgz", + "integrity": "sha512-JXOWM8j4LfNSJ+Q/var6C96RAx3xLKdqnwnXN+kxlmamye4e+YaQd+YLz3hL8wVmNDi0H4LfodZerxwbF7MzEQ==", "dependencies": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" @@ -10271,9 +10272,9 @@ } }, "@alicloud/ros-cdk-sls": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-sls/-/ros-cdk-sls-1.4.0.tgz", - "integrity": "sha512-+9v07o2uLyOksRdZZF3FPa3fYO1XilwPl6YDDdwM8TYhdhhWGFn0RNfW2xAM5Hrwfmi9PoFyHbyIn+yMHtQiYg==", + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@alicloud/ros-cdk-sls/-/ros-cdk-sls-1.5.0.tgz", + "integrity": "sha512-JXOWM8j4LfNSJ+Q/var6C96RAx3xLKdqnwnXN+kxlmamye4e+YaQd+YLz3hL8wVmNDi0H4LfodZerxwbF7MzEQ==", "requires": { "@alicloud/ros-cdk-core": "^1.0.27", "constructs": "^3.0.4" diff --git a/package.json b/package.json index 4f19998..dda131f 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "@alicloud/ros-cdk-oss": "^1.4.0", "@alicloud/ros-cdk-ossdeployment": "^1.4.0", "@alicloud/ros-cdk-ram": "^1.4.0", + "@alicloud/ros-cdk-sls": "^1.5.0", "@alicloud/ros20190910": "^3.5.2", "ajv": "^8.17.1", "ali-oss": "^6.22.0", diff --git a/src/common/base64.ts b/src/common/base64.ts new file mode 100644 index 0000000..828db54 --- /dev/null +++ b/src/common/base64.ts @@ -0,0 +1 @@ +export const encodeBase64 = (str: string) => Buffer.from(str, 'utf-8').toString('base64'); diff --git a/src/common/index.ts b/src/common/index.ts index 15bf26b..ee310da 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -6,3 +6,4 @@ export * from './actionContext'; export * from './iacHelper'; export * from './constants'; export * from './imsClient'; +export * from './base64'; diff --git a/src/stack/rosStack/event.ts b/src/stack/rosStack/event.ts index 68e44ce..092938f 100644 --- a/src/stack/rosStack/event.ts +++ b/src/stack/rosStack/event.ts @@ -1,7 +1,7 @@ import * as ros from '@alicloud/ros-cdk-core'; import { ActionContext, EventDomain, EventTypes, ServerlessIac } from '../../types'; import * as ram from '@alicloud/ros-cdk-ram'; -import { replaceReference } from '../../common'; +import { encodeBase64, replaceReference } from '../../common'; import * as agw from '@alicloud/ros-cdk-apigateway'; import { isEmpty } from 'lodash'; @@ -80,15 +80,18 @@ export const resolveEvents = ( apiGateway.forEach((event) => { event.triggers.forEach((trigger) => { - const key = `${trigger.method}_${trigger.path}`.toLowerCase().replace(/\//g, '_'); + const key = encodeBase64( + replaceReference(`${trigger.method}_${trigger.path}`, context), + ).replace(/=+$/, ''); const api = new agw.RosApi( scope, - replaceReference(`${event.key}_api_${key}`, context), + `${event.key}_api_${key}`, { apiName: replaceReference(`${event.name}_api_${key}`, context), groupId: apiGatewayGroup.attrGroupId, visibility: 'PRIVATE', + authType: 'ANONYMOUS', requestConfig: { requestProtocol: 'HTTP', requestHttpMethod: replaceReference(trigger.method, context), @@ -111,6 +114,13 @@ export const resolveEvents = ( true, ); api.addDependsOn(apiGatewayGroup); + + new agw.Deployment(scope, `${service}_deployment`, { + apiId: api.attrApiId, + groupId: apiGatewayGroup.attrGroupId, + stageName: 'RELEASE', + description: `${service} Api Gateway deployment`, + }); }); }); } diff --git a/src/stack/rosStack/function.ts b/src/stack/rosStack/function.ts index e93047e..cc3f195 100644 --- a/src/stack/rosStack/function.ts +++ b/src/stack/rosStack/function.ts @@ -1,4 +1,4 @@ -import { ActionContext, FunctionDomain } from '../../types'; +import { ActionContext, FunctionDomain, ServerlessIac } from '../../types'; import { CODE_ZIP_SIZE_LIMIT, getFileSource, @@ -10,16 +10,48 @@ import * as fc from '@alicloud/ros-cdk-fc3'; import { isEmpty } from 'lodash'; import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment'; import * as ros from '@alicloud/ros-cdk-core'; +import * as sls from '@alicloud/ros-cdk-sls'; export const resolveFunctions = ( scope: ros.Construct, functions: Array | undefined, + tags: ServerlessIac['tags'] | undefined, context: ActionContext, service: string, ) => { if (isEmpty(functions)) { return undefined; } + const slsService = new sls.Project( + scope, + `${service}_sls`, + { name: `${service}-sls`, tags: replaceReference(tags, context) }, + true, + ); + + const slsLogstore = new sls.Logstore( + scope, + `${service}_sls_logstore`, + { + logstoreName: `${service}-sls-logstore`, + projectName: slsService.attrName, + ttl: 7, + }, + true, + ); + new sls.Index( + scope, + `${service}_sls_index`, + { + projectName: slsService.attrName, + logstoreName: slsLogstore.attrLogstoreName, + fullTextIndex: { + enable: true, + }, + }, + true, + ); + const fileSources = functions ?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT) .map(({ code, name }) => { @@ -71,9 +103,15 @@ export const resolveFunctions = ( timeout: replaceReference(fnc.timeout, context), environmentVariables: replaceReference(fnc.environment, context), code, + logConfig: { + project: slsService.ref, + logstore: slsLogstore.attrLogstoreName, + enableRequestMetrics: true, + }, }, true, ); + fcn.addRosDependency(`${service}_sls`); if (storeInBucket) { fcn.addRosDependency(`${service}_artifacts_code_deployment`); } diff --git a/src/stack/rosStack/index.ts b/src/stack/rosStack/index.ts index ead9220..6fa8153 100644 --- a/src/stack/rosStack/index.ts +++ b/src/stack/rosStack/index.ts @@ -27,7 +27,7 @@ export class RosStack extends ros.Stack { // Define Mappings resolveStages(this, iac.stages, context); // Define functions - resolveFunctions(this, iac.functions, context, this.service); + resolveFunctions(this, iac.functions, iac.tags, context, this.service); // Define Events resolveEvents(this, iac.events, iac.tags, context, this.service); // Define Databases