> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Building With the CDK

> Guide showing how to create a minimal AWS CDK Python app that provisions an S3 bucket, with steps to scaffold, bootstrap, synthesize, deploy, and verify

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:

| Resource | Purpose | Example / Notes |
| - | - | - |
| AWS CLI | Communicate with AWS from your workstation | Install from the [AWS CLI docs](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) |
| Python | Run the CDK Python app and virtualenv | Use a supported runtime (3.8+) |
| Node.js & npm | Required by the CDK CLI | Use `nvm` to manage versions |
| CDK CLI | Synthesizes and deploys CDK apps | Installed globally with `npm` |
| Python CDK libraries | `aws-cdk-lib`, `constructs` for CDK v2 | Install into a virtualenv |
| AWS IAM user | Programmatic access (access key & secret) | Grant appropriate permissions for deployment |

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.](https://mintcdn.com/kodekloud-c4ac6d9a/5BuH6-_lHDMy3nuh/images/AWS-Certified-DevOps-Engineer-Professional/Domain-2-Configuration-Management-and-IaC/Demo-Building-With-the-CDK/aws-cli-install-docs-screenshot.jpg?fit=max\&auto=format\&n=5BuH6-_lHDMy3nuh\&q=85\&s=919138f5ef2cd731af9249491d2f01fb)

## Node.js & CDK CLI

Install Node.js (or use `nvm` to manage versions). Example Unix-like shell commands:

```bash theme={null}
# Install nvm (follow upstream instructions for the latest version)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

# Use nvm to install Node.js LTS
nvm install --lts
```

Install the CDK CLI globally:

```bash theme={null}
npm install -g aws-cdk
# or to install a specific CDK v2 version:
npm install -g aws-cdk@2.XX.X
```

## Python CDK libraries

For CDK v2, install the consolidated libraries into your project environment (virtualenv recommended):

```bash theme={null}
pip install aws-cdk-lib constructs
```

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:

```bash theme={null}
aws configure
```

Example (values masked):

```text theme={null}
AWS Access Key ID [None]: AKIA****************
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCY****************
Default region name [None]: us-east-2
Default output format [None]: json
```

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:

```bash theme={null}
# Create and enter project directory
mkdir cdk-demo
cd cdk-demo

# Initialize a Python CDK app
cdk init app --language python
```

`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.](https://mintcdn.com/kodekloud-c4ac6d9a/5BuH6-_lHDMy3nuh/images/AWS-Certified-DevOps-Engineer-Professional/Domain-2-Configuration-Management-and-IaC/Demo-Building-With-the-CDK/vscode-cdk-demo-explorer-files.jpg?fit=max\&auto=format\&n=5BuH6-_lHDMy3nuh\&q=85\&s=fb035b6d202f3b2e92511d4743085d75)

## 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:

```python theme={null}
from aws_cdk import Stack, aws_s3 as s3
from constructs import Construct

class CdkDemoStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create an S3 bucket in this stack
        bucket = s3.Bucket(self, "My-KodeKloud-bkt-cld2")
```

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.):

```bash theme={null}
cdk bootstrap
```

Example output (shortened):

```text theme={null}
⏳ Bootstrapping environment aws://123456789012/us-east-2...
Using default execution policy of 'arn:aws:iam::aws:policy/AdministratorAccess'.
✅ Environment aws://123456789012/us-east-2 bootstrapped (no changes)
```

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:

```bash theme={null}
cdk synth
```

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:

```bash theme={null}
cdk deploy
```

CDK synthesizes the app and calls CloudFormation to create/update the stack. Example success summary:

```text theme={null}
✅ CdkDemoStack

✨ Deployment time: 30.00s
Stack ARN:
arn:aws:cloudformation:us-east-2:123456789012:stack/CdkDemoStack/abcd1234-ef56-78gh-90ij-klmnopqrstuv

✨ Total time: 90.00s
```

## 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).](https://mintcdn.com/kodekloud-c4ac6d9a/5BuH6-_lHDMy3nuh/images/AWS-Certified-DevOps-Engineer-Professional/Domain-2-Configuration-Management-and-IaC/Demo-Building-With-the-CDK/aws-cloudformation-stacks-list-screenshot.jpg?fit=max\&auto=format\&n=5BuH6-_lHDMy3nuh\&q=85\&s=cc7e0011d4bd4b09c780980a13c803e5)

## Useful CDK commands

Use these commands during development and cleanup:

```bash theme={null}
cdk ls        # list stacks in the app
cdk synth     # emit the synthesized CloudFormation template
cdk deploy    # deploy the stack to your default AWS account/region
cdk diff      # compare deployed stack to local stack
cdk destroy   # delete the stack
```

To remove deployed resources when finished:

```bash theme={null}
cdk destroy
```

## Links and references

* AWS CLI installation: [https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
* AWS CDK documentation: [https://docs.aws.amazon.com/cdk/latest/guide/home.html](https://docs.aws.amazon.com/cdk/latest/guide/home.html)
* CDK Python guide: [https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-python.html](https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-python.html)

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](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/42510d3c-bacc-4a37-adb8-2cfea86f1a36/lesson/6cca8cea-c09c-4ebf-9f51-ccd21dee7caa)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.