Skip to content

Commit 997dbcc

Browse files
authored
feat: Add python support to cdk init (#2130)
* First take on an init template for a Python CDK project. * The cdk.context.json should not be ignored. * Fixing some review issues * Restructured to move app into a separate file outside of package directory. Also some formatting changes. * Slight change to code formatting. * Remove reference to app.sh * Moving this over to sample-app since it actually creates a sample app, not an empty project. Will need to create a separate issue to move the others over and create templates for empty projects. * Adding an app template for Python. * Keep all dependencies in setup.py and use -e . in requirements.txt to use those and automatically run the python setup.py develop step. Also use templating feature for blank app. * Templatize the class name.
1 parent 2e92d44 commit 997dbcc

21 files changed

+312
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aws_cdk import cdk
2+
3+
4+
class %name.PascalCased%Stack(cdk.Stack):
5+
6+
def __init__(self, app: cdk.App, id: str, **kwargs) -> None:
7+
super().__init__(app, id)
8+
9+
# The code that defines your stack goes here

packages/aws-cdk/lib/init-templates/app/python/%name%/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.swp
2+
package-lock.json
3+
__pycache__
4+
.pytest_cache
5+
.env
6+
*.egg-info
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
# Welcome to your CDK Python project!
3+
4+
This is a blank project for Python development with CDK.
5+
6+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
7+
8+
This project is set up like a standard Python project. The initialization process also creates
9+
a virtualenv within this project, stored under the .env directory.
10+
11+
After the init process completes, you can use the following steps to get your project set up.
12+
13+
```
14+
$ source .env/bin/activate
15+
$ pip install -r requirements.txt
16+
$ python setup.py develop
17+
```
18+
19+
At this point you can now synthesize the CloudFormation template for this code.
20+
21+
```
22+
$ cdk synth
23+
```
24+
25+
To add additional dependencies, for example other CDK libraries, just add to
26+
your requirements.txt file and rerun the `pip install -r requirements.txt`
27+
command.
28+
29+
# Useful commands
30+
31+
* `cdk ls` list all stacks in the app
32+
* `cdk synth` emits the synthesized CloudFormation template
33+
* `cdk deploy` deploy this stack to your default AWS account/region
34+
* `cdk diff` compare deployed stack with current state
35+
* `cdk docs` open CDK documentation
36+
37+
Enjoy!
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
3+
from aws_cdk import cdk
4+
5+
from %name%.%name%_stack import PyStack
6+
7+
8+
app = cdk.App()
9+
PyStack(app, "%name%-cdk-1")
10+
11+
app.run()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "python3 app.py"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-e .
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import setuptools
2+
3+
4+
with open("README.md") as fp:
5+
long_description = fp.read()
6+
7+
8+
setuptools.setup(
9+
name="%name%",
10+
version="0.0.1",
11+
12+
description="An empty CDK Python app",
13+
long_description=long_description,
14+
long_description_content_type="text/markdown",
15+
16+
author="author",
17+
18+
package_dir={"": "%name%"},
19+
packages=setuptools.find_packages(where="%name%"),
20+
21+
install_requires=[
22+
"aws-cdk.cdk",
23+
],
24+
25+
python_requires=">=3.6",
26+
27+
classifiers=[
28+
"Development Status :: 4 - Beta",
29+
30+
"Intended Audience :: Developers",
31+
32+
"License :: OSI Approved :: Apache Software License",
33+
34+
"Programming Language :: JavaScript",
35+
"Programming Language :: Python :: 3 :: Only",
36+
"Programming Language :: Python :: 3.6",
37+
"Programming Language :: Python :: 3.7",
38+
"Programming Language :: Python :: 3.8",
39+
40+
"Topic :: Software Development :: Code Generators",
41+
"Topic :: Utilities",
42+
43+
"Typing :: Typed",
44+
],
45+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.swp
2+
package-lock.json
3+
__pycache__
4+
.pytest_cache
5+
.env
6+
*.egg-info
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# Welcome to your CDK Python project!
3+
4+
You should explore the contents of this template. It demonstrates a CDK app with two instances of
5+
a stack (`HelloStack`) which also uses a user-defined construct (`HelloConstruct`).
6+
7+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
8+
9+
This project is set up like a standard Python project. The initialization process also creates
10+
a virtualenv within this project, stored under the .env directory.
11+
12+
After the init process completes, you can use the following steps to get your project set up.
13+
14+
```
15+
$ source .env/bin/activate
16+
$ pip install -r requirements.txt
17+
```
18+
19+
At this point you can now synthesize the CloudFormation template for this code.
20+
21+
```
22+
$ cdk synth
23+
```
24+
25+
You can now begin exploring the source code, contained in the hello directory.
26+
There is also a very trivial test included that can be run like this:
27+
28+
```
29+
$ pytest
30+
```
31+
32+
To add additional dependencies, for example other CDK libraries, just add to
33+
your requirements.txt file and rerun the `pip install -r requirements.txt`
34+
command.
35+
36+
# Useful commands
37+
38+
* `cdk ls` list all stacks in the app
39+
* `cdk synth` emits the synthesized CloudFormation template
40+
* `cdk deploy` deploy this stack to your default AWS account/region
41+
* `cdk diff` compare deployed stack with current state
42+
* `cdk docs` open CDK documentation
43+
44+
Enjoy!

0 commit comments

Comments
 (0)