Skip to content

Maintenance: initialize AWS SDK client only when needed to improve performance in Parameters #1740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
marcioemiranda opened this issue Oct 12, 2023 · 5 comments · Fixed by #1757
Closed
1 of 2 tasks
Assignees
Labels
completed This item is complete and has been merged/shipped good-first-issue Something that is suitable for those who want to start contributing parameters This item relates to the Parameters Utility

Comments

@marcioemiranda
Copy link

Summary

Hello,
I am currently doing some performance tuning in our lambda functions and i notice that getting SSM parameters introduce some latency in the first execution. I've executed tests using both the getParameter utility and also SSMProvider. These were my findings:
ssm client instantiation: ~50ms
ssm provider instantiation: ~50ms
getParameter: 380ms (discounting 300ms from SDK, ~80ms)
SSM SDK get parameter: 300ms

In my tests I've used lambda functions configured with provisioned concurrency, so I tried to bring some of the latency to the INIT phase. I understand the best way to do it would be to:

  1. Instantiante the SSM client in the static scope, maybe using a capture to trace this call
  2. Passing the client to SSMProvider
  3. Using SSMProvider instead of getParameter utility, which would add the latency of instantiating a SSM client inside the handler code.

However, I haven't noticed any gain of doing that. By inspecting the code bellow, I've noticed that SSM client is instantiated even when it's not used, i.e., when one passes the client as parameter. I believe this section could be optimized so that SSM client is only instantiated if none is available:

/**
   * It initializes the SSMProvider class.
   *
   * @param {SSMProviderOptions} config - The configuration object.
   */
  public constructor(config?: SSMProviderOptions) {
    super();

    this.client = new SSMClient(config?.clientConfig || {});
    if (config?.awsSdkV3Client) {
      if (isSdkClient(config.awsSdkV3Client)) {
        this.client = config.awsSdkV3Client;
      } else {
        console.warn(
          'awsSdkV3Client is not an AWS SDK v3 client, using default client'
        );
      }
    }
    addUserAgentMiddleware(this.client, 'parameters');
  }

Why is this needed?

Improve performance of first execution when SSMProvider has a SSM client.

Which area does this relate to?

Parameters

Solution

Instantiate SSM client only when none is available

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@marcioemiranda marcioemiranda added triage This item has not been triaged by a maintainer, please wait internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) labels Oct 12, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Oct 12, 2023

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@dreamorosi
Copy link
Contributor

Hi @marcioemiranda, thank you for opening this issue.

You are right, the portion of the code that you highlighted could be refactored to create the client only when strictly needed. I'll put this item on the backlog and mark it as open for contributions.

On a side note, we are currently working on supporting ES Modules and will release support in the coming weeks. Once that's released you will be able to use top-level await and fetch the secrets outside of the Lambda handler as described in this post.

@dreamorosi dreamorosi added good-first-issue Something that is suitable for those who want to start contributing help-wanted We would really appreciate some support from community for this one parameters This item relates to the Parameters Utility confirmed The scope is clear, ready for implementation hacktoberfest and removed triage This item has not been triaged by a maintainer, please wait labels Oct 12, 2023
@dreamorosi dreamorosi removed the internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) label Oct 12, 2023
@dreamorosi dreamorosi changed the title Maintenance: Possible improvement in performance Maintenance: initialize AWS SDK client only when needed to improve performance in Parameters Oct 12, 2023
@marcioemiranda
Copy link
Author

@dreamorosi this is excelent news, will have big impact in performance

@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Oct 20, 2023
@dreamorosi dreamorosi self-assigned this Oct 20, 2023
@dreamorosi dreamorosi removed help-wanted We would really appreciate some support from community for this one hacktoberfest labels Oct 20, 2023
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Oct 20, 2023
@github-actions
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Oct 20, 2023
Copy link
Contributor

github-actions bot commented Nov 1, 2023

This is now released under v1.14.1 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Nov 1, 2023
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped good-first-issue Something that is suitable for those who want to start contributing parameters This item relates to the Parameters Utility
Projects
2 participants