DEV Community

Cover image for Failed to convert ‘Body’ to string S3.InvalidContent arn:aws:states:::aws-sdk:s3:getObject in step function
Olawale Adepoju for AWS Community Builders

Posted on • Originally published at dev.classmethod.jp

1

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

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
}
}
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
}
}
}
Enter fullscreen mode Exit fullscreen mode

This is the JSON definition of variable

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

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

Image description

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.

Image description

{
"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
}
}
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay