Worker Service Preset
The Worker Service preset creates a background processing service with Lambda functions, SQS queues, and DynamoDB for task management.
Architecture
Generated resources
- SQS queue(s) for work dispatch
- Optional DLQ for failures
- Lambda consumer(s) for background processing
- Optional DynamoDB table for persistence/state
- Necessary IAM permissions and event source mappings
Typical event flow
- A producer (API, scheduler, or another service) enqueues a job to SQS
- Lambda consumer picks messages in batches
- Handler processes the job and writes results to DynamoDB/S3 or publishes follow-up events
- Failed messages go to DLQ for inspection and retries
Usage
Generate a worker service:
nx g @mhshahzad/nx-cdk-deploy:preset --name=jobs --preset=worker-service
Deploy to an environment:
nx deploy jobs --configuration=dev
Configuration
- Queue name, visibility timeout, batch size
- DLQ enablement and retention period
- Concurrency limits and reserved concurrency for Lambda
- Environment variables for handler behavior
Configure per environment via project config and .env.* files:
Extend with resource bundles
- Add a table for job state: DynamoDB
- Publish/consume domain events: SNS/SQS
- Store artifacts/results: S3
Operations and scaling
- Use DLQ alarms and metrics for failure visibility
- Tune batch size and concurrency to match downstream capacity
- Prefer idempotent handlers to safely retry
- Tag resources for cost and ownership