.NET Workloads on AWS Lambda

MODULE 2

Module 2: Tooling for .NET Development

 LEARNING MODULE

AWS provides multiple tools to help you develop Lambda functions for .NET. All are free and can be installed directly into the IDE of your choice, or into your chosen command line. The tools don’t need to be configured if you already have an AWS account setup on your computer.

.NET tooling for AWS Lambda is available for Visual Studio (Windows only), Visual Studio Code and Rider. Command line tooling is available for Windows, Linux, and Mac. The level of support varies between IDEs and operating systems. A summary of the available tooling is provided in this module, along with links to relevant downloads.

 Time to Complete

60 minutes 

AWS Toolkit for Visual Studio

As of May 2022, the AWS Toolkit for Visual Studio is only available for Windows. Upon installation, the toolkit will help you configure it to access your AWS account.

Through a familiar user interface, the toolkit allows you to develop, debug, deploy, update, and test your Lambda functions via a graphical interface within Visual Studio. The Toolkit is available for download for Visual Studio 2022, 2019, and 2017.

Installation

In Visual Studio, go to the Extension menu, and select Manage Extensions. On the right, in the search box, type "AWS Toolkit". In the results pane, select the "AWS Toolkit for Visual Studio" extension and click Download. Follow the prompts to install the extension, you may need to restart Visual Studio.

If you would prefer, you can download the AWS Toolkit directly, go to the documentation for AWS Toolkit for Visual Studio.

Overview and main features

In relation to the Lambda service, the toolkit provides two key sets of features, project templates for Lambda functions, and tooling to assist with deployment, management, and testing/invocation of Lambda functions. Along with this, the toolkit allows you to manage other AWS resources and deploy applications to other types of AWS infrastructure such as AWS Fargate, AWS App Runner, and AWS Elastic Beanstalk (they are not covered in this course).

The toolkit adds many Lambda project templates to help you create a variety of Lambda functions.

The most simple takes a string and returns an uppercase version of that string. This is a great one to get started with because of its simplicity, ease of deployment and ease of testing. In just a few minutes you can have a .NET Lambda function running on AWS, send it requests, and get responses.

On the more complex side, there is a template that provides a full .NET 6 Web Application (with web pages and API controllers). When this is deployed, the required infrastructure to make it accessible from the web will also be deployed and configured for you.

There are container project templates available if that is your preference. Note you must have Docker installed on your computer for these to work. See the section “Choosing the runtime for you”, in module 4 for why you may choose to use container images with Lambda functions.

Unit testing your Lambda functions is just as important as testing any of your other code. Most project templates include a test project that uses xUnit to test the code in the source code project. This makes it easy to get started with unit testing.

As you can tell from the above, from Visual Studio, you can also publish your functions to the AWS Lambda service. The publish functionality takes care of building a package, uploading to the Lambda service, applying permissions, creating the API Gateway endpoint, and any other necessary steps to get your code running and available for invocation. All you have to do is write the code.

Another feature of the toolkit is the AWS Explorer, which allows you to view and manage many of your AWS resources, such as DynamoDB, S3, SNS, SQS queues, EC2, and, of course, Lambda functions.

To open the AWS Explorer, go to View menu and select AWS Explorer.

In relation to Lambda functions, the AWS Explorer lets you:

  • Choose a region.
  • View and manage your Lambda functions.
  • Invoke Lambda functions on AWS, passing in any necessary parameters, and view the response. The tooling provides sample input to simulate calls from a variety of other AWS services. This is very useful when you are building a Lambda function that is triggered by an event from some other service, e.g. a file being created on S3.
  • Debug a locally running version of a Lambda function.
  • Configure services that are allowed to trigger a Lambda function.
  • View logs generated by Lambda functions.

To invoke a Lambda function from the toolkit, open the AWS Explorer, expand the Lambda section, right click the Lambda function you want to execute, select View Function.

From there, you can choose an example request from the dropdown list. These samples let you simulate calls from other AWS services such as the API Gateway, S3, SNS, etc. This lets you easily and directly test your Lambda functions that are invoked by events from other services.

Or you can use your own request.

Along with the above, all Lambda related command line tools can be used via the terminal.

If you are new to AWS, the toolkit for Visual Studio is the best way to get started. It has the most features of all the toolkits that are available, and is the easiest to debug with, and perform deployments. The AWS toolkit for Visual Studio also works with Visual Studio Community Edition, which is available for free. This, along with the free tier on AWS is a great combination for developers who are new to AWS.

AWS Toolkit for Visual Studio Code

