AWS PutItem not creating entries in DB

Issue

I’m trying to write a lambda function which writes an array of strings to a generic collection where the Primary key is the type of a collection. It should always replace the current item (if exists, otherwise create one) with a new one.

This is my handler function

func Handler(ctx context.Context) (Response, error) {
    item := lib.SkillsCollection{
        Collection: lib.SkillsCollectionName,
        Value: lib.Skills{
            "test",
            "test 2",
        },
    }

    log.Printf("Prepare item %v", item)

    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))
    svc := dynamodb.New(sess)

    log.Printf("Created session")

    av, err := dynamodbattribute.MarshalMap(item)

    if err != nil {
        log.Fatalf("Marshall error mapping value %s", err)
    }

    log.Printf("Marshalled item %v", av)

    input := &dynamodb.PutItemInput{
        TableName: aws.String(lib.DbCollectionsTable),
        Item:      av,
    }

    log.Println("Start scanning collections")

    _, err = svc.PutItem(input)

    if err != nil {
        log.Fatalf("Error during put %v", err)
    }

    var buf bytes.Buffer

    body, err := json.Marshal(map[string]interface{}{
        "message": "Go Serverless v1.0! Your function executed successfully!",
    })
    if err != nil {
        return Response{StatusCode: 404}, err
    }
    json.HTMLEscape(&buf, body)

    resp := Response{
        StatusCode:      200,
        IsBase64Encoded: false,
        Body:            buf.String(),
        Headers: map[string]string{
            "Content-Type": "application/json",
        },
    }

    return resp, nil
}

According to the logs everything is OK. No errors are thrown and I get response with status 200

These are the logs
enter image description here

However, after that I don’t see any items in the DynamoDB table. It’s empty. I can’t figure out where to look for the root of the issue. At the same time I can see that there were writes to the table.

enter image description here

I’ve already rewritten the solution to follow the example in the AWS docs but no luck with that.

The struct in case it matters

type Skills []string

type SkillsCollection struct {
    Collection string `dynamodbav:"collection" json:"collection"`
    Value      Skills `dynamodbav:"value" json:"value"`
}

And the DB setup config

CollectionsTable:
  Type: AWS::DynamoDB::Table
  Properties:
    AttributeDefinitions:
      - AttributeName: collection
        AttributeType: S
    KeySchema:
      - AttributeName: collection
        KeyType: HASH
    BillingMode: PAY_PER_REQUEST
    TableName: ${self:custom.collectionsTableName}

Solution

You haven’t demonstrated that there are no items in the table. Are you seeing that "Item count" of 0 and thinking it’s timely? Notice under the Item Summary header is says the count is only updated every 6 hours. Try hitting the "Get live item count" button and I bet you’ll see some items.

Why isn’t the item count continuously maintained? Because it’s a large distributed database which can store many trillions of items. Getting a live item count isn’t always a lightweight process.

Answered By – hunterhacker

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.