You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Composable Terraform module that adds safe concurrency control to AWS Step Functions.
It wraps a purpose-built Lambda function, DynamoDB table, and IAM wiring to implement the distributed lease pattern so you can throttle fan-out workloads without rewriting business logic.
Why this module?
Deterministic fan-out throttling – enforce a fixed number of parallel tasks across Step Functions Map states or nested workflows.
Drop-in state machine guards – ship pre-defined AcquireLease, optional CheckLeaseStatus + WaitForLease, and ReleaseLease state JSON that can be merged into existing definitions.
Production defaults – opinionated CloudWatch log retention, Powertools observability configuration, and optional DynamoDB autoscaling.
Transparent IAM – emitted inline policies and managed policy attachments so platform teams can review grants before deploying.
Tested runtime – the bundled Lambda function is covered by 100% unit test coverage using moto-backed regression tests.
Quick start
module"sfn_concurrency" {
source="GiamPy5/sfn-concurrency-lease/aws"name_prefix="analytics-pipeline"max_concurrent_leases=10max_lease_duration_seconds=900# Optional: let the module create the table and Lambda packagecreate_lambdas=truecreate_dynamodb_table=true# Optional: reuse an external table# create_dynamodb_table = false# ddb_table_name = "shared-concurrency-leases"kms_key_arn=aws_kms_key.lambda_env.arn
}
Integrate the concurrency guard states into an existing Step Functions definition:
Handles acquire / release actions, counts active leases, and enforces TTLs. Ships with AWS Lambda Powertools logging, tracing, and metrics.
DynamoDB table
Stores leases as {PK, SK} items with TTL attribute so expired leases are reclaimed automatically.
Terraform locals & outputs
Provide ready-made Step Functions state fragments and IAM policy JSON to embed in existing workflows.
Execution flow:
Step Function enters AcquireLease state and invokes the Lambda with action=acquire.
Lambda counts active (TTL > now) leases; returns wait if the fleet is saturated.
Business logic executes while holding the lease.
ReleaseLease state calls the same Lambda with action=release to free capacity. If a lease disappears naturally (TTL expiry), the release call is treated as idempotent.
# 1. Create a virtual environment
python -m venv .venv
source .venv/bin/activate
# 2. Install test dependencies
pip install -U pip
pip install boto3 moto pytest aws-lambda-powertools
# 3. Run the regression suite (includes 100% coverage for the Lambda runtime)
pytest
# Optional: view coverage
pytest --cov=src/lease_manager/lambda_function.py
Contributing
Fork the repository and create a feature branch.
Run terraform fmt and pytest before opening a PR.
Update examples and documentation when toggling or adding inputs.
Submit the pull request with a concise summary of the change.
Bug reports and feature requests are welcome through GitHub issues. Please include Terraform version, AWS provider version, and reproduction steps when reporting problems.
License
This project is licensed under the Apache License 2.0. You are free to use, modify, and distribute the module in personal or commercial projects as long as you retain the license notice.
About
Reusable concurrency lease system for AWS Step Functions — implements the Distributed Lease Pattern using Lambda and DynamoDB to coordinate parallel executions safely across distributed workflows.