Skip to content

Commit c5b5314

Browse files
rhboydrix0rrr
authored andcommitted
fix: update Python StepFunctions project layout (aws-samples#66)
1 parent 8ec63ed commit c5b5314

File tree

7 files changed

+176
-66
lines changed

7 files changed

+176
-66
lines changed

python/stepfunctions/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
# AWS Step Functions with Python CDK!
3+
4+
## Overview
5+
6+
Creates an AWS Step Functions StateMachine with the Python language bindings for CDK.
7+
8+
![alt text](./statemachine.png "State Machine created with CDK")
9+
10+
## Setup
11+
12+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
13+
14+
This project is set up like a standard Python project. The initialization
15+
process also creates a virtualenv within this project, stored under the .env
16+
directory. To create the virtualenv it assumes that there is a `python3`
17+
(or `python` for Windows) executable in your path with access to the `venv`
18+
package. If for any reason the automatic creation of the virtualenv fails,
19+
you can create the virtualenv manually.
20+
21+
To manually create a virtualenv on MacOS and Linux:
22+
23+
```
24+
$ python3 -m venv .env
25+
```
26+
27+
After the init process completes and the virtualenv is created, you can use the following
28+
step to activate your virtualenv.
29+
30+
```
31+
$ source .env/bin/activate
32+
```
33+
34+
If you are a Windows platform, you would activate the virtualenv like this:
35+
36+
```
37+
% .env\Scripts\activate.bat
38+
```
39+
40+
Once the virtualenv is activated, you can install the required dependencies.
41+
42+
```
43+
$
44+
```
45+
46+
At this point you can now synthesize the CloudFormation template for this code.
47+
48+
```
49+
$ cdk synth
50+
```
51+
52+
To add additional dependencies, for example other CDK libraries, just add
53+
them to your `setup.py` file and rerun the `pip install -r requirements.txt`
54+
command.
55+
56+
# Useful commands
57+
58+
* `cdk ls` list all stacks in the app
59+
* `cdk synth` emits the synthesized CloudFormation template
60+
* `cdk deploy` deploy this stack to your default AWS account/region
61+
* `cdk diff` compare deployed stack with current state
62+
* `cdk docs` open CDK documentation
63+
64+
Enjoy!

python/stepfunctions/app.py

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,7 @@
11
#!/usr/bin/env python3
2-
from aws_cdk import (
3-
aws_stepfunctions as sfn,
4-
aws_stepfunctions_tasks as sfn_tasks,
5-
core,
6-
)
72

8-
9-
class JobPollerStack(core.Stack):
10-
def __init__(self, app: core.App, id: str, **kwargs) -> None:
11-
super().__init__(app, id, **kwargs)
12-
13-
submit_job_activity = sfn.Activity(
14-
self, "SubmitJob"
15-
)
16-
check_job_activity = sfn.Activity(
17-
self, "CheckJob"
18-
)
19-
20-
submit_job = sfn.Task(
21-
self, "Submit Job",
22-
task=sfn_tasks.InvokeActivity(submit_job_activity),
23-
result_path="$.guid",
24-
)
25-
wait_x = sfn.Wait(
26-
self, "Wait X Seconds",
27-
time=sfn.WaitTime.seconds_path('$.wait_time'),
28-
)
29-
get_status = sfn.Task(
30-
self, "Get Job Status",
31-
task=sfn_tasks.InvokeActivity(check_job_activity),
32-
input_path="$.guid",
33-
result_path="$.status",
34-
)
35-
is_complete = sfn.Choice(
36-
self, "Job Complete?"
37-
)
38-
job_failed = sfn.Fail(
39-
self, "Job Failed",
40-
cause="AWS Batch Job Failed",
41-
error="DescribeJob returned FAILED"
42-
)
43-
final_status = sfn.Task(
44-
self, "Get Final Job Status",
45-
task=sfn_tasks.InvokeActivity(check_job_activity),
46-
input_path="$.guid",
47-
)
48-
49-
definition = submit_job\
50-
.next(wait_x)\
51-
.next(get_status)\
52-
.next(is_complete
53-
.when(sfn.Condition.string_equals(
54-
"$.status", "FAILED"), job_failed)
55-
.when(sfn.Condition.string_equals(
56-
"$.status", "SUCCEEDED"), final_status)
57-
.otherwise(wait_x))
58-
59-
sfn.StateMachine(
60-
self, "StateMachine",
61-
definition=definition,
62-
timeout=core.Duration.seconds(30),
63-
)
3+
from aws_cdk import core
4+
from stepfunctions.stepfunctions_stack import JobPollerStack
645

656
app = core.App()
667
JobPollerStack(app, "aws-stepfunctions-integ")
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
aws-cdk.cdk
2-
3-
aws-cdk.aws-stepfunctions
4-
aws-cdk.aws-stepfunctions-tasks
5-
1+
-e .

python/stepfunctions/setup.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import setuptools
2+
3+
4+
with open("README.md") as fp:
5+
long_description = fp.read()
6+
7+
8+
setuptools.setup(
9+
name="stepfunctions",
10+
version="0.0.1",
11+
12+
description="A simple StepFunction created with AWS CDK for Python",
13+
long_description=long_description,
14+
long_description_content_type="text/markdown",
15+
16+
author="author",
17+
18+
package_dir={"": "stepfunctions"},
19+
packages=setuptools.find_packages(where="stepfunctions"),
20+
21+
install_requires=[
22+
"aws-cdk.core",
23+
"aws-cdk.aws-stepfunctions",
24+
"aws-cdk.aws-stepfunctions-tasks"
25+
],
26+
27+
python_requires=">=3.6",
28+
29+
classifiers=[
30+
"Development Status :: 4 - Beta",
31+
32+
"Intended Audience :: Developers",
33+
34+
"License :: OSI Approved :: Apache Software License",
35+
36+
"Programming Language :: JavaScript",
37+
"Programming Language :: Python :: 3 :: Only",
38+
"Programming Language :: Python :: 3.6",
39+
"Programming Language :: Python :: 3.7",
40+
"Programming Language :: Python :: 3.8",
41+
42+
"Topic :: Software Development :: Code Generators",
43+
"Topic :: Utilities",
44+
45+
"Typing :: Typed",
46+
],
47+
)
32.2 KB
Loading

python/stepfunctions/stepfunctions/__init__.py

Whitespace-only changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from aws_cdk import (
2+
aws_stepfunctions as sfn,
3+
aws_stepfunctions_tasks as sfn_tasks,
4+
core,
5+
)
6+
7+
8+
class JobPollerStack(core.Stack):
9+
def __init__(self, app: core.App, id: str, **kwargs) -> None:
10+
super().__init__(app, id, **kwargs)
11+
12+
submit_job_activity = sfn.Activity(
13+
self, "SubmitJob"
14+
)
15+
check_job_activity = sfn.Activity(
16+
self, "CheckJob"
17+
)
18+
19+
submit_job = sfn.Task(
20+
self, "Submit Job",
21+
task=sfn_tasks.InvokeActivity(submit_job_activity),
22+
result_path="$.guid",
23+
)
24+
wait_x = sfn.Wait(
25+
self, "Wait X Seconds",
26+
time=sfn.WaitTime.seconds_path('$.wait_time'),
27+
)
28+
get_status = sfn.Task(
29+
self, "Get Job Status",
30+
task=sfn_tasks.InvokeActivity(check_job_activity),
31+
input_path="$.guid",
32+
result_path="$.status",
33+
)
34+
is_complete = sfn.Choice(
35+
self, "Job Complete?"
36+
)
37+
job_failed = sfn.Fail(
38+
self, "Job Failed",
39+
cause="AWS Batch Job Failed",
40+
error="DescribeJob returned FAILED"
41+
)
42+
final_status = sfn.Task(
43+
self, "Get Final Job Status",
44+
task=sfn_tasks.InvokeActivity(check_job_activity),
45+
input_path="$.guid",
46+
)
47+
48+
definition = submit_job\
49+
.next(wait_x)\
50+
.next(get_status)\
51+
.next(is_complete
52+
.when(sfn.Condition.string_equals(
53+
"$.status", "FAILED"), job_failed)
54+
.when(sfn.Condition.string_equals(
55+
"$.status", "SUCCEEDED"), final_status)
56+
.otherwise(wait_x))
57+
58+
sfn.StateMachine(
59+
self, "StateMachine",
60+
definition=definition,
61+
timeout=core.Duration.seconds(30),
62+
)

0 commit comments

Comments
 (0)