Skip to main content
This hands‑on guide demonstrates how to use the AWS CDK (Cloud Development Kit) with Python to create an S3 bucket. Follow the sequence below: install prerequisites, initialize a Python CDK project, add an S3 bucket construct, synthesize the CloudFormation template, bootstrap (if required), deploy the stack, verify the resources, and clean up.

Prerequisites

  • Node.js (includes npm) — required for the CDK CLI.
  • AWS CLI installed and configured with credentials and a default region.
Verify AWS connectivity (optional):

Install the CDK CLI

Install the CDK CLI globally:
Verify installation:

Important CDK CLI commands

Initialize a new Python CDK project

Create a starter project:
This generates the standard Python CDK layout:
  • Virtual environment placeholder (we’ll create it next)
  • source.bat (Windows helper)
  • requirements.txt (Python dependencies)
  • README (usage and test instructions)
  • cdk.json (CDK config)
  • app.py (CDK app entrypoint)
  • A module folder with a generated Stack class

Create and activate the Python virtual environment

macOS / Linux:
Windows (PowerShell):
Note: The generated source.bat replicates source .venv/bin/activate behaviour on Windows by calling .venv\Scripts\activate.bat. Example source.bat (for reference):

Install Python dependencies

After activating the virtual environment, install dependencies:
A typical requirements.txt for CDK v2:

Project entrypoint: app.py

The generated app.py bootstraps the CDK app and instantiates the stack:

Generated sample stack (example)

The sample stack demonstrates how CDK resources are defined (the generated template often includes SQS/SNS examples). For this tutorial you can comment those out and add an S3 bucket instead:
For this lesson we will not use the SQS/SNS resources — comment them out and add the S3 bucket as shown below.

Create an IAM user for deployment

CDK uses the same AWS credentials mechanism as the AWS CLI. Create an IAM user with programmatic access and generate an access key/secret key. Prefer least privilege rather than Administrator access.
Best practice: avoid using AdministratorAccess for deployment agents. Create a policy that grants only the permissions required by your CDK app.
When creating the IAM user you will see screens like this:
A screenshot of the AWS Management Console's IAM "Create user" page showing a form to enter a username and optional console access. The left sidebar shows setup steps and there's a highlighted "Next" button at the bottom right.
Attach the required policy (or AdministratorAccess for quick demos):
A screenshot of the AWS IAM console showing the "Permissions policies" list while creating a user, with options to add or attach policies directly and a table of AWS-managed policy names (e.g., AdministratorAccess). The page includes search/filter controls and pagination for browsing available policies.
When creating an access key, you’ll see best-practice guidance:
Screenshot of the AWS IAM "Create access key" page showing "Access key best practices & alternatives" with selectable use-case options (CLI, local code, AWS compute, third-party, etc.). The bottom-right has "Cancel" and an orange "Next" button.
Configure the keys locally:

Add an S3 bucket to the stack

Open your stack file (for example cdk/cdk_stack.py) and:
  1. Add aws_s3 to the import block:
  1. Comment out sample SQS/SNS code (if present) and add a simple S3 bucket:
Notes:
  • The logical ID "MyKodeKloudCdkBucket" is used by CDK when synthesizing resources. CDK generates a unique physical bucket name by default for global uniqueness.
  • To use a fixed bucket name, set the bucket_name property (ensure global uniqueness and S3 naming rules).
  • Configure encryption, versioning, lifecycle rules, or removal_policy via constructor arguments as needed.

Synthesize the CloudFormation template

Generate the CloudFormation template:
This prints the CloudFormation YAML/JSON that CDK will deploy. Example snippet from cdk synth (trimmed):

Bootstrap the environment (if required)

If you haven’t bootstrapped this account/region for CDK v2, cdk deploy will ask you to run:
Bootstrapping deploys supporting resources (an S3 staging bucket, roles, etc.). If bootstrap fails because of resource conflicts (e.g., an S3 bucket with the same name exists), resolve the conflict and re-run cdk bootstrap.

Preview and deploy

Preview changes:
If the diff matches your intent, deploy:
During deploy you’ll see CloudFormation progress. Example trimmed output:

Verify in the console

After deployment, check the S3 console. The created bucket appears in the Buckets list (region shown):
A screenshot of the Amazon S3 web console showing a list of S3 buckets with their names, AWS regions, access settings, and creation dates, plus the left navigation panel and a "Create bucket" button. Five buckets are listed, mostly in the US East (N. Virginia) region.
The CDK deploys your construct via CloudFormation. You can inspect the stack in the CloudFormation console to see resources and status:
Screenshot of the AWS CloudFormation console showing stacks on the left and the Resources tab for a stack named "CdkStack" on the right. It lists two resources — My-kodekloud-cdk-bucket and CDKMetadata — both with status CREATE_COMPLETE.

Post-deploy checks

Run cdk diff again to confirm no outstanding changes:

Destroying the stack

When finished, remove the stack:
You will be prompted to confirm deletion.
If the S3 bucket contains objects, CloudFormation cannot delete the bucket and cdk destroy will fail. Either empty the bucket first (console, CLI, or SDK), or configure the bucket for development/testing with removal_policy=cdk.RemovalPolicy.DESTROY and auto_delete_objects=True to enable automated deletion. Do not enable auto_delete_objects in production without understanding the consequences.
After destroy completes (or after emptying and re-running destroy), verify the CloudFormation stack and S3 bucket are removed in the console.

Conclusion

You learned how to:
  • Initialize a Python CDK project
  • Create and activate a virtual environment
  • Add an S3 bucket construct to the stack
  • Synthesize the CloudFormation template
  • Bootstrap the environment (if required)
  • Deploy and verify the bucket
  • Destroy resources when finished
The CDK lets you define infrastructure as code, synthesize a CloudFormation template, and deploy predictable infrastructure. Explore additional CDK constructs and best practices as you build more sophisticated stacks.

Watch Video