Sunday, August 04, 2019

AWS CDK Python - Sample Project

This blog shows you how to create a sample project using AWS CDK Python. If you don't have the environment setup yet, check out my previous blog:

Create the Python Application

1. Fire up you Python3 virtual environment, create a project directory contains your Python project source code
$ source cdk-env/bin/activate
(cdk-venv) $ mkdir sample-dev
(cdk-venv) $ cd sample-dev
(cdk-venv) $ cdk init
Available templates:
* app: Template for a CDK Application
   └─ cdk init app --language=[csharp|fsharp|java|javascript|python|typescript]
* lib: Template for a CDK Construct Library
   └─ cdk init lib --language=typescript
* sample-app: Example CDK Application with some constructs
   └─ cdk init sample-app --language=[javascript|python|typescript]
2. The return information from cdk is very self-explanatary, we can choose "sample-app" so by the creation of this app, we have some exisitng constructs
$ cdk init --language python sample-app
Applying project template sample-app for python
Initializing a new git repository...
Executing Creating virtualenv...

3. The entire sample project looks like this:
├── - Readme file
├──    - Defines app stacks, also the "main" file
├── cdk.json  - Configuration file for CDK that defines what executable CDK should run to generate CDK construct tree
├── hello
│   ├──         - To make a Python module
│   ├── hello.egg-info
│   │   ├── PKG-INFO
│   │   ├── SOURCES.txt
│   │   ├── dependency_links.txt
│   │   ├── requires.txt
│   │   └── top_level.txt
│   ├──  - A custom CDK construct defined for use in your CDK application.
│   └──      - A custom CDK stack construct for use in your CDK application
├── requirements.txt     - Required Python modules
├──             - Defines how this Python package would be constructed and what the dependencies are
└── tests                - Test folder
    └── unit             - ALl the unit tests

You can take time to read and understand the source code, but esentially what this code does is to create two CloudFormation templates: "hello-cdk-1" and "hello-cdk-2", and deploy them to "us-east-1" and "us-west-1" regions. Each template includes:

  • IAM user with predefined policies
  • AWS sqs queue
  • AWS sns topic and subscribe to the sqs queue
  • Few AWS S3 buckets

4. Give a read of the file, you can ignore the Python virtual env part, as we are already using a virtual env. As the suggest, we going to install all required Python packages and run some unit tests.
(cdk-venv) $ pip install -r requirements.txt
Collecting pytest (from -r requirements.txt (line 2))
  Downloading (221kB)
    100% |████████████████████████████████| 225kB 4.5MB/s 

(cdk-venv) $ pytest
================================================================================================================= test session starts
platform darwin -- Python 3.7.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/txu/code/flashhop-dev
collected 1 item                                                                                                                                                                                                                                      

tests/unit/ . 

5. Generate AWS CloudFormation template
(cdk-venv) $ cdk synth
Successfully synthesized to /Users/txu/code/flashhop-dev/cdk.out
Supply a stack name (hello-cdk-1, hello-cdk-2) to display its template.
All the output files will be located in "cdk.out"

6. Deploy the generated templates to AWS account and validate the right resouces got created.
(cdk-venv) $ cdk deploy --profile YOU_AWS_PROFILE hello-cdk-1

7. To destroy the stack
(cdk-venv) $ cdk --profile flashhop-dev destroy hello-cdk-1
Are you sure you want to delete: hello-cdk-1 (y/n)? y
hello-cdk-1: destroying...

At this point, you learned how to use AWS CDK CLI to initialize a new Python project/app, how to synthesize hte Python code into AWS Cloudformation templates, deploy them, provision your infrastructure and destroy your infrastructure. You can learn more from AWS CDK developer documentations (

No comments: