Creating a Simple .NET Lambda Function

This is a bare-bones example of creating, deploying, and invoking a Lambda function. I am going to refer to this post in other more complex examples, so I don’t have to include the same instructions every time.

1. Get the tools

Install the latest tooling, this lets you deploy and run Lambda functions.

dotnet tool install -g Amazon.Lambda.Tools

Install the latest templates to get .NET 6 support.

dotnet new --install Amazon.Lambda.Templates

Get the latest version of the AWS CLI, from here. (Not used in the example, but will be used in other posts).

2. Create the Lambda function

From the command line run -

dotnet new lambda.EmptyFunction --name MyFunctionName

3. Take a look at the code

Change to the MyFunctionName/src/MyFunctionName directory.

Open the Function.cs file.

Inside it, you will see a FunctionHandler(..) method.

You won’t change this method now.

4. Deploy the function

Open a terminal in the MyFunctionName/src/MyFunctionName directory.

Run -

    
dotnet lambda deploy-function MyFunctionName

You will be asked to select an IAM role, or create a new one, at the bottom of the list will be *** Create new IAM Role ***, type in the associated number.

You will be asked for a role name, enter MyFunctionNameRole.

After this you will be prompted to select the IAM Policy to attach to the role, choose AWSLambdaBasicExecutionRole, it is number 6 on my list.

After a few seconds, the function will be deployed.

5. Invoke the function

From the terminal, run -

dotnet lambda invoke-function MyFunctionName --payload "Invoking the function"

You should get a response with -

Payload:
"INVOKING THE FUNCTION"

You’re done!

comments powered by Disqus

Related