title: AWS Step Functions does not catch error when Lamba function returns an error
body: <p>I have been struggling with AWS Step Functions for hours now. The use case is quite simple as I want to get gradually familiar with AWS Step Functions. However, I think I do not understand how they handle errors that come back from a failed lambda function.</p>
  <p><a href="https://i.stack.imgur.com/P2wDh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P2wDh.png" alt="My State Machine" /></a></p>
  <p>Here is the corresponding code:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;Comment&quot;: &quot;A simple AWS Step Functions for managing users with in the context of the AWS Training Initiative at AXA.&quot;,
    &quot;StartAt&quot;: &quot;Process-All-Deletion&quot;,
    &quot;States&quot;: {
      &quot;Process-All-Deletion&quot;: {
        &quot;Type&quot;: &quot;Map&quot;,
        &quot;InputPath&quot;: &quot;$&quot;,
        &quot;ItemsPath&quot;: &quot;$.Users&quot;,
        &quot;MaxConcurrency&quot;: 0,
        &quot;Iterator&quot;: {
          &quot;StartAt&quot;: &quot;DeleteAccessKeys&quot;,
          &quot;States&quot;: {
            &quot;DeleteAccessKeys&quot;: {
              &quot;Type&quot;: &quot;Task&quot;,
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-central-1:###:function:listUserAccessKeys&quot;,
              &quot;Next&quot;: &quot;DetachUserPolicy&quot;,
              &quot;Catch&quot;: [
                {
                  &quot;ErrorEquals&quot;: [&quot;NoSuchEntityException&quot;],
                  &quot;ResultPath&quot;: &quot;$.DeleteAccessKeysError&quot;,
                  &quot;Next&quot;: &quot;CatchDeleteAccessKeysError&quot;
                }
              ]
            },
            &quot;DetachUserPolicy&quot;: {
              &quot;Type&quot;: &quot;Task&quot;,
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-central-1:###:function:detachUserPolicy&quot;,
              &quot;Next&quot;: &quot;DeleteIamUser&quot;,
              &quot;Catch&quot;: [
                {
                  &quot;ErrorEquals&quot;: [&quot;States.TaskFailed&quot;],
                  &quot;ResultPath&quot;: &quot;$.ErrorDescription&quot;,
                  &quot;Next&quot;: &quot;CatchDeleteUserPolicyError&quot;
                }
              ]
            },
            &quot;DeleteIamUser&quot;: {
              &quot;Type&quot;: &quot;Task&quot;,
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-central-1:###:function:deleteIamUser&quot;,
              &quot;End&quot;: true,
              &quot;Catch&quot;: [
                {
                  &quot;ErrorEquals&quot;: [&quot;States.TaskFailed&quot;],
                  &quot;ResultPath&quot;: &quot;$.ErrorDescription&quot;,
                  &quot;Next&quot;: &quot;CatchDeleteIamUserError&quot;
                }
              ]
            },
            &quot;CatchDeleteIamUserError&quot;: {
              &quot;Type&quot;: &quot;Task&quot;,
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-central-1:###:function:errorHandler&quot;,
              &quot;End&quot;: true
            },
            &quot;CatchDeleteAccessKeysError&quot;: {
              &quot;Type&quot;: &quot;Task&quot;,
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-central-1:###:function:errorHandler&quot;,
              &quot;Next&quot;: &quot;DetachUserPolicy&quot;
            },
            &quot;CatchDeleteUserPolicyError&quot;: {
              &quot;Type&quot;: &quot;Task&quot;,
              &quot;Resource&quot;: &quot;arn:aws:lambda:eu-central-1:###:function:errorHandler&quot;,
              &quot;Next&quot;: &quot;DeleteIamUser&quot;
            }
          }
        },
        &quot;ResultPath&quot;: &quot;$.Result&quot;,
        &quot;End&quot;: true
      }
    }
  }
  
  </code></pre>
  <p>So basically the state machine should catch the error properly and the status should be orange respectively 'caught error' in 'DeleteAccessKeys'. Instead it turns into green.</p>
  <p>This is the code of my lambda function:</p>
  <pre class="lang-py prettyprint-override"><code>
  import boto3 
  import botocore
  
  print('Loading deleteUserAccessKeys function...')
  
  def deleteUserAccessKeys(message, context):
  
      # Get IAM client
      client = boto3.client('iam')
      item = message['Name']
  
    
      try:
          # List all keys associated with the user
          result = client.list_access_keys(UserName=item)
          accessKeyIds = [accessKeyId for element['AccessKeyId'] in result['AccessKeyMetadata']]
          
          # Exit if there are no access keys
          if not accessKeyIds: return message
          
          # Delete all keys associated with the user
          for element in accessKeyIds:
              client.delete_access_key(
                  UserName=item,
                  AccessKeyId=element
              )
           
          message['DeletedAccessKeys']=len(accessKeyIds)
          
          print(message)
          return message
  
              
      except botocore.exceptions.ClientError as error:
          print(error.response)
          if error.response['Error']['Code'] == 'NoSuchEntity':
              print('Entity not found exception')
              raise error
          else:
              raise Exception(&quot;Failed! Check the error!&quot;)
      
    
  
  </code></pre>
  <p>What might be the issue or what did I wrongly configure?</p>