8

An Introduction to AWS Step Functions

 2 years ago
source link: https://mydeveloperplanet.com/2022/03/22/an-introduction-to-aws-step-functions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In this blog, you will get a short introduction into AWS Step Functions. You will also create a workflow from scratch with a lambda. The output of the lambda will be transformed so that it can be used in the workflow.

1. Introduction

AWS Step Functions is a low-code, visual workflow service in order to build applications. The goal is to focus on the business logic and to combine services with each other in a visual way. You can make use drag-and-drop in order to create a workflow and to integrate services. More information about AWS Step Functions can be found at the AWS website. The easiest way to discover how it looks like, is to create a workflow by yourself and that is exactly what you are going to do.

In the remainder of this blog, you will create an AWS Lambda which has been used in previous blogs. So, you do not need to develop the lambda yourself, you can just make use of it. Around this lambda you will create a workflow. During this exercise, you will encounter some problems which will be solved along the way.

2. Create Lambda

In this section, you will create the lambda which will be used in the workflow.

Navigate to the AWS Lambda service and click the Create Function button.

Choose Author from scratch, give the lambda the name MyStepFunctionsLambda and choose as runtime Java 11 (Corretto). Click the Create function button.

aws-step-functions-create-lambda-1.png?w=1024

Download the jar file of the lambda from the releases page at GitHub. In the Code source section, choose for .zip or .jar file from the Upload from dropdown box.

aws-step-functions-create-lambda-2.png?w=192

Upload the jar file and click the Save button.

aws-step-functions-create-lambda-3.png?w=817

In the Runtime settings section, click the Edit button. Fill the following text in the Handler input field.

com.mydeveloperplanet.myawslambdajavaplanet.LambdaJava::handleRequest

Click the Save button.

aws-step-functions-create-lambda-4.png?w=821

In order to verify whether the lambda functions properly, just click the Test button in the Test tab. As a response "Version 1" is returned. The lambda also can take a JSON as input and will log some data, but you will not use this functionality in this blog.

3. Create Workflow

In this section, you will create a basic workflow which will only invoke the lambda function.

Navigate to the Step Functions service and click the Create state machine button.

In Step 1, choose for Design your workflow visually and choose the Standard type. Click the Next button.

aws-step-functions-create-step-1.png?w=957

In Step 2, the visual workflow designer is shown.

aws-step-functions-create-step-2-1.png?w=1024

Drag the AWS Lambda Invoke component from the Actions menu to Drag first state here in the form.

On the right submenu Configuration, navigate to the API parameters section and choose from the dropdown list the MyStepFunctionsLambda as Function name. Click the Next button.

aws-step-functions-create-step-2-2.png?w=1024

In Step 3, the generated code can be reviewed. Besides creating the workflow by means of the visual designer, it is also possible to create the workflow by means of a JSON configuration file. It is less easy to follow the flow when the workflow starts getting a bit more complex, but for small workflows it is quite ok. Besides that, the JSON file can be put into version control which allows you to track the changes made to the workflow. Click the Next button.

aws-step-functions-create-step-3-1.png?w=940

In Step 4, give the workflow the name MyFirstStepFunctions, scroll to the bottom of the page and click the Create state machine button.

aws-step-functions-create-step-4.png?w=955

Click the Start execution button.

aws-step-functions-first-execution-1.png?w=901

A popup window is shown where you can enter some input for the workflow. Leave it as default and click the Start execution button.

aws-step-functions-first-execution-popup.png?w=1024

The workflow is executed and when the lambda is successful, it is marked as green. Click the Step output tab and notice that the expected lambda output is shown.

aws-step-functions-first-execution-succes.png?w=873

It might be that the following error occurs after executing the step function. Just, wait a bit and try a second time and probably it will work then.

Neither the global service principal states.amazonaws.com, nor the regional one is authorized to assume the provided role.

4. Add Choice State

The above workflow is quite simple, so let’s try to add a Choice after the lambda which will set the workflow to a success state when Version 1 is returned from the lambda and otherwise the workflow will be set to a failed state.

At the top of the page, click the Edit button.

aws-step-functions-edit.png?w=900

In the Edit screen, click the Workflow studio button.

aws-step-functions-edit-screen-1.png?w=894

In the left menu, select the Flow tab and drag the Choice component between the lambda and the End state.

aws-step-functions-add-choice.png?w=951

Next, drag the Success component in branch Rule #1 and drag the Fail component in branch Default.

aws-step-functions-add-success-fail.png?w=833

Select the Choice component and click the Edit icon in the Configuration tab next to Rule #1.

aws-step-functions-choice-configuration.png?w=499

