Great! You want to design a scalable, cost-efficient system on AWS to scrape millions of records from TMDB using two Lambda functions:
- Harvester: Downloads a ZIP file containing all valid IDs for a dataset.
- Collector: Given a batch of IDs, collects detailed info and saves it.
You want to orchestrate this with AWS Step Functions, combining Standard and Express workflows to optimize cost and performance.
Key Requirements & Constraints
- Millions of records → large scale, so cost and concurrency matter.
- Harvester runs once per dataset (daily).
- Collector runs many times, each time processing a batch of IDs.
- Use Step Functions to orchestrate the flow.
- Combine Standard and Express Step Functions for best price/performance.
Proposed Architecture
1. High-Level Flow
-
Trigger Harvester (Standard Step Function)
- Input: dataset name
- Output: ZIP file with all valid IDs (stored in S3)
- Extract IDs and split into batches (e.g., 1000 IDs per batch)
- Store batches metadata (e.g., batch files or batch ID lists) in S3 or DynamoDB
-
Trigger Collector (Express Step Function)
- For each batch of IDs, invoke Collector Lambda to fetch details and save results.
- Use Express Step Function for high concurrency and low cost per execution.
-
Monitor and Aggregate Results
- Optionally, use a Standard Step Function or Lambda to monitor progress and aggregate results.
2. Why combine Standard + Express?
-
Standard Step Functions:
- Durable, long-running, supports retries, error handling.
- Good for orchestrating the entire workflow (harvesting + batch creation).
- Cost: $0.025 per 1,000 state transitions (higher cost but reliable).
-
Express Step Functions:
- Designed for high-volume, short-duration workflows.
- Lower cost per execution, but less durable.
- Perfect for running millions of Collector executions in parallel.
3. Detailed Design
Step 1: Harvester Workflow (Standard Step Function)
-
Start → Invoke Harvester Lambda with dataset name.
-
Harvester Lambda:
- Downloads ZIP from TMDB.
- Extracts all IDs.
- Splits IDs into batches (e.g., 1000 IDs per batch).
- Saves batches metadata (e.g., batch files or DynamoDB entries).
-
Output: List of batch references (S3 keys or DynamoDB keys).
-
Next Step: Pass batch list to a Lambda that triggers Collector executions.
Step 2: Collector Workflow (Express Step Function)
- Input: Batch of IDs.
- Collector Lambda:
- For each ID in batch, call TMDB API to get details.
- Save details to S3/DynamoDB.
- Express Step Function:
- Runs Collector Lambda.
- Handles retries and errors at batch level.
- Runs many executions in parallel (thousands or more).
Step 3: Orchestration Lambda (in Standard Step Function)
- After Harvester finishes, this Lambda:
- Reads batch list.
- For each batch, starts an Express Step Function execution for Collector.
- Optionally, track progress (e.g., count of completed batches).
4. Example Step Function State Machine
{
"Comment": "Harvester + Collector Orchestration",
"StartAt": "RunHarvester",
"States": {
"RunHarvester": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:Harvester",
"ResultPath": "$.batches",
"Next": "TriggerCollectors"
},
"TriggerCollectors": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:TriggerCollectors",
"End": true
}
}
}
Harvester Lambda returns a list of batch IDs or S3 keys.
TriggerCollectors Lambda loops over batches and calls Express Step Function executions for Collector.
5. TriggerCollectors Lambda Pseudocode
import boto3
sf_client = boto3.client('stepfunctions')
def lambda_handler(event, context):
batches = event['batches'] # list of batch references
for batch in batches:
sf_client.start_execution(
stateMachineArn='arn:aws:states:region:account-id:stateMachine:CollectorExpressStateMachine',
input=json.dumps({"batch": batch})
)
return {"message": "Collector executions started", "count": len(batches)}
6. Collector Express Step Function
- Simple state machine that runs Collector Lambda on the batch.
- Handles retries on failure.
- Example:
{
"StartAt": "CollectBatch",
"States": {
"CollectBatch": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:Collector",
"Retry": [
{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 5,
"MaxAttempts": 3,
"BackoffRate": 2.0
}
],
"End": true
}
}
}
7. Cost Optimization Tips
- Use Standard Step Function only for orchestration and long-running tasks (Harvester).
- Use Express Step Function for massive parallel Collector executions.
- Batch IDs to reduce number of executions.
- Use S3 or DynamoDB for intermediate storage.
- Tune batch size to balance Lambda execution time and concurrency limits.
- Use Lambda reserved concurrency and Step Functions quotas carefully.
Summary
| Component | Type | Purpose | Notes |
|---|
| Harvester Lambda | Lambda + Standard SF | Download ZIP, extract IDs, batch | Durable, long-running |
| Collector Lambda | Lambda + Express SF | Fetch details for batch of IDs | High concurrency, low cost |
| TriggerCollectors | Lambda (Standard SF) | Start Collector executions | Orchestrates Express SF runs |
| Storage | S3 / DynamoDB | Store batches and results | Durable, scalable |
If you want, I can help you write the full CloudFormation or CDK templates for this architecture or provide sample Lambda code! Would you like that?