Skip to content
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

(cli): hotswap results in many call to ListStackResources of cloudformation API (with ThrottlingException) #19021

Closed
JPLemelin opened this issue Feb 17, 2022 · 9 comments · Fixed by #19081
Assignees
Labels
bug This issue is a bug. p2 package/tools Related to AWS CDK Tools or CLI

Comments

@JPLemelin
Copy link

What is the problem?

We are using the hot-swap with lambda code change (cdk deploy --hotswap my-stack-with-lambda-functions --exclusively) .
Currently the stack have 68 lambdas, who are all using the same code asset, but with different entry point.

The problem we are facing is, the process take almost the exact same time of cloudformation deploy, i.e around 5-6mins 🥲

Reproduction Steps

Have a stack with many lambda, in my case 68 lambda

What did you expect to happen?

Only one call to ListStackResources of cloudformation API (or more if paged)

What actually happened?

Foreach lambdas, a ListStackResources of cloudformation API are done and some hit the ThrottlingException

CDK CLI Version

2.12.0

Framework Version

No response

Node.js Version

14

OS

MacOs

Language

Typescript

Language Version

No response

Other information

Hi!

We are using the hot-swap with lambda code change (cdk deploy --hotswap my-stack-with-lambda-functions --exclusively) .
Currently the stack have 68 lambdas, who are all using the same code asset, but with different entry point.

The problem we are facing is, the process take almost the exact same time of cloudformation deploy, i.e around 5-6mins 🥲

After some investigation we found the cdk do some (many many many 🙃) api call to cloudforamtion on ListStackResources, who take around 5mins.

On the 876 call to ListStackResources, 604 end with ThrottlingException: Rate exceeded

On first few second there are 68 call (same number of lambda) to ListStackResources who don’t have nextToken parameter, I think it’s the first call to ListStackResources, other call have a paging parameters (nextToken

)

So most of the hot-swap execution time is spent doing ListStackResources

Our cloudformation stack have 359 ressources

When we use the cli, aws cloudformation list-stack-resources --stack-name my-stack-with-lambda-functions , we did not hit any paging

I also add some screen shot from cloudtrail of CDK call during the hot-swap

image
image
image

@JPLemelin JPLemelin added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 17, 2022
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Feb 17, 2022
@JPLemelin JPLemelin changed the title hotswap: Many call to ListStackResources of cloudformation API hotswap: Many call to ListStackResources of cloudformation API (with ThrottlingException) Feb 17, 2022
@kaizencc kaizencc changed the title hotswap: Many call to ListStackResources of cloudformation API (with ThrottlingException) (cli): hotswap results in many call to ListStackResources of cloudformation API (with ThrottlingException) Feb 17, 2022
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Feb 17, 2022
@kaizencc kaizencc added package/tools Related to AWS CDK Tools or CLI and removed package/tools Related to AWS CDK Tools or CLI @aws-cdk/aws-lambda Related to AWS Lambda labels Feb 17, 2022
@kaizencc kaizencc assigned skinny85 and unassigned rix0rrr and kaizencc Feb 17, 2022
@skinny85
Copy link
Contributor

skinny85 commented Feb 18, 2022

Hey @JPLemelin,

thanks for opening the issue. Is there any chance you can show us some of your CDK and runtime Lambda code? It will make it much easier to reproduce the issue locally.

Thanks,
Adam

@NGL321 NGL321 added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 21, 2022
@github-actions
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Feb 21, 2022
@JPLemelin
Copy link
Author

@skinny85, I added a repo here https://github.com/JPLemelin/aws-cdk-issue-19021

In the repo I also add a cloud-trail export https://github.com/JPLemelin/aws-cdk-issue-19021/blob/master/doc/cloudtrail_event_history.json

Let's me know if you need more Information!

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Feb 21, 2022
@skinny85
Copy link
Contributor

Thanks, that's super helpful! I'll be diving into this today.

@skinny85
Copy link
Contributor

@JPLemelin actually, I ran into a problem 😛.

Did you forget to include some code in your example app? I see this code in your CDK app: https://github.com/JPLemelin/aws-cdk-issue-19021/blob/6f115b8a97ce407eae2c7d35b614c63bd8fdf69f/lib/aws-cdk-issue-19021-stack.ts#L13, but the code does not contain a src directory...?

cdk synth fails with:

$ /Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/.bin/cdk synth

/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:109
      throw new Error(`Cannot find asset at ${this.sourcePath}`);
            ^
Error: Cannot find asset at /Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/src
    at new AssetStaging (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:109:13)
    at new Asset (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.ts:72:21)
    at AssetCode.bind (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/aws-cdk-lib/aws-lambda/lib/code.ts:180:20)
    at new Function (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/aws-cdk-lib/aws-lambda/lib/function.ts:350:29)
    at new AwsCdkIssue19021Stack (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/lib/aws-cdk-issue-19021-stack.ts:16:7)
    at Object.<anonymous> (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/bin/aws-cdk-issue-19021.ts:8:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/adamruka/workplace/cloud-development-kit/oncall/hotswap-list-resources-spam/node_modules/ts-node/src/index.ts:1059:12)
Subprocess exited with error 1
error Command failed with exit code 1.

skinny85 added a commit to skinny85/aws-cdk that referenced this issue Feb 21, 2022
In the `LazyListStackResources` class,
we were incorrectly caching the results of the ListStackResources CloudFormation API call.
This resulted in executing redundant calls,
and very visibly impacted the performance of hotswapping when the Stack included many resources.

In manual testing, this change brought down deployment time of a large Stack from 80 to 7 seconds.

Closes aws#19021
@skinny85
Copy link
Contributor

Nvm, figured it out, PR has been opened with a fix 🙂.

@JPLemelin
Copy link
Author

My bad, the src folder was missing due to my ignore file who was ignoring all *.js 😑

@skinny85
Copy link
Contributor

No worries 🙂.

BTW - your setup with having to pass a -c envName =dev to execute any CDK command is not really idiomatic, and makes using this app harder than it has to be.

The idiomatic way is just to have 2 Stacks:

new AwsCdkIssue19021Stack(app, 'AwsCdkIssue19021StackDev', {
  env: {
    region: 'ca-central-1',
    account: 'abc',
  },
});

new AwsCdkIssue19021Stack(app, 'AwsCdkIssue19021StackProd', {
  env: {
    region: 'ca-central-1',
    account: 'xyz',
  },
});

More details: https://www.endoflineblog.com/cdk-tips-05-have-a-stack-instance-per-deployed-stack.

@mergify mergify bot closed this as completed in #19081 Feb 24, 2022
mergify bot pushed a commit that referenced this issue Feb 24, 2022
…9081)

In the `LazyListStackResources` class,
we were incorrectly caching the results of the ListStackResources CloudFormation API call.
This resulted in executing redundant calls,
and very visibly impacted the performance of hotswapping when the Stack included many resources.

In manual testing, this change brought down deployment time of a large Stack from 80 to 7 seconds.

Closes #19021

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants