question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws-cloudformation) Deploy a CDK stack in a Lambda

See original GitHub issue

My use case:

  • I have a pipeline stack that is defined in Typescript
  • I would like this stack to be deployed dynamically via a Lambda with parameters that will be resolved during the Lambda execution

All the documentation examples that I found show how to deploy a CDK stack via a CLI cdk synth command. What I need to achieve is exactly the same but in a Lambda, Typescript, doesn’t really matter, so:

I found this but it doesn’t show how to handle the template synthesis part.

I would appreciate some info if this is the correct way to go or that there is maybe any more generic out-of-the-box way to do it.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
mikestopcontinuescommented, Aug 4, 2021

I think this is worth giving more consideration to. There are cases where pipelines is just a bad fit.

For instance, I run a multi-tenant app where a new stack has to be deployed when a user account is created, updated when key details change, and destroyed when the account is closed. This is in addition to pushing out bulk updates across accounts. To do this in pipelines would be incredibly inefficient, as it would have to check all user accounts for changes.

I’m not saying CDK should be redesigned lambda-first, but I think it’s worth an official example of how to handle this situation. The examples out there are broken and incomplete. I’ve started down the path of making this work, and it’s the wild west. The CDK CLI needs to be bundled in a layer. The CDK entry app needs to be bundled separately (probably with a custom cdk.json. Then the lambda func needs deploy privileges. And that’s just the issues I see from the get-go.

If anything, I think the real key to this situation (and many others) is programmatic usage of CDK (filed #15851), but in the interim, there’s got to be a clear way through the lambda issue.

2reactions
fab-mindflowcommented, Dec 22, 2021

We are deploying a simple CDK app from lambda. We’ve successfully tried 2 options so far:

  • Spawning a CDK process: cumbersome and very slow
  • Using aws-cdk classes: simple and amazingly fast (especially with hotswap when that’s possible depending on use case)

Here is the snippet:

  const stackArtifact = app.synth().getStackByName(stack.stackName)
  const chain = new CredentialProviderChain(CredentialProviderChain.defaultProviders)
  const sdkProvider = new SdkProvider(chain, process.env.TARGET_REGION)
  const cloudFormation = new CloudFormationDeployments({ sdkProvider })
  await cloudFormation.deployStack({ stack: stackArtifact, /* hotswap: true */ })

Hope this will become more or less the Programmatic API for CDK.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating a serverless application using the AWS CDK
Create an AWS CDK app · Create a Lambda function to list all widgets · Create a widget service · Add the service...
Read more >
Your first AWS CDK app - AWS Documentation
Synthesize one or more stacks in the app to create an AWS CloudFormation template. Deploy one or more stacks to your AWS account....
Read more >
aws-cdk/aws-lambda module - AWS Documentation
When deploying a stack that contains this code, the directory will be zip archived and then uploaded to an S3 bucket, then the...
Read more >
Deploy multiple-stack applications using AWS CDK with ...
This pattern provides a step-by-step approach for application deployment on Amazon Web Services (AWS) using AWS Cloud Development Kit (AWS CDK) with TypeScript....
Read more >
Stacks - AWS Cloud Development Kit (AWS CDK) v2
The unit of deployment in the AWS CDK is called a stack . All AWS resources defined within the scope of a stack,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found