Amazon Batch with Lambda
This sample project demonstrates how to use Step Functions to pre-process data with Amazon Lambda functions and then orchestrate Amazon Batch jobs. Deploying this sample project will create an Amazon Step Functions state machine, a Lambda function, and an Amazon Batch job.
In this project, Step Functions uses a state machine to invoke a Lambda function to do simple pre-processing before an Amazon Batch job is submitted. Multiple jobs may be invoked depending on the result/success of the previous one.
Create the State Machine and Provision Resources
-
Open the Amazon Batch with Lambda
sample project. The state machine Code and Visual Workflow are displayed.
-
Choose Next.
The Deploy and run page is displayed, listing the resources that will be created. For this sample project, the resources include:
-
An Amazon Batch job
-
A Lambda function
-
-
Choose Deploy Resources.
Note
It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Deploy resources page is displayed, you can open the Stack ID link to see which resources are being provisioned.
Start a New Execution
-
On the New execution page, enter an execution name (optional), and then choose Start Execution.
(Optional) To identify your execution, you can specify a name for it in the Name box. By default, Step Functions generates a unique execution name automatically.
Note
Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.
-
Optionally, you can go to the newly created state machine on the Step Functions Dashboard, and then choose New execution.
-
When an execution is complete, you can select states on the Visual workflow and browse the Input and Output under Step details.
Example State Machine Code
The state machine in this sample project integrates with Amazon Batch and Amazon SNS by passing parameters directly to those resources.
Browse through this example state machine to see how Step Functions controls Amazon Batch and Amazon SNS by
connecting to the Amazon Resource Name (ARN) in the Resource
field, and by passing
Parameters
to the service API.
For more information about how Amazon Step Functions can control other Amazon services, see Using Amazon Step Functions with other services.
{
"Comment": "An example of the Amazon States Language for using batch job with pre-processing lambda",
"StartAt": "Generate batch job input",
"TimeoutSeconds": 3600,
"States": {
"Generate batch job input": {
"Type": "Task",
"Resource": "arn:aws-cn:states:::lambda:invoke",
"OutputPath": "$.batch_input",
"Parameters": {
"FunctionName": "<GENERATE_BATCH_JOB_INPUT_LAMBDA_FUNCTION_NAME>
"
},
"Next": "Submit Batch Job"
},
"Submit Batch Job": {
"Type": "Task",
"Resource": "arn:aws-cn:states:::batch:submitJob.sync",
"Parameters": {
"JobName": "BatchJobFanOut",
"JobQueue": "<BATCH_QUEUE_ARN>
",
"JobDefinition": "<BATCH_JOB_DEFINITION_ARN>
",
"Parameters.$": "$.batch_input"
},
"End": true
}
}
}
IAM Example
These example Amazon Identity and Access Management (IAM) policies generated by the sample project includes the least privilege necessary to execute the state machine and related resources. We recommend that you include only those permissions that are necessary in your IAM policies.
Example BatchJobWithLambdaAccessPolicy
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sns:Publish"
],
"Resource": [
"arn:aws-cn:sns:us-west-2
:123456789012:ManageBatchJob-SNSTopic-JHLYYG7AZPZI
"
],
"Effect": "Allow"
},
{
"Action": [
"batch:SubmitJob",
"batch:DescribeJobs",
"batch:TerminateJob"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"events:PutTargets",
"events:PutRule",
"events:DescribeRule"
],
"Resource": [
"arn:aws-cn:events:us-west-2
:123456789012:rule/StepFunctionsGetEventsForBatchJobsRule"
],
"Effect": "Allow"
}
]
}
Example InvokeGenerateBatchJobMapLambdaPolicy
{
"Statement": [
{
"Action": [
"lambda:InvokeFunction"
],
"Resource": "arn:aws-cn:lambda:us-west-2
:123456789012
:function:StepFunctionsSample-BatchWithL-GenerateBatchJobMap-444455556666
",
"Effect": "Allow"
}
]
}
For information about how to configure IAM when using Step Functions with other Amazon services, see IAM Policies for integrated services.