0
votes

I am creating a dynamodb table via CDK.

    const table = new dynamodb.Table(this, "my-table", {
      tableName: StackConfiguration.tableName,
      partitionKey: { name: "file_id", type: dynamodb.AttributeType.STRING },
    });

    dynamoReplayTable.addGlobalSecondaryIndex({
      indexName: "processed",
      # ideally would like boolean here but doesn't seem to be an option
      partitionKey: { name: "processed", type: dynamodb.AttributeType.STRING },
    });

Then using boto 3 i am inserting data into the table like so

    failedRecord = {
        "file_id": str(file_id),
        "processed": "false",
        "payload": str(payload),
        "headers": str(headers),
    }

    table.put_item(Item=failedRecord)

I then have another service that reads the items, processes then and i want to update the processed field which is a global secondary index to true.

I have this code at the minute

    table.update_item(
        Key={"file_id": file_id}, AttributeUpdates={"processed": "true"},
    )

But this results in the following error

Parameter validation failed:
Invalid type for parameter AttributeUpdates.processed, value: true, type: <class 'str'>, valid types: <class 'dict'>
2
Is this answer helpful? - Kasper
I had a look but that's for put item and I think because I don't specify a type when sending the data in it should default to string - AnonymousAlias
Have you tried using ` AttributeUpdates={"processed": {"S": "true"}}`? - Marcin
Yea when I try that i get [ERROR] ParamValidationError: Parameter validation failed: Unknown parameter in AttributeUpdates.processed: "S", must be one of: Value, Action - AnonymousAlias

2 Answers

4
votes

DynamoDB handles dat types in a very specific way about which you can find more info here and here.

In your case, the issue it's around the value "true" of your update command. Working with types can be tricky, boto3 provides both a TypeSerializer and TypeDeserializer which you can use to handle the conversion for you:

import boto3
from boto3.dynamodb.types import TypeSerializer

serializer = TypeSerializer()

my_single_value = "processed"

print(serializer.serialize(my_single_value))
# {'S': 'processed'}

my_dict_object = {
  "processed": "true"
}

print({k: serializer.serialize(v) for k, v in my_dict_object.items()})
# {'processed': {'S': 'true'}}
0
votes

Resolved this with the following code

    table.update_item(
        Key={"file_id": file_id},
        UpdateExpression="SET processed_status = :processed",
        ExpressionAttributeValues={":processed": "true"},
    )