Run npx aws-cdk deploy --all to deploy every stack declared by your CDK app. Use explicit stack IDs when you want a smaller scope, and review each stack's diff before deployment.
Step 1: Install AWS CDK
The first step is to install the AWS CDK. You can do this by running the following command:
npm install -g aws-cdk
Step 2: Create a new CDK project
Once you have the AWS CDK installed, you can create a new project by running the following command:
cdk init --language [language]
Where [language] is the programming language you want to use for your project. For example, if you want to use TypeScript, you would run:
cdk init --language typescript
Step 3: Define your stacks
Once you have your project set up, you can define your stacks. A stack is a collection of AWS resources that are created and managed together. You can define your stacks in the lib/stacks.ts file. For example, the following defines a MyS3Stack and MySNSStack.
import * as cdk from "aws-cdk-lib"; import * as s3 from "aws-cdk-lib/aws-s3"; export class MyS3Stack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, "MyBucket", { blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL, }); } }
import * as cdk from "aws-cdk-lib"; import * as sns from "aws-cdk-lib/aws-sns"; export class MySNSStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); new sns.Topic(this, "MyTopic", { topicName: "my-topic", }); } }
Instantiate the stacks in your app
Class declarations alone do not add stacks to an app. Import both classes from lib/stacks.ts, then replace the generated app entry point with:
import { App } from "aws-cdk-lib"; import { MyS3Stack, MySNSStack } from "../lib/stacks"; const app = new App(); new MyS3Stack(app, "MyS3Stack"); new MySNSStack(app, "MySNSStack");
Step 4: Deploy your stacks
Once you have defined your stacks, you can deploy them by running the following command to deploy a single stack:
cdk deploy [stack-name]
Where [stack-name] is the name of the stack you want to deploy. For example, if you want to deploy the stack defined in the previous step, you would run the:
cdk deploy MyS3Stack
Or
cdk deploy MySNSStack
You can deploy multiple stacks at once by specifying multiple stack names.
cdk deploy MyS3Stack MySNSStack
Or
cdk deploy --all
Or
cdk deploy "*"
Conclusion
AWS CDK is a powerful tool that allows you to define your infrastructure as code. By following the steps outlined in this article, you can deploy all stacks in AWS CDK. This makes it easy to manage and automate your AWS resources, saving you time and reducing the risk of errors.
Please note that in this article I am providing you the basic and general steps to deploy all the stacks in AWS CDK, the implementation and details may vary based on your specific use case.
Before deploying, run cdk list and cdk diff. Stack dependencies determine ordering; --all is not an account-wide CloudFormation deployment. Review cleanup per stack: a default-retained bucket can remain after stack deletion.
Quick Recap
How do you deploy a single stack in AWS CDK?
Run cdk deploy followed by the stack's name, e.g. cdk deploy MyS3Stack.
How do you deploy multiple specific stacks at once?
List each stack name after cdk deploy, e.g. cdk deploy MyS3Stack MySNSStack, and CDK deploys all of them.
How do you deploy every stack in a CDK app at once?
Run cdk deploy --all, or alternatively cdk deploy "*", to deploy all stacks defined in the app.
Comments
Share your thoughts and questions below