<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yana Sapach on Medium]]></title>
        <description><![CDATA[Stories by Yana Sapach on Medium]]></description>
        <link>https://medium.com/@yana_s?source=rss-df7aebfd2b54------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*L6_E_8BsA6_Kn1u-M5_dqA.jpeg</url>
            <title>Stories by Yana Sapach on Medium</title>
            <link>https://medium.com/@yana_s?source=rss-df7aebfd2b54------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 14:58:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yana_s/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unlocking the Full Potential of AWS Step Functions: A Comprehensive Guide to Error Handling]]></title>
            <link>https://medium.com/provectus-articles/unlocking-the-full-potential-of-aws-step-functions-a-comprehensive-guide-to-error-handling-62f7c425d28c?source=rss-df7aebfd2b54------2</link>
            <guid isPermaLink="false">https://medium.com/p/62f7c425d28c</guid>
            <category><![CDATA[aws-step-functions]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[error-handling]]></category>
            <dc:creator><![CDATA[Yana Sapach]]></dc:creator>
            <pubDate>Fri, 16 Jun 2023 09:12:41 GMT</pubDate>
            <atom:updated>2023-06-16T09:12:41.027Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hjptBI-jKjlw7N8ZdDTiww.jpeg" /><figcaption>Image by <a href="https://www.freepik.com/author/pressranya">pressranya</a> from <a href="https://www.freepik.com/premium-photo/orange-black-wallpaper-with-black-background-gold-red-liquid-design_43976321.htm">freepik</a></figcaption></figure><p>Handling errors in an Amazon SageMaker pipeline is crucial for creating and sustaining dependable and resilient machine learning workflows. Nonetheless, addressing these errors can be extremely challenging.</p><p>A multitude of factors, including the intricacy of ML workflow, insufficient domain expertise, limited visibility and monitoring, lack of a comprehensive error management strategy, and fluctuating and evolving environments, can all affect error handling. These challenges can impede the efficient identification, resolution, and mitigation of errors, resulting in escalated costs, prolonged downtime, and diminished productivity. Surmounting them necessitates a methodical approach to error management that encompasses robust monitoring and well-defined error handling strategies that leverage the full potential of <em>SageMaker pipelines</em>.</p><p>There are plenty of resources available covering <a href="https://aws.amazon.com/step-functions/">AWS Step Functions</a>, <a href="https://aws.amazon.com/lambda/?nc1=h_ls">AWS Lambda</a>, and error handling. Here, we bring them together to explore and demonstrate various error handling approaches. From the simplest to the most complex, we discuss their advantages and drawbacks, and provide code samples to help you choose the best fit for your application.</p><p>By way of introduction, we provide a brief overview of AWS Step Functions and other related services. However, if you are already familiar with these services, feel free to skip ahead to our example projects. This article is designed to guide you step-by-step through the error handling process in these projects, with different approaches.</p><p>Whether you’re just starting out or you’re a seasoned developer looking to refine your skills, let’s begin by exploring the various<strong> error handling approaches in AWS Step Functions and AWS Lambda</strong>!</p><h3>AWS Step Functions Pipeline</h3><h4>Definition</h4><p>What is AWS Step Functions? Here is how <a href="https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html">AWS defines the service</a>:</p><blockquote><em>AWS Step Functions is a serverless orchestration service that lets you integrate with AWS Lambda functions and other AWS services to build business-critical applications.</em></blockquote><p>Simply put, AWS Step Functions is a service designed to enable engineers to build data processing pipelines with minimal effort: you simply write data processing code, split it into manageable steps, and rely on AWS for such orchestration tasks as scheduling, development, networking, scaling, health monitoring, and step management.</p><h4>How do we use the Pipeline?</h4><p>Our project, <a href="https://provectus.com/intelligent-document-processing/">a comprehensive intelligent document processing system</a>, uses many different modules: an OCR module to extract text from scanned documents; document classification, text preprocessing, and field extraction modules to extract relevant information from the documents; and, a post processing module to refine the extracted data. The idea of using a step-based solution seems advantageous, because this approach makes it easy to quickly adjust and change the processing graph. For example, to change the preprocessing module, you can add different kinds of preprocessing functions like paragraph separation and classification, as well as adapt the code to the business needs of the client with minimal effort.</p><p>In our ML project, we also use a backend integration step that sends the processed results to relevant systems or applications where they are needed for further utilization. This step can vary for different clients — and this split-code method allows us to reuse the same components in other projects.</p><p>Every step of your pipeline is either a codeless step or a Lambda step — both of them are called “<strong>Tasks</strong>”. In most cases, you will work with Lambda steps, as they contain the core code of your project, but sometimes there is no need for a separate Lambda step, as is the case with some error handling or run’s state steps. Tracking the pipeline’s state and managing the processing pipeline become easier with AWS Step Functions’ built-in tools. By utilizing discrete components with a contract in place, we can modify the data processing pipeline to accommodate clients’ needs.</p><p>Here is an example of what an ML Step Functions pipeline may look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_wkF-fvscl2bMjf2" /><figcaption>Figure 1 — AWS Step Functions pipeline for ML project</figcaption></figure><p>Basically, this is a visualization of a State Machine — a representation of the pipeline. It is written in an easy-to-use JSON-based <a href="https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html">Amazon States Language</a>. The visualization helps us to see the connections between all steps and check whether or not these connections are correct and completed.</p><p>Let’s build a simple and straightforward Step Functions pipeline that will serve as a foundation for understanding error handling techniques and strategies in Step Functions workflows.</p><h3>Building an AWS Step Functions Pipeline</h3><p>If we access the AWS console to create an AWS Step Functions pipeline, we will need to start with some code, like this:</p><pre>{<br>   &quot;Comment&quot;:&quot;An Error-Handling example of the Amazon States Language&quot;,<br>   &quot;StartAt&quot;:&quot;Inference&quot;,<br>   &quot;States&quot;:{<br>   &quot;Inference&quot;:{<br>      &quot;Type&quot;:&quot;Task&quot;,<br>      &quot;Resource&quot;:&quot;arn:aws:states:::lambda:invoke&quot;,<br>      &quot;OutputPath&quot;:&quot;$.Payload&quot;,<br>      &quot;Parameters&quot;:{<br>         &quot;Payload.$&quot;:&quot;$&quot;,<br>&quot;FunctionName&quot;:&quot;arn:aws:lambda:REGION:ACCOUNT_ID:function:LAMBDA_NAME&quot;<br>      },<br>      &quot;End&quot;:true<br>   }<br>   }<br>}</pre><p>Here is the structure of the pipeline that we will get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zClxQZNKTM90HsoB" /><figcaption>Figure 2 — Simplified structure of the AWS Step Functions pipeline</figcaption></figure><p>The pipeline can contain more than one Lambda function, but for the sake of simplicity, let’s start with a single Lambda. Now, let’s proceed to the error handling part.</p><h3>AWS Error Handling</h3><p>First of all, why not simply have a try-except block inside Lambda? Let’s take a close look at some of the disadvantages of this fast solution.</p><ol><li><strong>Increased complexity:</strong> If you include try-except blocks in every Lambda function, you will have to handle errors in each function separately. This can lead to increased complexity and make it harder to understand and maintain the code.</li><li><strong>Limited error handling:</strong> Try-except blocks only handle errors that occur within the function where they are defined. If an error occurs further up the pipeline, in the Step Function itself, or in another AWS service that is being used, the try-except blocks within the Lambda functions will not catch it. By contrast, error handling at the Step Function level enables you to handle errors that occur anywhere in the pipeline.</li><li><strong>Decentralized error handling:</strong> By handling errors at the Step Function level, you can centralize error handling and make it easier to manage errors across your pipeline. For example, you can set up a catch-all error handler that sends an email or logs an error message when an error occurs. Regardless of the changes in the error handling logic, you will always know where your code is located in the project, and it will have clean architecture that will be easy to test.</li><li><strong>Downgraded visibility:</strong> If you handle errors at the Step Function level, you can more easily see where errors are occurring in your pipeline and what is causing them. This can help you identify and fix problems more quickly. Putting error handling code inside the Lambda with business logic reduces error visibility.</li></ol><p>Let’s explore how Amazon SageMaker provides powerful capabilities to handle errors outside of the main Lambda steps of the ML pipeline, ensuring a more comprehensive and robust error handling strategy that covers the entire pipeline workflow.</p><h4>I. “Retry” block</h4><p>To enhance the reliability and robustness of AWS Step Functions pipelines, AWS has introduced a built-in retry mechanism that automatically retries failed steps — “Retry.” It is obvious that AWS works in the real world, and some transient service errors may sometimes occur.</p><p>The user deals with errors like “<strong>Lambda.ServiceException</strong>”, “<strong>Lambda.AWSLambdaException</strong>”, or “<strong>Lambda.SdkClientException</strong>”. For errors like these, AWS <a href="https://docs.aws.amazon.com/step-functions/latest/dg/bp-lambda-serviceexception.html">recommends</a> using the “Retry” block, to call your Lambda function again, like this:</p><pre>&quot;Retry&quot;: [<br>  {<br>    &quot;ErrorEquals&quot;: [<br>      &quot;Lambda.ServiceException&quot;,<br>      &quot;Lambda.AWSLambdaException&quot;,<br>      &quot;Lambda.SdkClientException&quot;<br>    ],<br>    &quot;IntervalSeconds&quot;: 2,<br>    &quot;MaxAttempts&quot;: 6,<br>    &quot;BackoffRate&quot;: 2<br>  }<br>]</pre><p>Some parameters that can be specified are the number of attempts (“MaxAttempts”), the pause between them (“IntervalSeconds”), and the growth rate of the interval between attempts (“BackoffRate”).</p><p>Once this code-block is added to the pipeline, it can look like this:</p><pre>{<br>  &quot;Comment&quot;: &quot;An Error-Handling example of the Amazon States Language&quot;,<br>  &quot;StartAt&quot;: &quot;Inference&quot;,<br>  &quot;States&quot;: {<br>    &quot;Inference&quot;: {<br>      &quot;Type&quot;: &quot;Task&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,<br>      &quot;OutputPath&quot;: &quot;$.Payload&quot;,<br>      &quot;Parameters&quot;: {<br>        &quot;Payload.$&quot;: &quot;$&quot;,<br>        &quot;FunctionName&quot;: &quot;arn:aws:lambda:REGION:ACCOUNT_ID:function:LAMBDA_NAME&quot;<br>      },<br>      &quot;Retry&quot;: [<br>        {<br>          &quot;ErrorEquals&quot;: [<br>            &quot;Lambda.ServiceException&quot;,<br>            &quot;Lambda.AWSLambdaException&quot;,<br>            &quot;Lambda.SdkClientException&quot;<br>          ],<br>          &quot;IntervalSeconds&quot;: 2,<br>          &quot;MaxAttempts&quot;: 6,<br>          &quot;BackoffRate&quot;: 2<br>        }<br>      ],<br>      &quot;End&quot;: true<br>    }<br>  }<br>}</pre><h4>II. “Catch” block</h4><p>Most of the time errors do not just disappear, so we need to stop the execution. In this case, AWS <a href="https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-handling-error-conditions.html">offers</a> a “<strong>Catch</strong>” block:</p><pre>&quot;Catch&quot;: [<br>  {<br>    &quot;ErrorEquals&quot;: [<br>      &quot;States.ALL&quot;<br>    ],<br>    &quot;Next&quot;: &quot;Error Handling&quot;<br>  }<br>]</pre><p>The next question is, what does an Error Handling handler look like?</p><h4>III. “Pass” step for error handling</h4><p>If additional actions are not required after the error has occurred, then the handler can simply return an explanation message, like this:</p><pre>&quot;Error Handling&quot;: {<br>  &quot;Type&quot;: &quot;Pass&quot;,<br>  &quot;Result&quot;: &quot;This is a fallback from the code. Fix it!&quot;,<br>  &quot;End&quot;: true<br>}</pre><p>The “<a href="https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-pass-state.html"><strong>Pass</strong></a>” type is a simplified step in the pipeline that gets some JSON as input and sends some JSON as output. It can be either a fixed JSON, or some input parts, or a combination of both. Bear in mind that we cannot do more complex operations or extract only specific parts of the field, only the entire JSON.</p><p>As to our case of handling errors, JSON comes with the fields “<em>Error</em>” — containing only the type of error, and “<em>Cause</em>” — a string with a stack trace and an error message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gsV61zl3cbzxxBZp" /><figcaption><em>Figure 3 — Execution of the StepFunction pipeline with an error in the “Inference” step</em></figcaption></figure><p>The error displayed in the image above was caught and sent as an input to the “Error Handling” step. The Input and output of the “Error Handling” step in that run is on the right. The output is a simple string that will not change for any input data.</p><p>It is also possible to save the input of a failed Lambda function; then, in addition to the error, the incoming data will be received. “<strong>ResultPath</strong>” will specify the path to the error in JSON, and the “catch” block will then look like this:</p><pre>&quot;Catch&quot;: [<br>  {<br>    &quot;ErrorEquals&quot;: [<br>      &quot;States.ALL&quot;<br>    ],<br>    &quot;ResultPath&quot;: &quot;$.exception&quot;,<br>    &quot;Next&quot;: &quot;Error Handling&quot;<br>  }<br>]</pre><h4>IV. “Task” step for error handling</h4><p>To do any additional tasks when an error occurs, we need to create a Lambda function — an error handler. It will be similar to a standard Lambda pipeline step, like this:</p><pre>&quot;Error Handling&quot;: {<br>  &quot;Type&quot;: &quot;Task&quot;,<br>  &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,<br>  &quot;OutputPath&quot;: &quot;$.Payload&quot;,<br>  &quot;Parameters&quot;: {<br>    &quot;Payload.$&quot;: &quot;$&quot;,<br>    &quot;FunctionName&quot;: &quot;arn:aws:lambda:REGION:ACCOUNT_ID:function:ERROR_HANDLER_LAMBDA_NAME&quot;<br>  },<br>  &quot;Retry&quot;: [<br>    {<br>      &quot;ErrorEquals&quot;: [<br>        &quot;Lambda.ServiceException&quot;,<br>        &quot;Lambda.AWSLambdaException&quot;,<br>        &quot;Lambda.SdkClientException&quot;<br>      ],<br>      &quot;IntervalSeconds&quot;: 2,<br>      &quot;MaxAttempts&quot;: 6,<br>      &quot;BackoffRate&quot;: 2<br>    }<br>  ],<br>  &quot;End&quot;: true<br>}</pre><p>Now, let’s write some simple code for our error handler:</p><pre>import json<br><br>ERROR_KEY = &#39;exception&#39;<br><br>def lambda_handler(event, context):<br>    exception = event.get(ERROR_KEY, None)<br>    if exception is None:<br>        error = &#39;No error information provided.&#39;<br>    else:<br>        error = &#39;Document cannot be processed.&#39;<br>    return {<br>        &#39;status&#39;: &#39;ERROR&#39;,<br>        &#39;error_message&#39;: error,<br>        &#39;error&#39;: exception.get(&#39;Error&#39;),<br>        &#39;error_description&#39;: exception.get(&#39;Cause&#39;)<br>    }</pre><p>With the help of that Lambda we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*du1s1GzMqtLCG77e" /><figcaption><em>Figure 4 — Execution of the StepFunction pipeline with an error in the “Inference” step.</em></figcaption></figure><p>This error was caught and sent as an input to the “Error Handling” step. The output of the “Error Handling” step in that run is on the right. This output has a “<em>status</em>” field and clearer naming for the error description field, so it can potentially be sent to any backend and be processed correctly.</p><p>This handler can log the information to any logger service and prepare a more complicated response (as a part of the input JSON). The handler itself can do whatever you need.</p><h4>V. Multiple error handlers</h4><p>Imagine that you are running a system that handles orders for an ecommerce website. When you are dealing with orders, there are a few things that can go wrong.</p><p>First, there are errors that can happen because of problems with the order itself. For example, a customer may have filled in an incorrect address or tried to order a product that is no longer available (“<strong>OrderError</strong>”).</p><p>The payment process can also return errors. For example, the customer’s credit card may be declined, or there may be an issue with how the payment is processed (“<strong>PaymentError</strong>”).</p><p>In the “Catch” block, then, we can specify different handlers for different errors:</p><pre>&quot;Catch&quot;: [<br>  {<br>    &quot;ErrorEquals&quot;: [<br>      &quot;OrderError&quot;<br>    ],<br>    &quot;Next&quot;: &quot;Order errors Handling&quot;<br>  },<br> {<br>    &quot;ErrorEquals&quot;: [<br>      &quot;PaymentError&quot;<br>    ],<br>    &quot;Next&quot;: &quot;Payment errors Handling&quot;<br>  },<br>  {<br>    &quot;ErrorEquals&quot;: [<br>      &quot;States.ALL&quot;<br>    ],<br>    &quot;Next&quot;: &quot;Other Error Handling&quot;<br>  }<br>]</pre><p>The Other Error Handling handler catches any type of errors; if we want to handle a certain type differently (e.g. PaymentError, OrderError), a special handler <a href="https://docs.aws.amazon.com/step-functions/latest/dg/concepts-error-handling.html#error-handling-fallback-states">should be placed above</a> <strong>States.ALL</strong> — otherwise, the run simply will not reach it.</p><p>For that reason, the State Machine graph should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*saA4GcdcUaWPG-j1" /><figcaption><em>Figure 5 — Structure of the AWS Step Functions pipeline with multiple error handlers.</em></figcaption></figure><p>If for all types of errors we need a common handling component (logs or alerts/notifications), we can build a pipeline, as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7l9Mi63-6sOmdVQV" /><figcaption><em>Figure 6 — Structure of the AWS Step Functions pipeline with multiple error handlers.</em></figcaption></figure><p>As shown in the image above, these handlers have a separate common component, but the split adds visibility to the pipeline and helps to avoid duplication.</p><p>Using separate error handlers for different errors in an AWS Step Functions pipeline allows for more fine-grained error handling, enabling specific and tailored error handling strategies for each type of error. This approach allows for better error resolution, improved error tracking, and more efficient error handling, resulting in a more robust and reliable project workflow.</p><h4>VI. State Machine status transparency and complex pipelines</h4><p>Returning to our model inference pipeline in Amazon SageMaker, there is almost always a backend or a third-party integration that should be notified about both successful and failed runs of the model. When we decide to do this, “Error Handling” is no longer the last step, so we replace “End”: true with “Next”: “Notifying”.</p><p>The pipeline can look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ff-7gwIhnURVIrJR" /><figcaption><em>Figure 7 — Structure of the AWS Step Functions pipeline with a step for backend notification.</em></figcaption></figure><p>Let’s see how it looks in the case of a successful run and an error run:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/0*VNQXyc7-MDtunyWz" /><figcaption><em>Figure 8 — Successfully executed SageMaker pipeline.</em></figcaption></figure><p>If the run is successful, it means that no errors appeared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9lbDaB3czGa-l6-U" /><figcaption><em>Figure 9 — Unsuccessfully executed SageMaker pipeline.</em></figcaption></figure><p>The error appeared in “Inference” and was successfully processed in the “Error Handling” step.</p><p>The pipeline has worked as expected: if no errors appear in the pipeline run, the “Error Handling” step is not used and the results from the “Inference” step go straight to the “Notifying” step. But when an error occurs, the “Error Handling” step does its job, processes errors and sends the results to the next “Notifying” step, where any (both successful and unsuccessful) results of the pipeline run will be sent to the backend.</p><p>So, what do we end up with in the history of the run? An error crept into the pipeline, but the processing status was still successful. Why?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mCLyedZ00-YNV85w" /></figure><p>When an error is successfully processed, a “Fail” status is not set. We can fix this by adding the “Fail” state to the “Error” step, but in that case the error step <strong>should be the last step in the pipeline</strong>. There is no way we can send a notification in a separate step.</p><p>The first solution comes immediately — to duplicate the entire pipeline after the error, and at the end force it to return the “Fail” status. It can look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yOIA4ZpP8HV7e_ao" /><figcaption><em>Figure 10 — Structure of the AWS Step Functions pipeline with duplicated “Notifying” step.</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TytrJHY8Cef-fEVo" /><figcaption><em>Figure 11 — Successfully executed SageMaker pipeline.</em></figcaption></figure><p>In this case, no errors appear. The step “Notifying Succeed” is used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7vSty6KEzW-39jGi" /><figcaption><em>Figure 12 — Unsuccessfully executed SageMaker pipeline.</em></figcaption></figure><p>An error has appeared in “Inference” and is successfully processed in the “Error Handling” step. The step “Notifying Fail” is used and the status “Fail” is set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*K-1YWCnl6kO6jhYe" /></figure><p>This time the status of the pipeline execution will better correspond to reality, and we will notice the problematic run at first glance.</p><p>If there are several steps planned, however, the parallel branch will continue to grow; with any changes, you will need to remember to add them in parallel branches as well. This approach can lead to many errors in the future. Which option is better? AWS and its states are the answer.</p><h3>Type “Choice”</h3><p>We can solve our problem with the “<strong>Choice</strong>” step. This pipeline step enables us to compare the input with the expected value and choose the next pipeline step, depending on the result of this comparison. Let’s try to use it:</p><pre>&quot;State Choice&quot;: {<br>  &quot;Type&quot;: &quot;Choice&quot;,<br>  &quot;Choices&quot;: [<br>    {<br>      &quot;Variable&quot;: &quot;$.status&quot;,<br>      &quot;StringEquals&quot;: &quot;ERROR&quot;,<br>      &quot;Next&quot;: &quot;State Fail&quot;<br>    }<br>  ],<br>  &quot;Default&quot;: &quot;State Succeed&quot;<br>},<br>&quot;State Fail&quot;: {<br>  &quot;Type&quot;: &quot;Fail&quot;<br>},<br>&quot;State Succeed&quot;: {<br>  &quot;Type&quot;: &quot;Succeed&quot;<br>}</pre><p>The pipeline can look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Islkp6xZ1IDKZIn3" /><figcaption><em>Figure 13 — Structure of the AWS Step Functions pipeline with the “Choice” step for selecting the next pipeline step, depending on the output of the “Notifying” step.</em></figcaption></figure><p>In other pipeline running scenarios, we obtain the following results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/0*iEHZpcAd8btmZc54" /><figcaption><em>Figure 14 — Successfully executed SageMaker pipeline.</em></figcaption></figure><p>No errors have appeared. The State “Succeed” step was used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6fqmKXNvHlECpbOz" /><figcaption><em>Figure 15 — Unsuccessfully executed SageMaker pipeline.</em></figcaption></figure><p>An error has appeared in “Inference” and was successfully processed in the “Error Handling” step. The “State Fail” step was used.</p><p>Execution pipelines for successful and unsuccessful cases use the same steps for the same tasks, so that duplication is avoided.</p><p>Now, we can easily locate the problematic run in the history list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hsMKDAVomz6eDqSk" /></figure><p>This is exactly what we wanted!</p><p>The full code for the built pipeline:</p><pre>{<br>  &quot;Comment&quot;: &quot;An Error-Handling example of the Amazon States Language&quot;,<br>  &quot;StartAt&quot;: &quot;Inference&quot;,<br>  &quot;States&quot;: {<br>    &quot;Inference&quot;: {<br>      &quot;Type&quot;: &quot;Task&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,<br>      &quot;OutputPath&quot;: &quot;$.Payload&quot;,<br>      &quot;Parameters&quot;: {<br>        &quot;Payload.$&quot;: &quot;$&quot;,<br>        &quot;FunctionName&quot;: &quot;arn:aws:lambda:REGION:ACCOUNT_ID:function:LAMBDA_NAME&quot;<br>      },<br>      &quot;Retry&quot;: [<br>        {<br>          &quot;ErrorEquals&quot;: [<br>            &quot;Lambda.ServiceException&quot;,<br>            &quot;Lambda.AWSLambdaException&quot;,<br>            &quot;Lambda.SdkClientException&quot;<br>          ],<br>          &quot;IntervalSeconds&quot;: 2,<br>          &quot;MaxAttempts&quot;: 6,<br>          &quot;BackoffRate&quot;: 2<br>        }<br>      ],<br>      &quot;Catch&quot;: [<br>        {<br>          &quot;ErrorEquals&quot;: [<br>            &quot;States.ALL&quot;<br>          ],<br>          &quot;ResultPath&quot;: &quot;$.exception&quot;,<br>          &quot;Next&quot;: &quot;Error Handling&quot;<br>        }<br>      ],<br>      &quot;Next&quot;: &quot;Notifying&quot;<br>    },<br>    &quot;Notifying&quot;: {<br>      &quot;Type&quot;: &quot;Task&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,<br>      &quot;ResultPath&quot;: null,<br>      &quot;Parameters&quot;: {<br>        &quot;Payload.$&quot;: &quot;$&quot;,<br>        &quot;FunctionName&quot;: &quot;arn:aws:lambda:REGION:ACCOUNT_ID:function:NOTIFY_LAMBDA_NAME&quot;<br>      },<br>      &quot;Retry&quot;: [<br>        {<br>          &quot;ErrorEquals&quot;: [<br>            &quot;Lambda.ServiceException&quot;,<br>            &quot;Lambda.AWSLambdaException&quot;,<br>            &quot;Lambda.SdkClientException&quot;<br>          ],<br>          &quot;IntervalSeconds&quot;: 2,<br>          &quot;MaxAttempts&quot;: 6,<br>          &quot;BackoffRate&quot;: 2<br>        }<br>      ],<br>      &quot;Next&quot;: &quot;State Choice&quot;<br>    },<br>    &quot;Error Handling&quot;: {<br>      &quot;Type&quot;: &quot;Task&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,<br>      &quot;OutputPath&quot;: &quot;$.Payload&quot;,<br>      &quot;Parameters&quot;: {<br>        &quot;Payload.$&quot;: &quot;$&quot;,<br>        &quot;FunctionName&quot;: &quot;arn:aws:lambda:REGION:ACCOUNT_ID:function:ERROR_HANDLER_LAMBDA_NAME&quot;<br>      },<br>      &quot;Retry&quot;: [<br>        {<br>          &quot;ErrorEquals&quot;: [<br>            &quot;Lambda.ServiceException&quot;, <br>            &quot;Lambda.AWSLambdaException&quot;,<br>            &quot;Lambda.SdkClientException&quot;<br>          ],<br>          &quot;IntervalSeconds&quot;: 2,<br>          &quot;MaxAttempts&quot;: 6,<br>          &quot;BackoffRate&quot;: 2<br>        }<br>      ],<br>      &quot;Next&quot;: &quot;Notifying&quot;<br>    },<br>    &quot;State Choice&quot;: {<br>      &quot;Type&quot;: &quot;Choice&quot;,<br>      &quot;Choices&quot;: [<br>        {<br>          &quot;Variable&quot;: &quot;$.status&quot;,<br>          &quot;StringEquals&quot;: &quot;ERROR&quot;,<br>          &quot;Next&quot;: &quot;State Fail&quot;<br>        }<br>      ],<br>      &quot;Default&quot;: &quot;State Succeed&quot;<br>    },<br>    &quot;State Fail&quot;: {<br>      &quot;Type&quot;: &quot;Fail&quot;<br>    },<br>    &quot;State Succeed&quot;: {<br>      &quot;Type&quot;: &quot;Succeed&quot;<br>    }<br>  }<br>}</pre><h3>Conclusion</h3><p>Error handling is a critical aspect of building robust and reliable applications in AWS. With AWS Step Functions and AWS Lambda, you have powerful tools at your disposal to implement effective error handling strategies.</p><p>This article explores various error handling approaches, from the simplest to the most complex, and provides code samples to help you get started. By selecting the best approach for your application, you can ensure that errors are handled effectively, minimizing their impact on your application’s functionality.</p><p>Let’s recap the main findings:</p><ul><li>In AWS Step Functions, you can <strong>work with errors</strong> in different ways, depending on the desired result:</li></ul><ol><li>Retry: for network errors or internal AWS errors where the error may disappear in a short time</li><li>Pass: for errors that do not need additional processing and have a static result</li><li>A separate Task Lambda function: for more complex error handling options.</li></ol><ul><li><strong>Fail state should always come last</strong> — no other step can be run after it</li><li>To <strong>avoid duplication</strong>, you can use the built-in features of AWS, in particular the “Choice” state</li><li>It is recommended to build the pipeline in accordance with<a href="https://docs.aws.amazon.com/step-functions/latest/dg/sfn-best-practices.html"> AWS Step Functions best practices</a>.</li></ul><p>We hope this article has been informative and useful in helping you <a href="https://provectus.com/aws-cost-optimization/">build more robust and cost-efficient applications on AWS</a>. If you have any questions or suggestions, feel free to reach out to the <strong>Provectus</strong> team!</p><p><a href="https://www.linkedin.com/in/yana-sapach/"><strong>Yana Sapach</strong></a> is a <strong>Machine Learning Engineer</strong> at Provectus. As a specialist in the field with over 3 years of experience, she has worked on various computer vision projects that required expertise in developing and implementing models and ML algorithms for reconstruction and localization problems. She has also gained proficiency in her latest interest — intelligent document processing for financial and healthcare sectors. Yana holds a Master’s Degree in Computer Science, which has equipped her with a robust foundation of technical skills that she continually enhances through her work. Her passion for using her expertise to overcome complex problems and deliver top-quality results is the hallmark of her success.</p><p>The article is written in close collaboration with <a href="https://www.linkedin.com/in/rinat-akhmetov/"><strong>Rinat Akhmetov</strong></a><strong>, Senior Machine Learning Engineer.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62f7c425d28c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/provectus-articles/unlocking-the-full-potential-of-aws-step-functions-a-comprehensive-guide-to-error-handling-62f7c425d28c">Unlocking the Full Potential of AWS Step Functions: A Comprehensive Guide to Error Handling</a> was originally published in <a href="https://medium.com/provectus-articles">Provectus Articles</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>