This workflow demonstrates how to use Step Functions to pre-process data with AWS Lambda and then orchestrate an AWS Batch job. Deploying this sample project will create an AWS Step Functions state machine, a Lambda function, and an AWS Batch job.
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS CDK Installed
-
If this is your first time using AWS CDK, bootstrap your environment.
cdk bootstrap aws://{your-aws-account-number}/{your-aws-region}
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/step-functions-workflows-collection
-
Change directory to the pattern directory:
cd batch-lambda-cdk/batch_lambda_cdk
-
Create a Python virtual environment and install the requirements:
python3 -m venv .venv source .venv/bin/activate python3 -m pip install -r requirements.txt
-
From the command line, use CDK to deploy the AWS resources for the workflow as specified in the
app.py
file:cdk deploy
-
During the prompts:
Do you wish to deploy these changes (y/n)? Y
-
Note the outputs from the cdk deployment process. The State Machine Name and ARN are outputted for testing.
The first step of this workflow uses a Lambda function to generate a random number and then passes this number to the next step. Next, the Submit Batch Job step submits an AWS Batch job with the value passed from the previous step. The AWS Batch job simply prints the supplied argument.
Manually trigger the workflow via the Console or the AWS CLI. The state machine ARN can be found as the StateMachineArn
output and the state machine name can be found as StateMachineName
in the output.
To trigger the workflow in the console, navigate to Step Functions and then click the step function name from the list of State Machines. In the Executions panel, click Start Execution. Click Start Execution again in the popup. No additional input is required.
Once the step function completes, inspect the output of the Generate batch job input
state. The output will look similiar to this, but your input
value may be different.
{
"Comment": "Insert your JSON here",
"batch_input": {
"input": "8"
}
}
Next, inspect the Input of the Submit Batch Job
state. This shows the result of passing the prior state's output to the input of the next state. Your input will match the output from the prior state:
{
"Comment": "Insert your JSON here",
"batch_input": {
"input": "8"
}
}
- Delete the stack
cdk destroy
- During the prompts:
Are you sure you want to delete: BatchLambdaCdkStack (y/n)? Y
Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0