diff --git a/src/parser/functionParser.ts b/src/parser/functionParser.ts index 0663c6d..bfeafeb 100644 --- a/src/parser/functionParser.ts +++ b/src/parser/functionParser.ts @@ -16,5 +16,6 @@ export const parseFunction = (functions?: { timeout: func.timeout, environment: func.environment, code: func.code, + log: func.log, })); }; diff --git a/src/stack/rosStack/function.ts b/src/stack/rosStack/function.ts index ad805c2..067c3f2 100644 --- a/src/stack/rosStack/function.ts +++ b/src/stack/rosStack/function.ts @@ -11,6 +11,7 @@ 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'; +import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated'; export const resolveFunctions = ( scope: ros.Construct, @@ -22,34 +23,44 @@ export const resolveFunctions = ( if (isEmpty(functions)) { return undefined; } - const slsService = new sls.Project( - scope, - `${service}_sls`, - { name: `${service}-sls`, tags: replaceReference(tags, context) }, - true, - ); + let logConfig: RosFunction.LogConfigProperty | undefined = undefined; - const slsLogstore = new sls.Logstore( - scope, - `${service}_sls_logstore`, - { - logstoreName: `${service}-sls-logstore`, - projectName: slsService.attrName, - ttl: 7, - }, - true, - ); + const enableLog = functions?.some(({ log }) => log); + if (enableLog) { + const slsService = new sls.Project( + scope, + `${service}_sls`, + { name: `${service}-sls`, tags: replaceReference(tags, context) }, + true, + ); - new sls.Index( - scope, - `${service}_sls_index`, - { - projectName: slsService.attrName, - logstoreName: slsLogstore.attrLogstoreName, - fullTextIndex: { enable: true }, - }, - 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, + ); + logConfig = { + project: slsLogstore.attrProjectName, + logstore: slsLogstore.attrLogstoreName, + enableRequestMetrics: true, + }; + } const fileSources = functions ?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT) @@ -102,17 +113,15 @@ export const resolveFunctions = ( timeout: replaceReference(fnc.timeout, context), environmentVariables: replaceReference(fnc.environment, context), code, - logConfig: { - project: slsLogstore.attrProjectName, - logstore: slsLogstore.attrLogstoreName, - enableRequestMetrics: true, - }, + logConfig, }, true, ); - fcn.addRosDependency(`${service}_sls`); - fcn.addRosDependency(`${service}_sls_logstore`); - fcn.addRosDependency(`${service}_sls_index`); + if (enableLog) { + fcn.addRosDependency(`${service}_sls`); + fcn.addRosDependency(`${service}_sls_logstore`); + fcn.addRosDependency(`${service}_sls_index`); + } if (storeInBucket) { fcn.addRosDependency(`${service}_artifacts_code_deployment`); diff --git a/src/types/domains/function.ts b/src/types/domains/function.ts index 33b741e..4b85eef 100644 --- a/src/types/domains/function.ts +++ b/src/types/domains/function.ts @@ -5,6 +5,7 @@ export type FunctionRaw = { code: string; memory: number; timeout: number; + log?: boolean; environment?: { [key: string]: string; }; diff --git a/src/validator/functionSchema.ts b/src/validator/functionSchema.ts index d4b0536..0fd976c 100644 --- a/src/validator/functionSchema.ts +++ b/src/validator/functionSchema.ts @@ -4,6 +4,7 @@ export const functionSchema = { patternProperties: { '.*': { type: 'object', + required: ['name', 'runtime', 'handler', 'code'], properties: { name: { type: 'string' }, runtime: { @@ -29,6 +30,7 @@ export const functionSchema = { code: { type: 'string' }, memory: { type: 'number' }, timeout: { type: 'number' }, + log: { type: 'boolean' }, environment: { type: 'object', additionalProperties: { diff --git a/tests/fixtures/deployFixture.ts b/tests/fixtures/deployFixture.ts index 09e012d..ea4cfdc 100644 --- a/tests/fixtures/deployFixture.ts +++ b/tests/fixtures/deployFixture.ts @@ -27,6 +27,7 @@ export const oneFcOneGatewayIac = { code: 'tests/fixtures/artifacts/artifact.zip', memory: 128, timeout: 10, + log: true, environment: { NODE_ENV: 'production', }, @@ -473,66 +474,16 @@ export const minimumRos = { ROSTemplateFormatVersion: '2015-09-01', Resources: { hello_fn: { - DependsOn: [ - 'my-demo-minimum-service_sls', - 'my-demo-minimum-service_sls_logstore', - 'my-demo-minimum-service_sls_index', - ], Properties: { Code: { ZipFile: 'resolved-code', }, FunctionName: 'hello_fn', Handler: 'index.handler', - LogConfig: { - EnableRequestMetrics: true, - Logstore: { - 'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'LogstoreName'], - }, - Project: { - 'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'ProjectName'], - }, - }, Runtime: 'nodejs18', }, Type: 'ALIYUN::FC3::Function', }, - 'my-demo-minimum-service_sls': { - Properties: { - Name: 'my-demo-minimum-service-sls', - }, - Type: 'ALIYUN::SLS::Project', - }, - 'my-demo-minimum-service_sls_index': { - Properties: { - FullTextIndex: { - Enable: true, - }, - LogReduce: false, - LogstoreName: { - 'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'LogstoreName'], - }, - ProjectName: { - 'Fn::GetAtt': ['my-demo-minimum-service_sls', 'Name'], - }, - }, - Type: 'ALIYUN::SLS::Index', - }, - 'my-demo-minimum-service_sls_logstore': { - Properties: { - AppendMeta: false, - AutoSplit: false, - EnableTracking: false, - LogstoreName: 'my-demo-minimum-service-sls-logstore', - PreserveStorage: false, - ProjectName: { - 'Fn::GetAtt': ['my-demo-minimum-service_sls', 'Name'], - }, - ShardCount: 2, - TTL: 7, - }, - Type: 'ALIYUN::SLS::Logstore', - }, }, }; @@ -561,6 +512,7 @@ export const oneFcIac = { code: 'tests/fixtures/artifacts/artifact.zip', memory: 128, timeout: 10, + log: true, environment: { NODE_ENV: 'production', }, @@ -697,6 +649,7 @@ export const oneFcIacWithStage = { code: 'tests/fixtures/artifacts/artifact.zip', memory: 128, timeout: 10, + log: true, environment: { NODE_ENV: '${stages.node_env}', }, diff --git a/tests/fixtures/serverless-insight.yml b/tests/fixtures/serverless-insight.yml index cc789b0..551df60 100644 --- a/tests/fixtures/serverless-insight.yml +++ b/tests/fixtures/serverless-insight.yml @@ -31,6 +31,7 @@ functions: code: tests/fixtures/artifacts/artifact.zip memory: 512 timeout: 10 + log: true environment: NODE_ENV: ${stages.node_env} TEST_VAR: ${vars.testv}