Description
Checklist
- I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
- I have looked into the API documentation and have not found a suitable solution or answer.
- I have searched the issues and have not found a suitable solution or answer.
- I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- I agree to the terms within the Auth0 Code of Conduct.
Description
Hello.
When using node-auth0 v4.3.1 and v3.7.2 in Lambda, there's a 1000ms difference in processing time during Lambda's cold start.
My Lambda function is developed using Node.js v20 + TypeScript + SAM. I've also added node-auth0 to a layer and imported it into my Lambda function.
Here are the Lambda function and layer codes for both v4.3.1 and v3.7.2.
Although it's simple code using node-auth0 and X-Ray (for measuring processing time), when executing the Lambda function, the processing time for the v4.3.1 function is 1000ms slower. Why is this?
I've managed to achieve the same processing time as v3.7.2 by increasing the Lambda's memory, but this incurs additional costs. If possible, I'd like to reduce the cost while maintaining the same processing time as v3.7.2.
Thanks.
v.4.3.1
lambda code
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { AWSXRay } from '/opt/nodejs/lib/x-ray';
import { getUsersByEmail } from '/opt/nodejs/lib/auth0';
export const lambdaHandler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
const segment = AWSXRay.getSegment();
if (!segment) {
const error = new Error('segment is undefined');
console.error(error);
throw error;
}
const getUserByEmailSegment = segment.addNewSubsegment('getUserByEmail');
let users;
try {
const email = JSON.parse(event.body!).email;
if (!email) {
const error = new Error('email is not found');
console.error(error);
throw error;
}
users = await getUsersByEmail(email);
} catch (error: any) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
} finally {
getUserByEmailSegment.close();
}
segment.close();
return {
statusCode: 200,
body: JSON.stringify(users),
}
};
layer code
import { ManagementClient } from 'auth0';
const managementClient = new ManagementClient({
domain: process.env.AUTH0_DOMAIN!,
clientId: process.env.AUTH0_CLIENT_ID!,
clientSecret: process.env.AUTH0_CLIENT_SECRET!,
});
export const getUsersByEmail = async (email: string) => {
try {
const result = await managementClient.usersByEmail.getByEmail({
email: email,
});
return result.data;
} catch (error: any) {
throw error;
}
};
v3.7.2
lambda code
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { AWSXRay } from '/opt/nodejs/lib/x-ray';
import { getUsersByEmail } from '/opt/nodejs/lib/auth0';
export const lambdaHandler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
const segment = AWSXRay.getSegment();
if (!segment) {
const error = new Error('segment is undefined');
console.error(error);
throw error;
}
const getUserByEmailSegment = segment.addNewSubsegment('getUserByEmail');
let users;
try {
const email = JSON.parse(event.body!).email;
if (!email) {
const error = new Error('email is not found');
console.error(error);
throw error;
}
users = await getUsersByEmail(email);
} catch (error: any) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
} finally {
getUserByEmailSegment.close();
}
segment.close();
return {
statusCode: 200,
body: JSON.stringify(users),
}
};
layer code
import { ManagementClient } from 'auth0';
const managementClient = new ManagementClient({
domain: process.env.AUTH0_DOMAIN!,
clientId: process.env.AUTH0_CLIENT_ID!,
clientSecret: process.env.AUTH0_CLIENT_SECRET!,
});
export const getUsersByEmail = async (email: string) => {
try {
const result = await managementClient.getUsersByEmail(email);
return result;
} catch (error: any) {
throw error;
}
};
Reproduction
- Create Lambda code and layer code using v4.3.1 with SAM
- Create Lambda code and layer code using v3.7.2 with SAM
- Deploy both versions
- Send requests via API Gateway
- The API response time for v4.3.1 is 1000ms slower than v3.7.2
Additional context
Processing time for v4.3.1 as measured by X-Ray
The getUserByEmail process takes 2040ms

Processing time for v3.7.2 as measured by X-Ray
The getUserByEmail process takes 1010ms

node-auth0 version
4.3.1 and 3.7.2
Node.js version
nodejs20.x(lambda)