Creating a Simple .NET Lambda Function
Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.
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
Inside it, you will see a
You won’t change this method now.
4. Deploy the function
Open a terminal in the
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
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 -
"INVOKING THE FUNCTION"
- Lambda Cold Starts - .NET 7 Native AOT vs .NET 6 Managed Runtime
- Signing calls to Lambda Function URLs using IAM auth with HttpClient
- Using a Memory Cache with .NET Lambda Functions
- Accessing the Internet from VPC Connected Lambda Functions using a NAT Gateway
- Accessing AWS Secrets Manager from .NET Lambda Functions, Part 4 - Connected to a VPC, and using a NAT Gateway