Skip to main content
Welcome — this guide walks through creating a minimal AWS CDK application in Python that provisions an S3 bucket. Follow the steps in order: prepare your environment, scaffold a CDK app, edit the stack to add an S3 bucket, bootstrap, synthesize, and deploy. Links and commands are included so you can reproduce the demo.

Prerequisites

Ensure the following are installed and configured before starting: First, install and configure the AWS CLI so your local environment can authenticate to AWS.
A dark-themed browser screenshot of the AWS documentation page titled "AWS CLI install instructions," showing a left navigation menu and expandable sections for Linux, macOS, and Windows. The top bar shows browser tabs and a "Return to the Console" button.

Node.js & CDK CLI

Install Node.js (or use nvm to manage versions). Example Unix-like shell commands:
Install the CDK CLI globally:

Python CDK libraries

For CDK v2, install the consolidated libraries into your project environment (virtualenv recommended):
Note: CDK v2 packages service L2 constructs inside aws-cdk-lib (older tutorials that reference aws-cdk.aws-s3 are outdated).

AWS credentials and IAM

Create an IAM user with programmatic access and attach the required policies. For experimentation in a controlled account, AdministratorAccess is convenient; for production follow least-privilege practices. Configure the AWS CLI with the user’s credentials:
Example (values masked):
Protect your access keys. Do not commit credentials to source control. Use IAM roles, secrets managers, or temporary credentials for production workflows.
On Windows use cls to clear the prompt; on macOS/Linux use clear.

Create the CDK project

Open a terminal in your workspace (for example, ~/Desktop) and scaffold a Python CDK app:
cdk init scaffolds a Python project with files like app.py, cdk.json, a package directory that contains the generated stack file, and requirements.txt. You should now see the generated files in your editor.
A dark-themed Visual Studio Code window showing the Explorer sidebar with a project folder named "cdk-demo" and files like app.py, cdk.json, README.md and requirements-dev.txt. The main editor area is empty, displaying a faint VS Code logo.

Edit the stack to add an S3 bucket

Open the generated stack file inside the project package directory (the directory named after your project). Modify the stack to import the S3 construct and create a bucket. A minimal CDK v2 Python stack example:
Key points:
  • s3.Bucket(self, "ConstructId") defines a bucket construct inside the stack. The construct ID (here "My-KodeKloud-bkt-cld2") uniquely identifies the resource in the CDK app.
  • The construct ID is an internal identifier; the physical S3 bucket name is generated by CDK unless you pass bucket_name="your-bucket-name".
  • Ensure the stack __init__ signature includes **kwargs and calls super().__init__(...) to accept standard CDK options.

Bootstrap the environment

Before deploying, bootstrap the target AWS environment so CDK can create required deployment resources (assets bucket, execution role, etc.):
Example output (shortened):
Bootstrapping requires the AWS credentials you configured and sufficient IAM permissions. If bootstrapping fails, verify your IAM user’s permissions and the configured AWS region.

Inspect the synthesized CloudFormation template

To review the CloudFormation template CDK will generate from your Python code:
This prints the synthesized template (YAML/JSON). Use this to confirm resources, properties, and logical IDs before deployment.

Deploy the stack

Deploy your stack to the configured account and region:
CDK synthesizes the app and calls CloudFormation to create/update the stack. Example success summary:

Verify resources in the AWS Console

  • Open the AWS Management Console and navigate to CloudFormation — confirm the deployed stack appears in Stacks.
  • In the same region, open the S3 console to view the created bucket.
A screenshot of the AWS CloudFormation "Stacks" console showing a list of stacks. It lists stacks like "CdkDemoStack" and "CDKToolkit" with statuses such as UPDATE_COMPLETE and CREATE_COMPLETE, and toolbar buttons (Delete stack, Update stack, Create stack).

Useful CDK commands

Use these commands during development and cleanup:
To remove deployed resources when finished:
Wrapping up: you now have a minimal Python CDK workflow to provision an S3 bucket — scaffold, edit the stack, bootstrap, synth, deploy, and verify. Use cdk diff to preview changes and cdk destroy to clean up.

Watch Video