The configuration for Rule #1 is expanded. Click the Add conditions button.

aws-step-functions-open-rule-1.png?w=470

In the popup window, the condition for Rule #1 can be configured. Here a problem arises in combination with the lambda you use. The lambda outputs a string as value, but the Variable for the condition must make use of a JsonPath. This means that the lambda should output a JSON instead of a string. Let’s assume for a moment that the lambda will output a version JSON parameter containing the text of the version and when the lambda outputs Version 1, the workflow should be successful. Click the Save conditions button.

aws-step-functions-rule-1-condition.png?w=1020

Next, click the Apply and exit button in order to save the workflow in the visual designer.

aws-step-functions-save-workflow.png?w=404

Strange thing is that after this, the workflow is not yet saved. So, do not forget to click the Save button in the workflow screen.

aws-step-functions-save-again.png?w=674

A warning is shown, click the Save anyway button.

aws-step-functions-warning-after-save.png?w=592

Start the execution of the workflow. The workflow fails, which is not directly visible from the workfow itself but in the event log below the workflow.

aws-step-functions-execution-choice-without-json.png?w=638

The error in the event log.

{
"error": "States.Runtime",
"cause": "An error occurred while executing the state 'Choice' (entered at the event id #7). Invalid path '$.version': The choice state's condition path references an invalid value."
}

5. Transform Lambda Output

Two options exist now in order to fix the problem with the lambda output:

  • Adapt the lambda in order that it outputs a JSON;
  • Transform the lambda output in the workflow.

The last option is chosen here, because when you have to change the code of the lambda each time you need something specific for a workflow, you do not have much benefit of a low-code platform.

Navigate to the Workfow Studio, select the lambda and select the Output tab. In this tab, an example is shown of the lambda task result. Copy the contents of this example.

aws-step-functions-taskresult-lambda.png?w=836

In the left menu, choose for Data flow simulator. With this tool it is possible to test the different transformations you want to create. Click the Task Result tab and paste the task result example into the Task result field.

aws-step-functions-data-flow-simulator.png?w=851

In the example, the payload is not the output of the lambda you are using. Therefore, change the payload from:

"Payload": {
"foo": "bar",
"colors": [
"red",
"blue",
"green"
],
"car": {
"year": 2008,
"make": "Toyota",
"model": "Matrix"
}
},

into:

"Payload": "Version 1",

Click the ResultSelector tab and paste the following transformation in the ResultSelector field.

{
"version.$": "$.Payload"
}

As can be seen in the fields at the bottom of the page, the transformation is exactly what is expected.

aws-step-functions-resultselector.png?w=844

Now that you know that the transformation is correct, navigate again to the workflow via the Workflow Studio. Select the lambda and the Output tab. Enable Transform result with ResultSelector and paste the transformation JSON into the field.

aws-step-functions-lambda-resultselector.png?w=495

Also, disable Filter output with OutputPath because the output is already in the correct format after the ResultSelector.

aws-step-functions-lambda-disable-outputpath.png?w=490

Save the workflow as you did before and execute the workflow. The success branch is now entered, just as expected.

aws-step-functions-lambda-choice-execution-success.png?w=634

More information about transformations can be found here.

6. Error Handling

As seen before, when something goes wrong during execution of the workflow, the workflow does not end gracefully. First, let’s fail the lambda. Navigate to the lambda service, select the lambda and edit the Runtime settings. Remove for example something from the Handler path. In the example below .LambdaJava is removed.

aws-step-functions-fail-lambda.png?w=820

Test the lambda and it will fail. Navigate to the workflow again and execute the workflow. The lambda fails, but the workflow does not end gracefully.

aws-step-functions-lambda-workflow-fail.png?w=608

Edit the workflow, select the lambda and select the Error Handling tab. Click the Add new catcher button.

aws-step-functions-configuration-error-handling.png?w=501

In the Errors field choose for States.ALL in order to catch all failures. In the Fallback state field choose for Add new state.

aws-step-functions-catcher.png?w=490

Drag a Fail component after the catch and give it the name Fail after error.

aws-step-functions-final-workflow.png?w=824

Save the workflow and execute it. The workflow now ends gracefully.

aws-step-functions-last-execution.png?w=610

7. Conclusion

In this blog, AWS Step Functions were introduced mainly by means of an example. The visualization and low-code functions will have their use cases but I do prefer plain coding than a tool like this. It can become quite complex when workflows become large and I doubt that you will have enough overview of what is happening where. Especially when you start transforming inputs and outputs in the workflow. So, it is a nice service of AWS but I do not think I will ever use this.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK