node.js - Await causing aws Lambda to timeout - Stack Overflow

I have some async code in my AWS Lambda function and no matter what async code is running it never reso

I have some async code in my AWS Lambda function and no matter what async code is running it never resolves. It just causes the Lambda to timeout.

export const handler: Handler = async (event) => {
   console.log("stops after this");
   const test = new Promise(() => setTimeout(() => void 1000));

   await test;

   // Never runs 
   console.log("test");
}

Here is my the code in my stack for the lambda. It is linked to API gateway:

const api = new cdk.aws_apigatewayv2.HttpApi(this, "devpad-api");

        const projectLambda = new lambda.Function(this, "ProjectLambda", {
            runtime: lambda.Runtime.NODEJS_20_X,
            handler: "index.handler",
            code: lambda.Code.fromAsset("./lambdas/project/out/"),
            timeout: cdk.Duration.seconds(10),
            environment: {
                MONGODB_CONNECTION_STRING: process.env.MONGODB_CONNECTION_STRING || "",
                CLERK_SECRET_KEY: process.env.CLERK_SECRET_KEY || "",
            },
        });

Why is this happening?

I have some async code in my AWS Lambda function and no matter what async code is running it never resolves. It just causes the Lambda to timeout.

export const handler: Handler = async (event) => {
   console.log("stops after this");
   const test = new Promise(() => setTimeout(() => void 1000));

   await test;

   // Never runs 
   console.log("test");
}

Here is my the code in my stack for the lambda. It is linked to API gateway:

const api = new cdk.aws_apigatewayv2.HttpApi(this, "devpad-api");

        const projectLambda = new lambda.Function(this, "ProjectLambda", {
            runtime: lambda.Runtime.NODEJS_20_X,
            handler: "index.handler",
            code: lambda.Code.fromAsset("./lambdas/project/out/"),
            timeout: cdk.Duration.seconds(10),
            environment: {
                MONGODB_CONNECTION_STRING: process.env.MONGODB_CONNECTION_STRING || "",
                CLERK_SECRET_KEY: process.env.CLERK_SECRET_KEY || "",
            },
        });

Why is this happening?

Share Improve this question asked Mar 3 at 1:15 EthanEthan 1,7542 gold badges12 silver badges44 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The issue in your Lambda function is with the Promise creation and resolution. The current Promise never resolves because the setTimeout is not properly handled.

Your CDK stack setup looks correct, but you might want to consider, 1 - increase memory ( if needed ) 2 - Add property logging configuration 3 - Configure correct API integration

export const handler: Handler = async (event) => {
   console.log("starts here");
   
   // Method 1: Using a properly constructed Promise
   const test = new Promise((resolve) => {
       setTimeout(() => {
           resolve(true);
       }, 1000);
   });

   // OR Method 2: Using util.promisify
   // const { promisify } = require('util');
   // const sleep = promisify(setTimeout);
   // await sleep(1000);

   await test;
   console.log("test");

   return {
       statusCode: 200,
       body: JSON.stringify({ message: "Success" })
   };
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745113133a4611964.html

相关推荐

  • node.js - Await causing aws Lambda to timeout - Stack Overflow

    I have some async code in my AWS Lambda function and no matter what async code is running it never reso

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信