Skip to content

The open alternative to Salesforce, designed for AI.

Cache Lambda GetFunction results per logic function (#28647)

Charles Bochetea29de2a3556
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
index e03f52dcf21..3bd494dfe23 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
@@ -201,9 +201,15 @@ export class LambdaDriver implements LogicFunctionDriver {
const lambdaClient = await this.awsClient.getLambdaClient();
const invokeStart = Date.now();
- const result = await lambdaClient.send(command, {
- abortSignal: AbortSignal.timeout(timeoutMs),
- });
+ const result = await lambdaClient
+ .send(command, { abortSignal: AbortSignal.timeout(timeoutMs) })
+ .catch(async (error) => {
+ await this.executorManager.clearLambdaExecutorCache(
+ flatLogicFunction,
+ );
+
+ throw error;
+ });
const invokeDurationMs = Date.now() - invokeStart;
const parsedResult = result.Payload
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/constants/lambda-driver.constant.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/constants/lambda-driver.constant.ts
index 93ae0c0e9b1..401c477e765 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/constants/lambda-driver.constant.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/constants/lambda-driver.constant.ts
@@ -30,6 +30,8 @@ export const SDK_LAYER_PREFIX_IN_ZIP = 'nodejs/node_modules/twenty-client-sdk';
export const LAMBDA_PREBUILT_BUNDLE_CHECKSUM_TAG = 'twenty:bundle-checksum';
export const PREBUILT_BUNDLE_FILE_NAME = 'prebuilt-logic-function.mjs';
+export const LAMBDA_FUNCTION_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
+
export const BUILD_LOCK_TTL_MS = 120_000;
export const BUILD_LOCK_RETRY_MS = 1_000;
export const BUILD_LOCK_MAX_RETRIES = 480;
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-aws-client.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-aws-client.service.ts
index 9dd4b1c1037..23153725131 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-aws-client.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-aws-client.service.ts
@@ -1,6 +1,9 @@
import {
+ GetFunctionCommand,
+ type GetFunctionCommandOutput,
Lambda,
ListLayerVersionsCommand,
+ ResourceNotFoundException,
waitUntilFunctionActiveV2,
waitUntilFunctionUpdatedV2,
} from '@aws-sdk/client-lambda';
@@ -11,6 +14,7 @@ import { isDefined } from 'twenty-shared/utils';
import {
CREDENTIALS_DURATION_IN_SECONDS,
+ LAMBDA_FUNCTION_CACHE_TTL_MS,
LAMBDA_CLIENT_CONNECTION_TIMEOUT_MS,
LAMBDA_CLIENT_MAX_ATTEMPTS,
LAMBDA_CLIENT_MAX_SOCKETS,
@@ -114,6 +118,56 @@ export class LambdaAwsClientService {
);
}
+ async getFunction(
+ functionName: string,
+ ): Promise<GetFunctionCommandOutput | undefined> {
+ const cacheKey = this.getFunctionCacheKey(functionName);
+ const cachedFunction =
+ await this.options.cacheStorageService.get<GetFunctionCommandOutput>(
+ cacheKey,
+ );
+
+ if (isDefined(cachedFunction)) {
+ return cachedFunction;
+ }
+
+ const lambdaClient = await this.getLambdaClient();
+
+ let getFunctionResult: GetFunctionCommandOutput;
+
+ try {
+ getFunctionResult = await lambdaClient.send(
+ new GetFunctionCommand({ FunctionName: functionName }),
+ );
+ } catch (error) {
+ if (error instanceof ResourceNotFoundException) {
+ return undefined;
+ }
+
+ throw error;
+ }
+
+ if (getFunctionResult.Configuration?.State === 'Active') {
+ await this.options.cacheStorageService.set(
+ cacheKey,
+ getFunctionResult,
+ LAMBDA_FUNCTION_CACHE_TTL_MS,
+ );
+ }
+
+ return getFunctionResult;
+ }
+
+ async clearFunctionCache(functionName: string): Promise<void> {
+ await this.options.cacheStorageService.del(
+ this.getFunctionCacheKey(functionName),
+ );
+ }
+
+ private getFunctionCacheKey(functionName: string): string {
+ return `lambda-function:${functionName}`;
+ }
+
async getExistingLayerArn(layerName: string): Promise<string | undefined> {
const lambdaClient = await this.getLambdaClient();
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts
index 57d1c760a6d..f67f78ab370 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts
@@ -5,7 +5,6 @@ import {
CreateFunctionCommand,
type CreateFunctionCommandInput,
DeleteFunctionCommand,
- GetFunctionCommand,
type GetFunctionCommandOutput,
ResourceNotFoundException,
TagResourceCommand,
@@ -70,33 +69,29 @@ export class LambdaExecutorManagerService {
async getLambdaExecutor(
flatLogicFunction: FlatLogicFunction,
): Promise<GetFunctionCommandOutput | undefined> {
+ return this.awsClient.getFunction(flatLogicFunction.id);
+ }
+
+ async clearLambdaExecutorCache(
+ flatLogicFunction: FlatLogicFunction,
+ ): Promise<void> {
+ await this.awsClient.clearFunctionCache(flatLogicFunction.id);
+ }
+
+ async delete(flatLogicFunction: FlatLogicFunction): Promise<void> {
const lambdaClient = await this.awsClient.getLambdaClient();
try {
- return await lambdaClient.send(
- new GetFunctionCommand({ FunctionName: flatLogicFunction.id }),
+ await lambdaClient.send(
+ new DeleteFunctionCommand({ FunctionName: flatLogicFunction.id }),
);
} catch (error) {
- if (error instanceof ResourceNotFoundException) {
- return undefined;
+ if (!(error instanceof ResourceNotFoundException)) {
+ throw error;
}
-
- throw error;
- }
- }
-
- async delete(flatLogicFunction: FlatLogicFunction): Promise<void> {
- const lambdaExecutor = await this.getLambdaExecutor(flatLogicFunction);
-
- if (!isDefined(lambdaExecutor)) {
- return;
}
- const lambdaClient = await this.awsClient.getLambdaClient();
-
- await lambdaClient.send(
- new DeleteFunctionCommand({ FunctionName: flatLogicFunction.id }),
- );
+ await this.clearLambdaExecutorCache(flatLogicFunction);
}
async buildExecutor(context: ExecutorBuildContext): Promise<void> {
@@ -125,6 +120,9 @@ export class LambdaExecutorManagerService {
}
await this.ensureExecutor({ ...refreshedContext, lambdaExecutor });
+ await this.clearLambdaExecutorCache(
+ refreshedContext.flatLogicFunction,
+ );
},
lockKey,
{
@@ -247,6 +245,8 @@ export class LambdaExecutorManagerService {
},
}),
);
+
+ await this.clearLambdaExecutorCache(flatLogicFunction);
} catch (error) {
this.logger.error(
`Failed to install prebuilt bundle for function ${flatLogicFunction.id}: ${error instanceof Error ? error.message : String(error)}`,
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts
index c7286e6da4f..1a802e4fb74 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts
@@ -4,11 +4,9 @@ import { join } from 'path';
import {
CreateFunctionCommand,
type CreateFunctionCommandInput,
- GetFunctionCommand,
InvokeCommand,
LogType,
PublishLayerVersionCommand,
- ResourceNotFoundException,
} from '@aws-sdk/client-lambda';
import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
@@ -73,16 +71,24 @@ export class LambdaToolFunctionsService {
...params,
};
- const result = await lambdaClient.send(
- new InvokeCommand({
- FunctionName: builderFunctionName,
- Payload: JSON.stringify(payload),
- LogType: LogType.Tail,
- }),
- {
- abortSignal: AbortSignal.timeout(BUILDER_LAMBDA_TIMEOUT_SECONDS * 1000),
- },
- );
+ const result = await lambdaClient
+ .send(
+ new InvokeCommand({
+ FunctionName: builderFunctionName,
+ Payload: JSON.stringify(payload),
+ LogType: LogType.Tail,
+ }),
+ {
+ abortSignal: AbortSignal.timeout(
+ BUILDER_LAMBDA_TIMEOUT_SECONDS * 1000,
+ ),
+ },
+ )
+ .catch(async (error) => {
+ await this.awsClient.clearFunctionCache(builderFunctionName);
+
+ throw error;
+ });
if (result.FunctionError) {
const parsedResult = result.Payload
@@ -133,18 +139,24 @@ export class LambdaToolFunctionsService {
...params,
};
- const result = await lambdaClient.send(
- new InvokeCommand({
- FunctionName: yarnInstallFunctionName,
- Payload: JSON.stringify(payload),
- LogType: LogType.Tail,
- }),
- {
- abortSignal: AbortSignal.timeout(
- YARN_INSTALL_LAMBDA_TIMEOUT_SECONDS * 1000,
- ),
- },
- );
+ const result = await lambdaClient
+ .send(
+ new InvokeCommand({
+ FunctionName: yarnInstallFunctionName,
+ Payload: JSON.stringify(payload),
+ LogType: LogType.Tail,
+ }),
+ {
+ abortSignal: AbortSignal.timeout(
+ YARN_INSTALL_LAMBDA_TIMEOUT_SECONDS * 1000,
+ ),
+ },
+ )
+ .catch(async (error) => {
+ await this.awsClient.clearFunctionCache(yarnInstallFunctionName);
+
+ throw error;
+ });
if (result.FunctionError) {
const parsedResult = result.Payload
@@ -212,16 +224,8 @@ export class LambdaToolFunctionsService {
const yarnInstallFunctionName = await this.getYarnInstallFunctionName();
const lambdaClient = await this.awsClient.getLambdaClient();
- try {
- await lambdaClient.send(
- new GetFunctionCommand({ FunctionName: yarnInstallFunctionName }),
- );
-
+ if (isDefined(await this.awsClient.getFunction(yarnInstallFunctionName))) {
return;
- } catch (error) {
- if (!(error instanceof ResourceNotFoundException)) {
- throw error;
- }
}
const commonLayerArn = await this.ensureCommonLayerExists();
@@ -261,16 +265,8 @@ export class LambdaToolFunctionsService {
const builderFunctionName = await this.getBuilderFunctionName();
const lambdaClient = await this.awsClient.getLambdaClient();
- try {
- await lambdaClient.send(
- new GetFunctionCommand({ FunctionName: builderFunctionName }),
- );
-
+ if (isDefined(await this.awsClient.getFunction(builderFunctionName))) {
return;
- } catch (error) {
- if (!(error instanceof ResourceNotFoundException)) {
- throw error;
- }
}
const commonLayerArn = await this.ensureCommonLayerExists();
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts
index 4a2536d1237..abc3bfb1ef3 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts
@@ -2,6 +2,7 @@ import { type LambdaClientConfig } from '@aws-sdk/client-lambda';
import { type LogicFunctionExecutionContext } from 'twenty-shared/logic-function';
import { type CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
+import { type CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
import { type LogicFunctionResourceService } from 'src/engine/core-modules/logic-function/logic-function-resource/logic-function-resource.service';
import { type SdkClientArchiveService } from 'src/engine/core-modules/sdk-client/sdk-client-archive.service';
import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
@@ -40,6 +41,7 @@ export interface LambdaDriverOptions extends LambdaClientConfig {
logicFunctionResourceService: LogicFunctionResourceService;
sdkClientArchiveService: SdkClientArchiveService;
cacheLockService: CacheLockService;
+ cacheStorageService: CacheStorageService;
workspaceCacheService: WorkspaceCacheService;
region: string;
lambdaRole: string;
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts
index 7bd07dfb307..cee72e8c686 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts
@@ -8,6 +8,9 @@ import {
} from 'src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface';
import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
+import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';
+import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
+import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';
import { DisabledDriver } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver';
import { LambdaDriver } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver';
import { LocalDriver } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver';
@@ -29,6 +32,8 @@ export class LogicFunctionDriverFactory extends DriverFactoryBase<LogicFunctionD
private readonly sdkClientArchiveService: SdkClientArchiveService,
private readonly cacheLockService: CacheLockService,
private readonly workspaceCacheService: WorkspaceCacheService,
+ @InjectCacheStorage(CacheStorageNamespace.ModuleApplications)
+ private readonly cacheStorageService: CacheStorageService,
) {
super(twentyConfigService, configGroupHashService);
}
@@ -90,6 +95,7 @@ export class LogicFunctionDriverFactory extends DriverFactoryBase<LogicFunctionD
return new LambdaDriver({
logicFunctionResourceService: this.logicFunctionResourceService,
cacheLockService: this.cacheLockService,
+ cacheStorageService: this.cacheStorageService,
workspaceCacheService: this.workspaceCacheService,
credentials: accessKeyId
? { accessKeyId, secretAccessKey }