The AWS Toolkit for Visual Studio Code (VS Code) is available for all operating systems that VS Code runs on - Windows, Linux, and Mac. The toolkit offers a consistent experience across all three operating systems.

While not as fully featured as the AWS Toolkit for Visual Studio, the AWS Toolkit for Visual Studio Code lets you browse and interact with your resources on AWS.

You can browse S3 buckets (and edit files!), invoke API Gateway endpoints, deploy from the Amazon Elastic Container Registry (ECR), invoke Lambda functions on AWS, and debug them locally (needs a dotnet tool, see below). For a full list of its features, see the AWS Toolkit for Visual Studio Code documentation.

You can also use all the available AWS command line tools with VS Code. This is most likely how you will interact with AWS from VS Code. The command line tools give you the flexibility to manage every AWS service.

Installation

In the primary side bar, click the extensions icon. In the search bar that opens, type “aws toolkit”, you should see the AWS Toolkit at or near the top of the list. Click Install. You may need to restart VS Code.

Overview and main features

The AWS Toolkit for VS Code can be added using the VS Code extension marketplace, or by downloading directly from the AWS Toolkit page on the Visual Studio marketplace.

If you have already configured an AWS account on your computer, the toolkit will automatically connect to AWS using that account.

An AWS icon will appear in the primary side bar of the VS Code window. From there, you can see all the services the toolkit has access to.

The toolkit does not include Lambda project templates like the Visual Studio Toolkit. Instead, you create a new Lambda function via the command line with the dotnet new ... command. A rich list of templates is available. These templates will help you get started very quickly with AWS Lambda functions. Instruction for installing dotnet CLI Templates is below.

In relation to Lambda functions, the AWS Explorer lets you:

  • Invoke Lambda functions on AWS, passing in any necessary parameters, and view the response. The tooling provides sample input to simulate calls from a variety of other AWS services triggering the Lambda.
  • Debug Lambda functions locally using the AWS .NET Mock Lambda Test Tool.
  • Upload a zip archive of a built .NET Lambda function.
  • View logs generated by Lambda functions (via CloudWatch Logs).

To invoke a Lambda function from the toolkit, open the AWS Explorer, expand the Lambda section, right click on the Lambda function you want to execute, select Invoke on AWS.

This will open a new window where you can select a request payload to invoke the function with.

Or you can write your own request payload. The VS Code output tab shows the response payload.

Along with the above, all Lambda related command line tools can be used with VS Code via the terminal. In particular, the dotnet lambda command is especially useful when working with Lambda functions in VS Code. Details and instructions for installing it are in the section “AWS Extensions for .NET CLI (dotnet lambda ...)” below.

AWS Toolkit for Rider

The AWS Toolkit for Rider is a plugin for Rider that helps you to build, view, and manage AWS resources. It is similar in capabilities to the AWS Toolkit for VS Code, but it includes a variety project templates for .NET to help you get started developing Lambda functions quickly.

Installation

To install the AWS Toolkit for Rider, open the File menu, then Settings, and Plugins. Search for AWS Toolkit and install.

If you have already configured an AWS account on your computer, the toolkit will automatically connect to your AWS account.

Once installed, you will have a range of new AWS Lambda function templates available to you.

Overview and main features

To open the AWS Explorer, go to the View menu, then Tool Windows, and select AWS Explorer.

From the AWS Explorer, you can do the following in relation to Lambda functions:

  • Invoke Lambda functions on AWS, passing in any necessary parameters, and view the response. The tooling provides sample input to simulate calls from a variety of other AWS services triggering the Lambda.
  • Debug Lambda functions locally using the AWS .NET Mock Lambda Test Tool.
  • Upload a zip archive of a built .NET Lambda function.
  • View logs generated by Lambda functions (via CloudWatch Logs).

To invoke a Lambda function, first open the AWS Explorer. To do this, go to the View menu, then Tool Windows, and select AWS Explorer.

In the AWS Explorer, expand the Lambda section, right click on the Lambda you want to invoke. Click Run '[Remote] …'

This opens a new window where you can choose a template to invoke the request.

Or type in your own request.

Click run. Then the output will be displayed.

Along with the above, all Lambda command line tools can be used with Rider via the terminal.

Command Line Tools

All the available command line tools will work with any shell/terminal/command prompt on Windows, Linux, and Mac.

But for a consistent experience across all three operating systems, we recommend you use PowerShell Core. Differences in JSON encoding in different terminals makes it difficult to provide examples that will work everywhere, that problems goes away when using PowerShell Core. As such, all examples in this course have been tested using PowerShell Core.

AWS Command Line Interface (CLI)

