Created
November 30, 2022 12:06
-
-
Save eyalroth/c8c59e02d70cc14335275d0683c47a5b to your computer and use it in GitHub Desktop.
AWS Step Functions Batch Process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash'); | |
const { mapSeries } = require('bluebird'); | |
// express handler | |
async (req, res) => { | |
const sfExecutionIdHeader = req.header('SF-Execution-Id'); | |
const executionId = sfExecutionIdHeader.substring(sfExecutionIdHeader.lastIndexOf(':') + 1); | |
const ids = await selectIdsFromDb(); | |
const batches = _.chunk(ids, config.get('batch_size')); | |
const batchS3Keys = await mapSeries(batches, async (batch, index) => { | |
const key = `${executionId}/${index}/batch.json`; | |
await s3Client.upload({ | |
Bucket: config.get('bucket'), | |
Key: key, | |
Body: JSON.stringify(batch) | |
}).promise(); | |
return key; | |
}); | |
res.status(200).send({ | |
batchS3Keys, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Definition: | |
StartAt: PrepareBatches | |
States: | |
PrepareBatches: | |
Type: Task | |
Resource: arn:aws:states:::lambda:invoke | |
Parameters: | |
FunctionName: !Ref MyLambda | |
Payload: | |
# using nodejs express application inside the lambda | |
httpMethod: POST | |
path: /prepare-batches | |
headers: | |
SF-Execution-Id.$: $$.Execution.Id | |
ResultSelector: | |
body.$: States.StringToJson($.Payload.body) | |
statusCode.$: $.Payload.statusCode | |
Next: InvokeBatches | |
InvokeBatches: | |
Type: Map | |
# previous lambda invocation writes all the S3 keys in this field | |
ItemsPath: $.body.batchS3Keys | |
MaxConcurrency: 1 | |
Iterator: | |
StartAt: InvokeCaller | |
States: | |
InvokeCaller: | |
Type: Task | |
Resource: arn:aws:states:::states:startExecution.sync:2 | |
Parameters: | |
StateMachineArn: !GetAtt CallerStepFunction.Arn | |
Input: | |
batchS3Key.$: $ | |
AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$: $$.Execution.Id | |
End: true | |
Retry: | |
- ErrorEquals: ["States.TaskFailed"] | |
IntervalSeconds: 10 | |
MaxAttempts: 2 | |
Catch: | |
- ErrorEquals: ["States.TaskFailed"] | |
Next: CallerFailed | |
CallerFailed: | |
Type: Succeed | |
End: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment