Skip to main content

Write AWS Step Function State Machines in python and export to AWS States Language JSON. Allows for easy reuse of states or state components as well as generating complex state machines from simple pieces. Integrates with other tools like Pulumi to easily have a single python definition of the state machine that can generate specific JSON for multiple AWS environments.

Project description

Step Scribe logo

A package to write AWS Step Function state machines using python and output valid AWS states language JSON or to various visualization formats (eventually).

Installation

pip install stepscribe

Examples

Here's a simple task example that invokes an existing lambda function:

from stepscribe import Task

if __name__ == "__main__":
    get_price = Task(
                    Name="Get Current Price",
                    Next='Check Price',
                    Resource="arn:aws:states:::lambda:invoke",
                    Arguments={'Payload': {"product": "{% $states.context.Execution.Input.product %}"},
                        'FunctionName': "arn:aws:lambda:<region>:account-id:function:priceWatcher:$LATEST",
                    },
                    Assign={'currentPrice': "{% $states.result.Payload.current_price %}"}
    )
    print(get_price.to_asl_json(indent=2))               

This python code generates the following Amazon states language JSON:

{
"Get Current Price": {
  "Type": "Task",
  "QueryLanguage" : "JSONata",
  "Resource": "arn:aws:states:::lambda:invoke",
  "Next": "Check Price",
  "Arguments": {
    "Payload": {
    "product": "{% $states.context.Execution.Input.product %}"
    },
    "FunctionName": "arn:aws:lambda:<region>:account-id:function:priceWatcher:$LATEST"
  },
  "Assign": {
    "currentPrice": "{% $states.result.Payload.current_price %}"
  }
}
}

The value of StepScribe comes from leveraging python to create or compose objects and then generate the corresponding JSON. For example, what if we wanted to get the current price of a product, check if it's on sale, and check our inventory. If each of these actions is a lambda, we now need 3 tasks like above, but with different names and lambda functions, and each of these to be a branch of a parallel state.

from stepscribe import Task, Parallel

def product_task(name: str, lambda_name: str) -> Task:
    output=name.lower().replace(' ', '_').replace('get_', '')

    return Task(
                Name=name,
                Next='Check Price',
                Resource="arn:aws:states:::lambda:invoke",
                Arguments={'Payload': {"product": "{% $states.context.Execution.Input.product %}"},
                        'FunctionName': f"arn:aws:lambda:<region>:account-id:function:{lambda_name}:$LATEST",
                },
                Assign={'currentPrice': f"{{% $states.result.Payload.{output} %}}"}
    )


if __name__ == "__main__":

    tasks = [("Get Current Price", "priceWatcher"), ("Check Sale", "isOnSale"), ("Get Current Inventory", "inventoryWatcher")]
    step_function = Parallel(
                        Name="Product Check",
                        Branches=[product_task(task[0], task[1]) for task in tasks)]
                        )
    print(step_function.to_asl_json())

This allows for considerable consolidation of the boilerplate and becomes more valuable if custom retry, catcher, item readers, or other components are reused across large state machines or for maintaining identical versions of state machines in multiple environments where resource arns can be provided via infrastructure as code without requiring multiple large messy JSON files to be kept in sync.

Roadmap

Currently, StepScribe uses pydantic models for state machine, states and major components and assumes only JSONata is being used (not JSONPath state attributes or query language) and writes these objects to the corresponding states language JSON. StepScribe also provides a load_asl_json that can take a JSON string or a filename for the JSON file and returns a state machine pydantic model.

The following features are planned:

  • Test coverage and documentation
  • JSONPath support
  • Write state machine to mermaidJS diagram
  • JSONata expression validation

Usage, desired features, bug reports, bug fixes, and feature implementations are welcome.

Contributing

For feature requests or bug reports, please submit an issue in GitHub with as much information as possible.

For bug fixes or feature implementations, fork the repo and create a PR to merge your fork branch here and request a review from me. See the contributions guide for more information.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stepscribe-0.4.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stepscribe-0.4.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file stepscribe-0.4.0.tar.gz.

File metadata

  • Download URL: stepscribe-0.4.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stepscribe-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d8b0b4c48c7d37c738282d9cbc0a8e5aa13c6ffbc0c8ebe184c99bf489240016
MD5 ce266b47dc3b1e6642adee971f1e42b9
BLAKE2b-256 01f18fe639eda2303871341861f0c965f092585cde5074c223efe3fa6fcb9803

See more details on using hashes here.

File details

Details for the file stepscribe-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: stepscribe-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stepscribe-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4971d270fb5d919ee6e9636f45fdc0b0ac645f15bc9ed31b8673ab00f64b361
MD5 cc1e4af342b450715cc4e01fa6653845
BLAKE2b-256 9d58d8c5c8616253137b0072566d48ca7e44a32549a4cb952059707e4c7a2c77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page