The AWS Command Line Interface (CLI) is an open source tool that lets you manage all AWS resources from the command line. You can manually perform these actions, or run them in an automated fashion using a scripting language of your choice. This tool is available for Windows, Linux, and Mac.

In most circumstances, you should install version 2 of the AWS CLI, as it supports all the latest features of AWS. Unless you have a specific reason to use version 1 of the AWS CLI, you should stick with version 2.

When new features are added to an AWS service, the AWS CLI tool will be updated to allow you to manage these features. The AWS CLI tool is frequently updated, so you should keep an eye out for the newer versions.

Installation

The AWS CLI is available for Windows, Linux, and Mac. Your method for installation will vary depending on your operating system. Full instructions can be found on the AWS page - Installing or updating the latest version of the AWS CLI. AWS advises against installing the AWS CLI using any package management software. Instead, use the link provided above.

Keep in mind that you need to update the AWS CLI from time to time to get the latest features.

To check your version of the AWS CLI, run the following command:

aws --version

You will see a response that looks similar to this:

aws-cli/2.5.4 Python/3.9.11 Windows/10 exe/AMD64 prompt/off

This tells me I have version 2.5.4 of the AWS CLI installed.

To see what is the most recent version of the AWS CLI, go to https://github.com/aws/aws-cli/blob/v2/CHANGELOG.rst, below “CHANGELOG” will be the version of the most recent release. At the time of this writing it is 2.7.2, so I need to update my AWS CLI!

Overview and main features

With the AWS CLI you can manage all your AWS resources. The AWS CLI includes powerful querying and filtering capabilities, which can help you find the resources you are looking for. As you would expect, the AWS CLI allows you to list, view, manage, and invoke Lambda functions.

Extensive documentation with examples is available at the AWS CLI Command Reference.

AWS Lambda for .NET Core - Templates

With the AWS CLI you can manage all your AWS resources. The AWS CLI includes powerful querying and filtering capabilities, which can help you find the resources you are looking for. As you would expect, the AWS CLI allows you to list, view, manage, and invoke Lambda functions.

Extensive documentation with examples is available at the AWS CLI Command Reference.

AWS Lambda for .NET Core - Templates

When using Visual Studio and Rider, the AWS Toolkits provide you with a range of C# (and F#) project templates to help you get started with AWS Lambda functions.

These templates start with the most simple - a Lambda function that takes a string, converts it to uppercase and returns a string. Very simple in itself, but a good place to get started and see how easy it is to write code, and deploy it AWS Lambda in a matter of minutes.

More complex templates are available too, these set up full .NET web applications or API applications, and build all the necessary infrastructure for you - roles, permissions, buckets, API Gateways, etc.

Most of the project templates come with a src and test directory, immediately giving you unit testing capabilities you can expand on as your application grows. The test projects will help you learn how the Lambda function works and provide an easy way to debug your way through your code.

Installation

To install the templates run the following command:

dotnet new -i "Amazon.Lambda.Templates::*"

Overview and main features

After the installation is complete, you will use the dotnet new command to create new Lambda function projects.

To see the list of project templates available, run the following command:

dotnet new lambda --list

You will see output that looks like this (note the available templates change periodically):

You might notice that the short name for some templates starts with "serverless." and some start with "lambda.".

Those that start with "serverless." use the AWS Serverless Application Model (SAM) to deploy the Lambda function and build the required infrastructure that the Lambda function will use. AWS SAM is an open source framework that let’s you build serverless application on AWS. Along with defining the Lambda function, with AWS SAM you can define other resources such as API Gateways, databases, event sources.

If you navigate to the projectname/src/projectname directory, you will see a file called serverless.template. This defines the required infrastructure, and how it is wired up to the Lambda function. In all the "serverless." templates listed above, an API Gateway is used to route HTTP requests to the Lambda function.

The templates that start with "lambda." do not use the Serverless Application Model, and do not setup other infrastructure. If other infrastructure is required, you will need to set it up yourself. For example, the lambda.Kinesis project template shows you how to process incoming Kinesis events, but it does NOT create the Kinesis streams for you, or add the relevant permissions needed for Kinesis to trigger the Lambda function.

To create a new project based on the lambda.EmptyFunction template run:

dotnet new lambda.EmptyFunction

The tooling is open source and available to view on GitHub,

https://github.com/aws/aws-lambda-dotnet#dotnet-cli-templates.

As with the other command line tools, you should keep an eye on the relevant GitHub pages to see when new versions are released.

AWS Extensions for .NET CLI (dotnet lambda ...)

While the AWS CLI tool lets you manage all AWS resources, the AWS Extensions for .NET CLI specifically focuses on Lambda functions and is invoked as a .NET tool, i.e. dotnet lambda.

