AWS CDK Toolkit CLI
The `aws-cdk` package provides the command-line interface (CLI) for the AWS Cloud Development Kit (CDK), a framework enabling developers to define cloud infrastructure using familiar programming languages (TypeScript, JavaScript, Python, Java, C#, Go) and provision it through AWS CloudFormation. As of version 2.1118.4, the CLI operates on CDK applications to synthesize CloudFormation templates, deploy, diff, and manage AWS resources. It maintains a rapid release cadence, often seeing multiple updates daily, with continuous bug fixes and feature enhancements across various underlying CDK modules. A key differentiator is its ability to abstract CloudFormation complexities, offering higher-level constructs and supporting advanced development workflows like `cdk watch` for rapid iteration and `cdk migrate` for importing existing resources. The CLI and the Construct Library (packaged as `aws-cdk-lib`) have independent release cadences since February 2025, though they remain compatible, with the CLI generally supporting Construct Library versions released before or with it.
Common errors
-
Error: This CDK application is not supported in the current Node.js version.
cause The installed Node.js version is below the minimum requirement for AWS CDK v2.fixUpgrade your Node.js environment to version 18 or higher. Use `nvm install 18 && nvm use 18` or your preferred Node.js version manager. -
No stacks found in the app. Make sure you are in a CDK app directory.
cause The `cdk` command was executed outside a CDK project root, or the `cdk.json` file is missing/misconfigured.fixNavigate to the root directory of your CDK project (where `cdk.json` is located) or verify that `cdk.json` correctly points to your application entry point. -
The 'default' credential provider chain failed to retrieve credentials. Regions or sources not set.
cause AWS authentication credentials are not configured in your environment or the default region is not set.fixConfigure your AWS CLI with valid credentials (`aws configure`) and ensure the `AWS_REGION` environment variable or `--region` flag is specified. -
CloudFormation deployments failed: User: arn:aws:iam::ACCOUNT-ID:user/username is not authorized to perform:...
cause The IAM user or role performing the `cdk deploy` command lacks the necessary permissions to create, update, or delete the specified AWS resources.fixGrant the required IAM permissions to the deploying entity. Start with policies like `AdministratorAccess` for testing, then refine to least privilege as you understand the resource requirements. -
Stack 'CDKToolkit' is not bootstrapped in account 'ACCOUNT-ID' and region 'REGION'.
cause The required CDK Toolkit CloudFormation stack has not been deployed to the target AWS account and region.fixRun `cdk bootstrap aws://ACCOUNT-ID/REGION` once per account and region to set up the necessary resources for CDK deployments.
Warnings
- breaking AWS CDK v1 reached end-of-support on June 1, 2023. Continuing to use v1 exposes projects to security vulnerabilities and unaddressed bugs, and may result in incompatibility with new AWS services and features.
- breaking The AWS CDK CLI requires Node.js v18 or newer. Support for Node.js 14.x and 16.x officially ended on May 30, 2025. Using unsupported Node.js versions may lead to build failures, unexpected behavior, and lack of support.
- gotcha The AWS account and region must be 'bootstrapped' with a CDK Toolkit stack before the first deployment using `cdk deploy` in that environment. This stack manages assets and deployment permissions.
- gotcha The `aws-cdk` CLI and `aws-cdk-lib` (Construct Library) now have independent release cadences, meaning their version numbers may diverge. While the CLI generally supports older library versions, always ensure your CLI is up-to-date.
- gotcha CDK deployments require specific IAM permissions for the deploying identity to interact with CloudFormation and provision resources. Default permissions, especially after bootstrapping, might not be sufficient for all resource types.
Install
-
npm install aws-cdk -
yarn add aws-cdk -
pnpm add aws-cdk
Imports
- App
import { App } from 'aws-cdk';import { App } from 'aws-cdk-lib'; - Stack
import { Stack } from '@aws-cdk/core';import { Stack } from 'aws-cdk-lib'; - Bucket
import { Bucket } from '@aws-cdk/aws-s3';import * as s3 from 'aws-cdk-lib/aws-s3'; // OR import { Bucket } from 'aws-cdk-lib/aws-s3';
Quickstart
import { App, Stack } from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
const app = new App();
const myStack = new Stack(app, 'MySimpleCdkStack', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
},
});
new s3.Bucket(myStack, 'MyBucket', {
versioned: true,
// Ensure bucket name is unique
bucketName: `my-unique-cdk-bucket-${myStack.account?.toLowerCase()}-${myStack.region?.toLowerCase()}`
});
new NodejsFunction(myStack, 'MyLambdaFunction', {
runtime: lambda.Runtime.NODEJS_18_X,
entry: 'src/handler.ts',
handler: 'handler',
environment: {
MESSAGE: 'Hello from CDK Lambda!',
},
});
// Dummy handler file for NodejsFunction (src/handler.ts)
// export async function handler(event: any) {
// console.log('Received event:', JSON.stringify(event, null, 2));
// return { statusCode: 200, body: process.env.MESSAGE || 'No message' };
// }