Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The most basic AWS Lambda Function with .net core does not build

I installed AWS Tools for Visual Studio and created a basic Lambda Function. I changed nothing in the code, but when running dotnet publish or doing the publish through Visual Studio, I get:

C:\Users\Jens Rabe\source\repos\lambdatest\SimpleLambdaTest>dotnet publish

Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 42.2 ms for C:\Users\Jens Rabe\source\repos\lambdatest\SimpleLambdaTest\SimpleLambdaTest.csproj.
Function.cs(2,12): error CS0246: The type or namespace name 'LambdaSerializerAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\Jens Rabe\source\repos\lambdatest\SimpleLambdaTest\SimpleLambdaTest.csproj]
Function.cs(2,12): error CS0246: The type or namespace name 'LambdaSerializer' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\Jens Rabe\source\repos\lambdatest\SimpleLambdaTest\SimpleLambdaTest.csproj]
Function.cs(13,53): error CS0246: The type or namespace name 'ILambdaContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\Jens Rabe\source\repos\lambdatest\SimpleLambdaTest\SimpleLambdaTest.csproj]

This is my SimpleLambdaTest.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.4.0" />
  </ItemGroup>

</Project>

I didn't change the Function.cs either:

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AlexaWhereIs {
    public class Function {

        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(string input, ILambdaContext context) {
            return input?.ToUpper();
        }
    }
}

When trying to compile it from within VS, it also complains about the three things not available.

What am I missing?

Followup 1

I installed the Amazon.Lambda.Templates using the command line:

dotnet new -i Amazon.Lambda.Templates

And recreated the function with

dotnet new lambda.EmptyFunction --name SimpleLambdaTest --profile default --region eu-west-1

I then imported the project in my solution, and it builds and publishes fine. However, after uploading, Visual Studio crashes with the "Visual Studio Community has stopped working".

Followup 2

So I deployed it once and Visual Studio crashed as stated above. But then I can redeploy it on the command line with dotnet lambda deploy-function and test it with dotnet lambda invoke-function. Not that convenient but will do as a workaround.

Any ideas?

like image 892
rabejens Avatar asked Jun 21 '26 16:06

rabejens


1 Answers

It seems like function.cs is missing the library reference to Amazon.Lambda.Core. Please use the below import line at the starting of your function.cs file

using Amazon.Lambda.Core;
like image 111
Sandeep Chandran C Avatar answered Jun 24 '26 07:06

Sandeep Chandran C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!