Lambda and DynamoDB issues

Viewed 26

I have created a Java Lambda function which is meant to persist to DynamoDB table, deploying with AWS SAM. I observed there are no error logs from the persistence attempt, the Lambda just times out

    public static AmazonDynamoDB getDynamoInstance() {
    if (DYNAMO_INSTANCE == null) {
        AmazonDynamoDBClientBuilder amazonDynamoDBClientBuilder = AmazonDynamoDBClientBuilder.standard()
                .withRegion(REGION);
        DYNAMO_INSTANCE = amazonDynamoDBClientBuilder.build();
    }
    return DYNAMO_INSTANCE;
  }


     public void putItemInEventHistory(String event) {
     
 
     DynamoDB dynamoDB = new DynamoDB(DynamoDBFactory.getDynamoInstance());

     Table table = dynamoDB.getTable(EVENTS_HISTORY_TABLE_NAME);

     Item item = new Item()
                .withPrimaryKey("Id", 210)
                .withJSON("event", event);

    PutItemOutcome outcome = table.putItem(item);
       
    
 }

Could someone point me in the right direction to troubleshoot.

Cheers Kris

2 Answers

I tested the code and it works with no issue:

public class StackOverflow {
    static AmazonDynamoDB DYNAMO_INSTANCE;

    public static void main(String[] args) {

        DynamoDB dynamoDB = new DynamoDB(getDynamoInstance());

        Table table = dynamoDB.getTable("testing");

        Item item = new Item()
                .withPrimaryKey("Id", "210")
                .withJSON("event", "{ \"UserID1\": 0, \"UserID2\": 0, \"Message\": \"my message\", \"DateTime\": 0}");

        PutItemOutcome outcome = table.putItem(item);
    }


    public static AmazonDynamoDB getDynamoInstance() {

        if (DYNAMO_INSTANCE == null) {
            AmazonDynamoDBClientBuilder client = AmazonDynamoDBClientBuilder.standard()
                    .withRegion(Regions.EU_WEST_1);

            DYNAMO_INSTANCE = client.build();
        }
        return DYNAMO_INSTANCE;
    }

}
  • Is your Lambda logging anything? Make sure it has access to logs?
  • Does your Lambda have permission to write to DynamoDB?
  • Make sure your Lambda has an adequate timeout value
  • If you can log and have permissions, try adding some custom logs in the code to understand what is happening at each step.

it was a stupid mistake....used 'Id' instead of small letters 'id'. But the additional logs did help out

log4j.appender.com.amazonaws.services.dynamodbv2=DEBUG, stderr
Related