aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.05k stars 573 forks source link

Slow import time for clients #5382

Closed mk0sojo closed 11 months ago

mk0sojo commented 11 months ago

Checkboxes for prior research

Describe the bug

Currently upgrading from AWS SDK v2 to AWS SDK v3 and importing @aws-sdk/client-xxx is slower than I would expect and is causing very slow cold starts (close to 1 second).

SDK version number

@aws-sdk/client-dynamodb@latest, @aws-sdk/client-s3, @aws-sdk/client-ssm

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

Node 18 lambda

Reproduction Steps

  1. In the AWS console create a new Node 18 lambda. Set the memory to 1024 MB
  2. It is pre populated with the following code:
    export const handler = async (event) => {
    // TODO implement
    const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
    };
  3. Test run the code, this returns something like this:
    
    Response
    {
    "statusCode": 200,
    "body": "\"Hello from Lambda!\""
    }

Function Logs START RequestId: 12248941-44db-4e8c-93fa-458c0080ad45 Version: $LATEST END RequestId: 12248941-44db-4e8c-93fa-458c0080ad45 REPORT RequestId: 12248941-44db-4e8c-93fa-458c0080ad45 Duration: 2.66 ms Billed Duration: 3 ms Memory Size: 1024 MB Max Memory Used: 67 MB Init Duration: 172.79 ms

Init duration (cold start) here is 172.79 ms which is expected.

4. Now add a few imports to the top of the file so it looks like this:

import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb" import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; import { SSMClient, GetParameterCommand } from "@aws-sdk/client-ssm";

export const handler = async (event) => { // TODO implement const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response; };

5. Deploy and test. This will return something like this:

Response { "statusCode": 200, "body": "\"Hello from Lambda!\"" }

Function Logs START RequestId: fa753048-9567-4836-b9e7-278df1a9767f Version: $LATEST END RequestId: fa753048-9567-4836-b9e7-278df1a9767f REPORT RequestId: fa753048-9567-4836-b9e7-278df1a9767f Duration: 2.56 ms Billed Duration: 3 ms Memory Size: 1024 MB Max Memory Used: 115 MB Init Duration: 917.74 ms

Request ID fa753048-9567-4836-b9e7-278df1a9767f


The cold start time is now 917.74 ms. This seems quite large (and this is before creating any clients (and definately more than AWS SDK v2). Is this expected?

### Observed Behavior

Slow cold start time when importing AWS SDK clients.

### Expected Behavior

A bit faster?

### Possible Solution

_No response_

### Additional Information/Context

_No response_
kuhe commented 11 months ago

We recommend creating a bundled application if cold start times are a concern.

https://aws.amazon.com/blogs/developer/reduce-lambda-cold-start-times-migrate-to-aws-sdk-for-javascript-v3/

mk0sojo commented 11 months ago

When I bundle it it takes around 450 ms for a cold start which is similar to v2 I think.

yenfryherrerafeliz commented 11 months ago

@mk0sojo I am glad to hear that bundling the SDK helped you in reducing the cold start times. It is actually what we recommend as @kuhe said. I am going to resolve this issue now, but please if you have anything else that we can help you with, feel free of opening a new issue.

Thanks!

github-actions[bot] commented 10 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.