Installation

To install the AWS Extension for .NET CLI, run the following:

dotnet tool install -g Amazon.Lambda.Tools

To verify it is installed correctly, run:

dotnet lambda

This will output a summary of commands you can use and a version number.

Overview and main features

This tool works from any shell and is especially useful when used from the terminal within Visual Studio Code and Rider. It allows you to perform common Lambda function related tasks.

For example, to list all Lambda functions, you can use:

dotnet lambda list-functions

Or to invoke a Lambda function, run:

dotnet lambda invoke-function functionName 

It can also deploy Lambda functions. After creating a Lambda function, with dotnet Lambda templates, (see above) you can deploy the function to AWS using -

dotnet lambda deploy-function

This will kick off a build and guided deployment process. The tool will ask you for the Lambda function name, the role to assign to the function (or create a new one). If you created a new role, it will ask you to choose a permission policy for that new role. Then the tool will deploy the function to AWS and within a few seconds you'll be able to invoke it!

To see all available command line options, use -

dotnet lambda deploy-function --help

Keep in mind that behind each command there can be a lot of functionality, so it is important to understand that each command line supports multiple options. For example, the update-function-config is where you enable and configure Lambda function URLs. That functionality is not available in the deploy-function command. Keeping up with the latest release documentation and features on the Lambda service is important, as it will help you understand and make use of the latest features.

While this tool is very convenient and powerful for managing Lambda functions when developing code and learning about AWS, we strongly encourage you to use a robust CI/CD process for deploying to your applications when they reach a certain level of maturity. AWS supports all major CI/CD tools.

AWS Tools for PowerShell

The AWS Tools for PowerShell expose the AWS SDK for .NET via a set of PowerShell modules. For those familiar with PowerShell scripting, this makes them a powerful tool for writing deployment and maintenance scripts.

The AWS Tools for PowerShell can be installed on Windows, Mac, and Linux, offering an identical set of features on all three platforms.

The tools are available in a single module, or in a modularized version where you download required modules as needed.

The modularized version is recommended for production environments.

You can find more information on choosing between these version check here.

Installation

For installation on Windows, see the Installing the AWS Tools for PowerShell on Windows guide.

For installation on Linux or Mac, see the Installing AWS Tools for PowerShell on Linux or macOS guide.

Overview and main features

The number of modules, and then commands that can be performed by AWS Tools for PowerShell is very large. In relation AWS Lambda, you can use the tools to list, view, deploy, manage, and invoke functions.

To see all available commands run:

Get-Command -Module AWS.Tools.Lambda

You can find full documentation for all commands in the AWS Tools for PowerShell Cmdlet Reference, navigate to the Lambda section in the left bar.

To list all Lambda functions run:

Get-LMFunctionList

To invoke a Lambda function run:

$Response=Invoke-LMFunction -FunctionName StringToUpperCase -Payload '"hello world"'

[System.IO.StreamReader]::new($Response.Payload).ReadToEnd() 

Docker for .NET 7 Native AOT Functions

In November 2022, Microsoft released .NET 7. Though there is no Lambda managed runtime for this version of .NET, you can use it as custom runtime with native ahead-of-time (AOT) compilation.

 The major benefit of this approach is significantly faster cold starts for .NET 7 functions. In tests using comparable functions running .NET 6 with a managed runtime, and .NET 7 using AOT compilation, the .NET 7 function starts up to 86% faster.

To read more about serverless .NET applications on AWS  see this post - Building serverless .NET applications on AWS Lambda using .NET 7.

To use compile with .NET 7 Lambda functions, you need to install and run Docker.
You can download Docker from the Docker Installation Overview.

Comparison of select tools

Knowledge Check

You’ve now completed Module 2, Tooling for .NET development with AWS Lambda. The following test will allow you to check what you’ve learned so far.

1. Which AWS toolkit for an IDE has the most features? (select one)

a. Visual Studio Code

b. Visual Studio

c. Rider

d. They all have the same features

2. Which operating systems support the AWS Extensions for .NET CLI?

a. Mac

b. Linux

c. Windows

d. All of the above

a. Homebrew

b. Pip

c. Chocolatey

d. Download directly from AWS

Answers: 1-b, 2-d, 3-d

Conclusion

In this module, you learned about the most popular and important tools for developing .NET Lambda functions. You now know what tools are available, how to install them, some basics of using them, and you have links to find out more.

But, as with many things like this, the best way to learn is to begin experimenting. In the next module, you learn how to use .NET with the Lambda service.

Was this page helpful?

.NET ON AWS LAMBDA