Skip to content

Commit

Permalink
feat: enable sls logs for lambda invoke
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Dec 21, 2024
1 parent a523eef commit 5b73144
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 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';
16 changes: 13 additions & 3 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 @@ -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),
Expand All @@ -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`,
});
});
});
}
Expand Down
40 changes: 39 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,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<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 +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`);
}
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

0 comments on commit 5b73144

Please sign in to comment.