Sanjay Chaudhari
Sanjay Chaudhari's Technical Notes

Sanjay Chaudhari's Technical Notes

.NET 7 Preview - Running AWS Lambda using Custom Runtime

.NET 7 Preview - Running AWS Lambda using Custom Runtime

Sanjay Chaudhari's photo
Sanjay Chaudhari
·Feb 20, 2022·

3 min read

Last week, after completing their 20th anniversary .Net announced - .NET 7 Preview 1, ASP .NET Core Preview 1 and EF7 Preview 1 ! So this is the perfect time to try preview version with AWS Lambda.

Announcement: devblogs.microsoft.com/dotnet/announcing-ne..

Prerequisite :

  • Visual Studio 2022

  • .NET 7 Preview

  • AWS Toolkit for Visual Studio

Step 1] You can download .NET 7 Preview 1, for Windows, macOS, and Linux from below link -

https://dotnet.microsoft.com/en-us/download/dotnet/7.0

Step 2] Now you need to install Amazon Lambda Tools to build and deploy the lambda, and the Lambda templates, to create the project.

dotnet new -i Amazon.Lambda.Templates
dotnet tool install -g Amazon.Lambda.Tools

Now run dotnet new lambda --list to see all the available templates.

You will see a list like below -

1.PNG

Step 3]

A runtime is an option visible to you when you are creating a Lambda function. It defines how the Lambda function will invoke your handler.

The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event. It takes input data as the first parameter and lambda context as the second parameter.

You need to select Custom Runtime Function (.NET 6)

2.PNG

Step 4]

Edit .csprojfile & update version to net7.0

<TargetFramework>net7.0</TargetFramework>

Step 5]

Update FunctionHandlercode as below -

using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using System;
using System.Threading.Tasks;

namespace CustomRuntimeLambdaDotNet7
{
    public class Function
    {
        private static async Task Main(string[] args)
        {
            Func<string, ILambdaContext, string> handler = FunctionHandler;
            await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
                .Build()
                .RunAsync();
        }

        public static string FunctionHandler(string input, ILambdaContext context)
        {
            var architecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture;
            var dotnetVersion = Environment.Version.ToString();
            return $"Details : Architecture - {architecture}, Current .NET Version -  {dotnetVersion}";
        }
    }
}

So basically now Function Handler will tell us the architecture it is running on as well as version of .NET

Step 6] Let's proceed by right clicking the project & select Publish to AWS Lambda option.

3.png

Step 7] Do provide proper lambda function's execution role - It is an AWS IAM role that grants the function permission to access AWS services and resources. You provide this role when you create a function, and Lambda assumes the role when your function is invoked.

Once deployment is successful - you can test output in Visual Studio itself by sending Sample Input.

4.PNG

Troubleshooting : While building project, If you get error like The current .NET SDK does not support 'newer version' as a target.

Resolution If you have a preview installed of the requested .NET SDK version, you also need to set the option to enable previews in Visual Studio.

Go to Tools > Options > Environment > Preview Features, and make sure that Use previews of the .NET Core SDK is checked.

5.PNG

Conclusion

I hope you enjoyed the article, after reading this you should know how to run AWS lambda using custom runtime on .net preview versions. Thank you for taking your valuable time to read the article. Please share your feedback & suggestion in comment section.

 
Share this