Home > Uncategorized > Getting #AWS #Lambda timeout value at runtime in C#

Getting #AWS #Lambda timeout value at runtime in C#

If you have a Lambda function in C#, and you have a process that must complete before Amazon kills the process, say in the classic bank transfer example, where you debit one account and credit another. Both actions need to be taken atomically, or else the process should be rolled back.

In this case, you can’t have AWS killing your lambda function due to lack of time remaining, in mid-transaction. In this case, you’d have one account missing money, and no money in the payee account. In this case, it would be better to get a forewarning, that gives you X seconds to rollback the transaction, so that at least, you’re back to where you started.

So, what’s the magic command:

     var context = (ILambdaContext) HttpContext.Items["LambdaContext"];

This gives you an ILambdaContext Object, that contains data similar to the following;


{
   "FunctionName":"HelloWorld-AspNetCoreFunction-1T4MMT4XYY3IJ",
   "FunctionVersion":"$LATEST",
   "LogGroupName":"/aws/lambda/HelloWorld-AspNetCoreFunction-1T4MMT4XYY3IJ",
   "LogStreamName":"2021/03/01/[$LATEST]36833c138ec040549dfd7aa350f2ca44",
   "MemoryLimitInMB":512,
   "AwsRequestId":"b481c563-a027-43d7-bb8e-841ea0f89cdf",
   "InvokedFunctionArn":"arn:aws:lambda:eu-west-1:005445879168:function:HelloWorld-AspNetCoreFunction-1T4MMT4XYY3IJ",
   "RemainingTime":"00:00:28.6830000",
   "ClientContext":null,
   "Identity":{
      "IdentityId":"",
      "IdentityPoolId":""
   },
   "Logger":{
      
   }
}

Which gives you Remaining Time , and the Memory Limit, along with other values.

For other values relating to the Lambda, you should also check the Environment variables in your code.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: