- Unit test with aws_cdk.assertions
- Doc to export package to PyPi
(.venv) ➜ aws_cdk_helloworld git:(master) ✗ cdk synth
[Error at /AwsCdkHelloworldStack/AwsCdkHelloworldQueue/Resource] AwsSolutions-SQS3: The SQS queue is not used as a dead-letter queue (DLQ) and does not have a DLQ enabled.
[Error at /AwsCdkHelloworldStack/AwsCdkHelloworldQueue/Resource] AwsSolutions-SQS4: The SQS queue does not require requests to use SSL.
Found errors
https://github.com/cdklabs/cdk-nag/blob/HEAD/RULES.md#additional-rules
To create an AWS CDK construct as a Pip package, follow these steps:
- Choose a programming language: AWS CDK supports Python, JavaScript (TypeScript), and Java. For this example, we’ll use Python.
- Create a new directory for your construct package. Initialize a new Python package using pip:
mkdir my_cdk_construct
cd my_cdk_construct
python -m pip init
- Install AWS CDK for Python as a development dependency:
python -m pip install -r requirements.txt
- Create a new construct class in a file named init.py (or construct.py if you prefer). This class should extend aws_cdk.core.Construct. For example:
from aws_cdk import core
class MyCdkConstruct(core.Construct):
def __init__(self, scope: core.Construct, id: str, **props):
super().__init__(scope, id, props)
# Define your construct's logic and resources here
-
Define your construct’s resources and logic within the MyCdkConstruct class. This might include creating AWS resources, such as S3 buckets or Lambda functions, using AWS CDK’s API.
-
Create a setup.py file to define your package’s metadata and dependencies:
from setuptools import setup
setup(
name='my_cdk_construct',
version='1.0.0',
packages=['my_cdk_construct'],
install_requires=['aws-cdk'],
author='Your Name',
author_email='[email protected]'
)
- Build and package your construct using setuptools:
python setup.py sdist bdist_wheel
- Upload your package to PyPI using twine:
twine upload dist/*
- Install your package using Pip:
pip install my_cdk_construct
My package: https://pypi.org/project/aws-cdk-helloworld/
This is a blank project for CDK development with Python.
The cdk.json
file tells the CDK Toolkit how to execute your app.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .venv
directory. To create the virtualenv it assumes that there is a python3
(or python
for Windows) executable in your path with access to the venv
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.
To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
Once the virtualenv is activated, you can install the required dependencies.
$ pip install -r requirements.txt
At this point you can now synthesize the CloudFormation template for this code.
$ cdk synth
To add additional dependencies, for example other CDK libraries, just add
them to your setup.py
file and rerun the pip install -r requirements.txt
command.
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation
(.venv) ➜ ✗ pytest
================================================================= test session starts ==================================================================
platform darwin -- Python 3.12.2, pytest-8.3.3, pluggy-1.5.0
rootdir: /Users/me/Documents/code/aws_cdk_helloworld
plugins: typeguard-2.13.3
collected 2 items
tests/cdk_test.py . [ 50%]
tests/unit/test_aws_cdk_helloworld_stack.py . [100%]
================================================================== 2 passed in 2.74s ===================================================================
(.venv) ➜ ✗
Enjoy!