Poll for job status with Lambda and AWS Batch - AWS Step Functions

Poll for job status with Lambda and AWS Batch

This sample project creates an AWS Batch job poller. It implements an AWS Step Functions state machine that uses AWS Lambda to create a Wait state loop that checks on an AWS Batch job.

This sample project creates and configures all resources so that your Step Functions workflow will submit an AWS Batch job, and will wait for that job to complete before ending successfully.

Note

You can also implement this pattern without using a Lambda function. For information about controlling AWS Batch directly, see Integrating services with Step Functions.

This sample project creates the state machine, two Lambda functions, and an AWS Batch queue, and configures the related IAM permissions.

For more information about how AWS Step Functions can control other AWS services, see Integrating services with Step Functions.

Step 1: Create the state machine

  1. Open the Step Functions console and choose Create state machine.

  2. Type Job Poller in the search box, and then choose Job Poller from the search results that are returned.

  3. Choose Next to continue.

  4. Choose Run a demo to create a read-only and ready-to-deploy workflow, or choose Build on it to create an editable state machine definition that you can build on and later deploy.

    This sample project deploys the following resources:

    • Three Lambda functions to submit an AWS Batch job, get the current status of the submitted AWS Batch job, and the final job completion status.

    • An AWS Batch job

    • An AWS Step Functions state machine

    • Related AWS Identity and Access Management (IAM) roles

    The following image shows the workflow graph for the Job Poller sample project:

    Workflow graph of the Job Poller sample project.
  5. Choose Use template to continue with your selection.

Next steps depend on your previous choice:

  1. Run a demo – You can review the state machine before you create a read-only project with resources deployed by AWS CloudFormation to your AWS account.

    You can view the state machine definition, and when you are ready, choose Deploy and run to deploy the project and create the resources.

    Deploying can take up to 10 minutes to create resources and permissions. You can use the Stack ID link to monitor progress in AWS CloudFormation.

    After deploy completes, you should see your new state machine in the console.

  2. Build on it – You can review and edit the workflow definition. You might need to set values for placeholders in the sample project before attemping to run your custom workflow.

Note

Standard charges might apply for services deployed to your account.

Step 2: Run the state machine

After all the resources are provisioned and deployed, the Start execution dialog box is displayed with example input similar to the following.

{ "jobName": "my-job", "jobDefinition": "arn:aws:batch:us-east-2:123456789012:job-definition/SampleJobDefinition-343f54b445d5312:1", "jobQueue": "arn:aws:batch:us-east-2:123456789012:job-queue/SampleJobQueue-4d9d696031e1449", "wait_time": 60 }
Note

wait_time instructs the Wait state to loop every 60 seconds.

  • In the Start execution dialog box, do the following:

    1. (Optional) Enter a custom execution name to override the generated default.

      Non-ASCII names and logging

      Step Functions accepts names for state machines, executions, activities, and labels that contain non-ASCII characters. Because such characters will not work with Amazon CloudWatch, we recommend using only ASCII characters so you can track metrics in CloudWatch.

    2. (Optional) In the Input box, enter input values as JSON. You can skip this step if you are running a demo.

    3. Choose Start execution.

    The Step Functions console will direct you to an Execution Details page where you can choose states in the Graph view to explore related information in the Step details pane.

    For example, to view the changing status of your AWS Batch job and the looping results of your execution, choose the Output tab.

    The following image shows the execution status graph in the Graph view. It also shows the execution output for the selected step in the Output tab.

    Execution output for the selected step named Get Final Job Status in the Graph view.

Example State Machine Code

The state machine in this sample project integrates with AWS Lambda to submit an AWS Batch job. Browse through this example state machine to see how Step Functions controls Lambda and AWS Batch.

For more information about how AWS Step Functions can control other AWS services, see Integrating services with Step Functions.

{ "Comment": "An example of the Amazon States Language that runs an AWS Batch job and monitors the job until it completes.", "StartAt": "Submit Job", "States": { "Submit Job": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:111122223333:function:StepFunctionsSample-JobStatusPol-SubmitJobFunction-jDaYcl4cx55r", "ResultPath": "$.guid", "Next": "Wait X Seconds" }, "Wait X Seconds": { "Type": "Wait", "SecondsPath": "$.wait_time", "Next": "Get Job Status" }, "Get Job Status": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:111122223333:function:StepFunctionsSample-JobStatusPoll-CheckJobFunction-1JkJwY10vonI", "Next": "Job Complete?", "InputPath": "$.guid", "ResultPath": "$.status" }, "Job Complete?": { "Type": "Choice", "Choices": [ { "Variable": "$.status", "StringEquals": "FAILED", "Next": "Job Failed" }, { "Variable": "$.status", "StringEquals": "SUCCEEDED", "Next": "Get Final Job Status" } ], "Default": "Wait X Seconds" }, "Job Failed": { "Type": "Fail", "Cause": "AWS Batch Job Failed", "Error": "DescribeJob returned FAILED" }, "Get Final Job Status": { "Type": "Task", "Resource": "arn:aws::lambda:us-east-1:111122223333:function:StepFunctionsSample-JobStatusPoll-CheckJobFunction-1JkJwY10vonI", "InputPath": "$.guid", "End": true } } }