Workaround for "Failed to create template" Error with .NET 7 RC1 and Lambda Functions
Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.
UPDATE - The release of Amazon.Lambda.Templates, version 6.4.0 resolves this problem. Updating is the best solution. Run dotnet new -i Amazon.Lambda.Templates
to perform the update.
But if you are unable to update, follow the procedure below.
If you are using AWS .NET Lambda templates version 6.3.0 or earlier, and you install .NET 7 RC 1, you get an error when you try to create a new Lambda function project.
If you run -
dotnet new lambda.EmptyFunction --name MyFunction
You will get an error like this -
Template "Lambda Empty Function" could not be created.
Failed to create template.
Details: Object reference not set to an instance of an object.
The same error occurs when trying to create Lambda serverless projects -
dotnet new serverless.AspNetCoreWebAPI --name AspNetCoreWeblAPI
The workaround is to create a global.json
in the folder where you want to create the Lambda project.
Check what version of .NET SDK you have installed -
dotnet --list-sdks
You will see the list of installed SDKs -
6.0.304 [C:\Program Files\dotnet\sdk]
6.0.401 [C:\Program Files\dotnet\sdk]
7.0.100-rc.1.22431.12 [C:\Program Files\dotnet\sdk]
Choose the version you want and run -
dotnet new globaljson --sdk-version 6.0.401
This creates a global.json
file with the following content -
{
"sdk": {
"version": "6.0.401"
}
}
Now your dotnet new lambda...
commands will work.
To find out more about the global.json
file, see this article.
One thing to keep in mind, if you are creating a Lambda custom runtime project to use .NET 7, you will need to delete the global.json
file for compilation to work.