AWS Developer Tools Blog
Creating .NET Core AWS Lambda Projects without Visual Studio
In the last post, we talked about AWS Lambda deployment integration with the dotnet CLI, using the Amazon.Lambda.Tools NuGet package to deploy Lambda functions and serverless applications. But what if you want to create an AWS Lambda project outside of Visual Studio? This is especially important if you’re working on platforms other than Windows.
The “dotnet new” Command
The dotnet CLI has a command named new that you can use to create .NET Core projects from the command line. For example, by default there are options for creating many of the common project types.
The new command also has the ability to add more project types via NuGet. We recently released a new NuGet package named Amazon.Lambda.Templates that wraps up all the templates we expose in Visual Studio as project types you can create from the dotnet CLI. To install this NuGet package, run the following command.
dotnet new -i Amazon.Lambda.Templates::*
The trailing ::* in the command specifies to install the latest version. Once the install is complete, the Lambda templates show up as part of dotnet new.
To get details about a template, you can use the help command.
dotnet new lambda.EmptyFunction –help
You can see here that the template takes two optional parameters to set the profile and region. These values are written to the aws-lambda-tools-default.json so you can get started deploying with the Lambda tooling right away.
To create a function, run the following command.
dotnet new lambda.EmptyFunction --name BlogFunction --profile default --region us-east-2
This creates a project for the Lambda function and a test project. We can now use any editor we want to build and test our .NET Core Lambda function. Once we’re ready to deploy the function, we run the following commands.
cd ./BlogFunction/src/BlogFunction dotnet restore dotnet lambda deploy-function BlogFunction –function-role TestRole
After deployment,we can even test the function from the command line by using the following command.
dotnet lambda invoke-function BlogFunction --payload "Hello World"
Summary
With our Lambda tooling provided by Amazon.Lambda.Tools and our project templates provided by Amazon.Lambda.Templates, you can develop .NET Core Lambda functions on any platform. As always, let us know what you think on our GitHub repository.