I solved an error showing Failed to convert ‘Body’ to string S3.InvalidContent arn:aws:states:::aws-sdk:s3:getObject in step function

2023.01.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Problem

I wrote a step function state machine that uses the AWS SDK to retrieve a file from S3. I input the bucket name and file name directly as the parameter.

{
"Comment": "A description of my state machine",
"StartAt": "GetAudioFromS3",
"States": {
"GetAudioFromS3": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject"
"Parameters": {
"Bucket": "BucketName",
"Key": "FileName.wav"
},
"End": true
}
}
}

When I run the task to get the file from S3, it fails with


S3.InvalidContent in step: Get Audio from S3

CAUSE

Failed to convert 'Body' to string After expanding and trying to check the root cause I got this TaskFailed event


{
"resourceType": "aws-sdk:s3",
"resource": "getObject",
"error": "S3.InvalidContent",
"cause": "Failed to convert 'Body' to string"
}

How I fixed it

I change the format of my step-function code and called the bucket using variable


{
"Comment": "A description of my state machine",
"StartAt": "GetAudioFromS3",
"States": {
"GetAudioFromS3": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject"
"Parameters": {
"Bucket.$": "$.S3BucketName",
"Key.$": "$.InputKey"
},
"End": true
}
}
}

This is the JSON definition of variable


{
"S3BucketName": "<name of the bucket where the original file is>",
"InputKey": "<name of the original file>"
}

When you are about to start the execution of the step function, it gives an option of inputting the JSON statement.

NOTE: This requires inputting every time you want to run an execution, so I used lambda for automation. Once the S3 bucket details are used in lambda.


{
"Comment": "A State Machine that process a video file",
"StartAt": "GetInput",
"States": {
"GetInput": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:********:function:FunctionName",
"Next": "GetAudioFromS3"
},
"GetAudioFromS3": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject"
"Parameters": {
"Bucket.$": "$.S3BucketName",
"Key.$": "$.InputKey"
},
"End": true
}
}
}