Skip to content

Commit

Permalink
feat: enable sls logs for lambda invoke (#25)
Browse files Browse the repository at this point in the history
feat: enable sls logs for lambda invoke

- enable sls logs for fc

Refs: #5

---------

Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll authored Dec 29, 2024
1 parent a523eef commit b25e3cf
Show file tree
Hide file tree
Showing 10 changed files with 718 additions and 64 deletions.
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/common/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const encodeBase64 = (str: string) => Buffer.from(str, 'utf-8').toString('base64');
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './actionContext';
export * from './iacHelper';
export * from './constants';
export * from './imsClient';
export * from './base64';
20 changes: 16 additions & 4 deletions src/stack/rosStack/event.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -61,6 +61,7 @@ export const resolveEvents = (
{
groupName: replaceReference(`${service}_apigroup`, context),
tags: replaceReference(tags, context),
passthroughHeaders: 'host',
},
true,
);
Expand All @@ -80,15 +81,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),
Expand All @@ -102,15 +106,23 @@ export const resolveEvents = (
functionName: replaceReference(trigger.backend, context),
roleArn: gatewayAccessRole.attrArn,
fcVersion: '3.0',
method: replaceReference(trigger.method, context),
},
},
resultSample: 'ServerlessInsight resultSample',
resultType: 'JSON',
resultType: 'PASSTHROUGH',
tags: replaceReference(tags, context),
},
true,
);
api.addDependsOn(apiGatewayGroup);

new agw.Deployment(scope, `${service}_deployment`, {
apiId: api.attrApiId,
groupId: apiGatewayGroup.attrGroupId,
stageName: 'RELEASE',
description: `${service} Api Gateway deployment`,
});
});
});
}
Expand Down
42 changes: 41 additions & 1 deletion src/stack/rosStack/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionContext, FunctionDomain } from '../../types';
import { ActionContext, FunctionDomain, ServerlessIac } from '../../types';
import {
CODE_ZIP_SIZE_LIMIT,
getFileSource,
Expand All @@ -10,16 +10,47 @@ 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<FunctionDomain> | 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 }) => {
Expand Down Expand Up @@ -71,9 +102,18 @@ export const resolveFunctions = (
timeout: replaceReference(fnc.timeout, context),
environmentVariables: replaceReference(fnc.environment, context),
code,
logConfig: {
project: slsLogstore.attrProjectName,
logstore: slsLogstore.attrLogstoreName,
enableRequestMetrics: true,
},
},
true,
);
fcn.addRosDependency(`${service}_sls`);
fcn.addRosDependency(`${service}_sls_logstore`);
fcn.addRosDependency(`${service}_sls_index`);

if (storeInBucket) {
fcn.addRosDependency(`${service}_artifacts_code_deployment`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/stack/rosStack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit b25e3cf

Please sign in to comment.