amazon-web-servicesrustaws-lambdaaws-sdk-rust

Serialize rusoto_dynamodbstream::AttributeValue to JSON


I'm creating a Rust script that get from event the DynamoDB Streams data and save it to a S3 Bucket.

After retrieving the data from the event body, I cannot convert the data to JSON because AttributeValue is not Serializable.

How can I do it?

Here below my not working code:

use lambda_runtime::{run, service_fn, Error, LambdaEvent};
use serde::{Deserialize, Serialize, Serializer};
use rusoto_dynamodbstreams::{Record, StreamRecord, AttributeValue};

#[derive(Deserialize)]
pub struct Payload {
    #[serde(rename = "Records")]
    records: Vec<Record>,
}

async fn function_handler(event: LambdaEvent<Payload>) -> Result<(), Error> {
    // useless code that init s3 client and gzip stream
    for record in event.payload.records {
        let old_image = record.dynamodb.unwrap().old_image.unwrap().to_owned();
        // here the code will break
        let json = serde_json::to_string(&old_image).unwrap();

        stream.write_all(json.as_bytes()).unwrap();
    }
    // ...
}


Solution

  • The Serialize implementations are gated behind the feature serialize_structs so all you have to do is to enable it:

    [dependencies]
    rusoto_dynamodbstreams = { version = "*", features = [ "serialize_structs" ] }