Skip to content

Output isn't being set on Catch

0

My task (which exists in side a Map) goes to get a JSON object from S3 based on the assigned accountId variable. If it exists the result is assigned to existingImages. If it doesn't exist then I am assigning the existingImages variable as an empty array. And in either case the output should be the step input.

The variable assignment is working as expected, but no matter what I try the output is the error result.

"GetObject": {
  "Type": "Task",
  "Arguments": {
    "Bucket": "bucketname",
    "Key": "{% 'images/accounts/' & $accountId & '.json' %}"
  },
  "Resource": "arn:aws:states:::aws-sdk:s3:getObject",
  "Next": "update or create",
  "Output": "{% $states.input %}",
  "Assign": {
    "existingImages": "{% $parse($states.result.Body) %}"
  },
  "Catch": [
    {
      "ErrorEquals": [
        "S3.NoSuchKeyException"
      ],
      "Next": "update or create",
      "Assign": {
        "existingImages": []
      },
      "Output": "{% $states.input %}"
    }
  ]
}

Actual output:

{
    "error": "S3.NoSuchKeyException",
    "cause": "The specified key does not exist. (Service: S3, Status Code: 404, Request ID: 5R5N0A0AJKGCVG47, Extended Request ID: 96myRqbx3f+8Rdz4xUlcLnYNOi85bcu+/G8VGTTaYW+Ncw0hR1gSIeuzEFZBpnefez6b0q7EIu1JfnRvp5RBdA==) (SDK Attempt Count: 1)"
}

Is this not a valid approach? Or is there an error in how I'm implementing this? I'm testing this through "Test State".

asked a year ago151 views

2 Answers
0
  1. Remove the "Output" field from both the main task and the "Catch" block.
  2. Add a "ResultPath" field to the main task set to null.
  3. Add a "ResultPath" field to the "Catch" block set to null.

Here's the modified task definition:

"GetObject": {
  "Type": "Task",
  "Arguments": {
    "Bucket": "bucketname",
    "Key": "{% 'images/accounts/' & $accountId & '.json' %}"
  },
  "Resource": "arn:aws:states:::aws-sdk:s3:getObject",
  "Next": "update or create",
  "ResultPath": null,
  "Assign": {
    "existingImages": "{% $parse($states.result.Body) %}"
  },
  "Catch": [
    {
      "ErrorEquals": [
        "S3.NoSuchKeyException"
      ],
      "Next": "update or create",
      "ResultPath": null,
      "Assign": {
        "existingImages": []
      }
    }
  ]
}

By setting "ResultPath" to null in both the main task and the "Catch" block, you're telling Step Functions to discard the result of the S3 GetObject operation (or the error in case of a failure) and keep the input as the output. This ensures that $states.input is passed along to the next state, regardless of whether the S3 object exists or not.

The "Assign" operations will still work as expected, setting the existingImages variable in both cases. This approach should resolve your issue and allow the step input to be passed along as the output, while still assigning the correct value to existingImages based on whether the S3 object exists or not.

Sources
Why Does S3 Return 403 Instead of 404 When the Object Doesn’t Exist? | AWS re:Post
Learn the basics of Amazon S3 with an AWS SDK - Amazon Simple Storage Service

answered a year ago

AWS
SUPPORT ENGINEER

reviewed a year ago

  • Honestly a pretty high-quality generative response! But "field 'ResultPath' is only supported for the 'JSONPath' QueryLanguage" and (as you can see from the {% %} designators) this step is using JSONata.

0

I think your code is assigning the value correctly but you are not using the value. The assigned value will be available in the next step. Can you confirm if the the value is appearing correctly or not in the next step.

EXPERT

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.