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.md - Readme file
├── app.py - 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
│ ├── __init__.py - To make a Python module
│ ├── hello.egg-info
│ │ ├── PKG-INFO
│ │ ├── SOURCES.txt
│ │ ├── dependency_links.txt
│ │ ├── requires.txt
│ │ └── top_level.txt
│ ├── hello_construct.py - A custom CDK construct defined for use in your CDK application.
│ └── hello_stack.py - A custom CDK stack construct for use in your CDK application
├── requirements.txt - Required Python modules
├── setup.py - Defines how this Python package would be constructed and what the dependencies are
└── tests - Test folder
├── __init__.py
└── unit - ALl the unit tests
├── __init__.py
└── test_hello_construct.py
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 README.md file, you can ignore the Python virtual env part, as we are already using a virtual env. As the README.md 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 https://files.pythonhosted.org/packages/69/1d/2430053122a3c6106f7fd1ff0bc68eb73e27db8f951db70fcd942da52c7b/pytest-5.0.1-py3-none-any.whl (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/test_hello_construct.py .
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 (https://docs.aws.amazon.com/cdk/latest/guide/home.html)
No comments:
Post a Comment