C# Lambda can't read from DynamoDB

0

I have a method:

{
public const string TableName = "tableName"

...

public static async Task<Data> Load(AmazonDynamoDBClient dynamo, string sName)
    {
      Data data = null;

      GetItemRequest request = new GetItemRequest
        {
          TableName = TableName,
          Key = new Dictionary<string, AttributeValue>
                {
                  { "Name", new AttributeValue { S = sName } }
                }
        };
   
      try
      {
        GetItemResponse getDataResponse = await dynamo.GetItemAsync(request);
       
        if (getDataResponse!= null && getDataResponse.Item != null)
        {
          data = new Data(sName, getDataResponse.Item);
        }
        else
        {
          Console.WriteLine("No data in dynamo");
          throw new Exception($"Unable to load data '{sName}' from table '{TableName}'!\nThe data was not found");
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine($"Stack trace: {ex.StackTrace}");
        throw new Exception($"Unable to load data '{sName}' from table '{TableName}'!\n" + ex.Message, ex);
      }

      return data;
    }
}

That when called, is only available to retrieve the data from the DynamoDB table approx. 50% of the time.

That it works some of time makes me confident that permissions and networking are configured correctly.

I have implemented a 'keep warm' event via EventBridge to try and mitigate cold starts, to no avail. The AmazonDynamoDBClient is instantiated as a field on a separate class, the lambda itself, that calls this method, prior to the function handler being called. Looking something like this:

public class LambdaFunction
{
    private Dynamo.Data data = null;
    private AmazonDynamoDBClient dynamo = new AmazonDynamoDBClient()

    private async Task FunctionHandler(event, context)
    {
        // event validation ...
        await ProcessData(dynamo, event.data);
    }

    public async ProcessData(AmazonDynamoDBClient dynamo, OtherClass.Data data)
    {
        Namespace.Class data = await Namespace.Class.Load(dynamo, data.name);              // Load() method above
        // processing of data
        return data;
    }
}

I can't see any throttled requests on the table and no exceptions are being thrown in the lambda logs. I've tried extending the lambda processing time and available memory but that doesn't help.

Any advice is greatly appreciated.

asked 2 months ago268 views
1 Answer
-1

Hi,

First of all, what error does the execution return? Have you tried adding logs to identify where the problem is located?

On the other hand, what timeout have you set to the Lambda function? Have you tried increasing it to see if the error persists?

Finally, have you connected the Lambda function with your VPC? Is it possible that you have some configuration error in the network, which, depending on the subnet, does not allow you to access DynamoDB?

profile picture
EXPERT
answered 2 months ago
  • Hi, yes I have as well as increasing the memory and the behavior remains the same. Thanks.

  • Sorry, I updated my response while you were answering me, could you check it again please?

  • Hi,

    No exception is present in the logs unfortunately. I have a log statement in the Load() method, omitted here for brevity, that prints the sName argument (parameter? property? can you tell I'm a javascript guy lost with C#?) fine and then the function just hangs.

    I have had the timeout as high as 60 seconds without luck. When it works, the data is returned in under a second so it seems unlikely that the function is timing out before the data is returned.

    The function is connected with the VPC and I have a VPC endpoint configured for dynamo. It works roughly half the time so I am confident the network is configured correctly.

    Thanks for following up.

  • Just in case, I would check that the VPC Endpoint or Gateway Endpoint is correctly associated with all the subnets and route tables. You can also review the subnet ACLs, in the event that you had modified them.

    On the other hand, I would add logs before and after accessing the data, just to ensure that the problem is in the connectivity with DynamoDB.

  • Thanks. Can confirm that the lambda's subnets are connected to the endpoint and the ACLs for those subnets allow the traffic. I've also added log statements before and after the

    GetItemResponse getDataResponse = await dynamo.GetItemAsync(request);

    call and only the first one gets logged. This is where it always fails.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions