Sumera
Sumera

Reputation: 215

Items are not added in dynamodb using aws toolkit for .net

I have created a profile in aws toolkit for .net and I am creating a lambda function by uploading the aws function. The function when tested in aws console does not throw any error. However , the data is not added in dynamodb table.

Here is the code snippet :

 public void FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context1)
    {
        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        var context = new DynamoDBContext(client);

        Table awsnet = Table.LoadTable(client, "bookmaster");
        context1.Logger.LogLine("In method : Function Handler : start");
        CreateBookItem(bookmaster);
    }

 private static void CreateBookItem(Table tblName)
    {
        var client = new AmazonDynamoDBClient();           
        Console.WriteLine("\n*** Executing CreateBookItem() ***");
        string sampleBookId = "3";
        var doc = new Document();
        doc["strid"] = sampleBookId;
        tblName.PutItemAsync(doc);
   }

Also , all the examples are using "tblName.PutItem(doc)" , but it is unavailable. SO I have used "tblName.PutItemAsync(doc)". The log lines are printed in the aws console , but data is not added in table.

Upvotes: 0

Views: 1794

Answers (1)

Sumera
Sumera

Reputation: 215

I was able to solve the above issue with the following code :

public void FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context1)
{
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    var context = new DynamoDBContext(client);

    Table bookmaster = Table.LoadTable(client, "bookmaster");
    context1.Logger.LogLine("In method : Function Handler : start");            

    string result = PutDataAsync(bookmaster, context1).Result;
    context1.Logger.LogLine("Result = " + result);            
}

private static async Task<string> PutDataAsync(Table table , ILambdaContext context1)
{
    try
    {
        string sampleBookId = "3";
        var doc = new Document();
        doc["strid"] = sampleBookId;

        Document x = await table.PutItemAsync(doc);
        context1.Logger.LogLine("In method after put item async");
        return "success";
    }
    catch(Exception ex)
    {
        context1.Logger.LogLine("In method after put item async catch block");
        return "failed";
    }                     
}

Upvotes: 1

Related Questions