DynamoDB in step function is not able to put item with map field

0

I am trying to put an item at dynamodb via step function. I am having issue when the item includes a map field which contains a list. For example.

My step function

{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB PutItem",
  "States": {
    "DynamoDB PutItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:putItem",
      "Parameters": {
        "TableName": "test",
        "Item": {
          "ID": {
            "S.$": "$.input"
          },
          "Info": {
            "M.$": "$.info"
          }
        }
      },
      "End": true
    }
  }
}

My input to the step function

{
  "input": "Second",
  "info": {
    "name": "John",
    "age": "30",
    "cars": [
      "Ford",
      "BMW",
      "Fiat"
    ]
  }
}

The error I am recieving.

An error occurred while executing the state 'DynamoDB PutItem' (entered at the event id #2). The Parameters '{"TableName":"test","Item":{"ID":{"S":"3"},"Info":{"M":{"name":"John","age":"30","cars":["apple","pear"]}}}}' could not be used to start the Task: [Cannot deserialize value of type com.amazonaws.services.dynamodbv2.model.AttributeValue from Array value (token JsonToken.START_ARRAY)]

1 Antwort
0

Hey tj! The MAP type need to have each property also mapped to the right type (see this link). If the map has a well defined format, you can map each property at the PutItem.

If the format is unknown, my recommendation is to try the ExecuteStatement with a PartiQL statement as it accepts JSON like maps. Only note that you'll need to do some transformation to convert the double-quotes (") to single-quotes (').

I did the test here:

INSERT INTO "Teste" VALUE {
  'id': 'Second',
  'info': {
    'name': 'John',
    'age': '30',
    'cars': [
      'Ford',
      'BMW',
      'Fiat'
    ]
  }
}

and at the table I have:

{
 "id": {"S": "Second"},
 "info": {
  "M": {
   "age": {"S": "30"},
   "cars": {
    "L": [
     {"S": "Ford"},
     {"S": "BMW"},
     {"S": "Fiat"}
    ]
   },
   "name": {"S": "John"}
  }
 }
}
profile pictureAWS
Eduardo
beantwortet vor 2 Jahren
  • thank you for the reply, can you please post your step function json? I can't figure out how to insert the PartiQL query into ExecuteStatement

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen