aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

Adding class library reference to function project fails to run in AWS

Open tparvi opened this issue 3 years ago • 5 comments

Description

If you create new lambda project, add a reference to .net 5 class library and deploy it to AWS it fails to run there. This has also been reported in the forums: https://forums.aws.amazon.com/thread.jspa?threadID=337142&tstart=0

Note that running this locally using Mock Lambda Test Tool succeeds.

Reproduction Steps

Using Windows 10, .NET 5 (dotnet --version reports 5.0.201), Visual Studio 2019 v16.9.3)

  1. Update tools (dotnet tool update -g Amazon.Lambda.Tools)
  2. Install templates (dotnet new -i Amazon.Lambda.Templates)
  3. Create new function project (dotnet new lambda.image.EmptyFunction)
  4. Add new .NET 5 class library which has some class and method that e.g. returns string
  5. Change lambda function (Function.cs) to call that method and return the result
  6. Deploy to AWS (package type: image), didn't change any settings
  7. Try to call the function e.g. from Visual Studio using AWS Toolkit.

Logs

When you execute the function you'll see the following error. It seems that the entrypoint or application that aws tries to run is a json file instead of the dll.

START RequestId: 52687131-02aa-43ad-b64d-b13f16329fb0 Version: $LATEST
dotnet exec needs a managed .dll or .exe extension. The application specified was '/var/task/lambdatest.deps.json'
END RequestId: 52687131-02aa-43ad-b64d-b13f16329fb0
REPORT RequestId: 52687131-02aa-43ad-b64d-b13f16329fb0	Duration: 226.99 ms	Billed Duration: 227 ms	Memory Size: 128 MB	Max Memory Used: 6 MB	
RequestId: 52687131-02aa-43ad-b64d-b13f16329fb0 Error: Runtime exited with error: exit status 129
Runtime.ExitError

Below is the log file from when executing publish from Visual Studio when I select the lambdatest.csproj in VS and click "Publish to AWS Lambda..."

Executing publish command
... invoking 'dotnet publish', working folder 'C:\lambdatest\src\lambdatest\./bin/Release/net5.0/linux-x64/publish'
... dotnet publish "C:\lambdatest\src\lambdatest" --output "C:\lambdatest\src\lambdatest\./bin/Release/net5.0/linux-x64/publish" --configuration "Release" /p:GenerateRuntimeConfigurationFiles=true --runtime linux-x64 --self-contained false 
... publish: Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
... publish: Copyright (C) Microsoft Corporation. All rights reserved.
... publish:   Determining projects to restore...
... publish:   Restored C:\lambdatest\src\ClassLibrary1\ClassLibrary1.csproj (in 54 ms).
... publish:   Restored C:\lambdatest\src\lambdatest\lambdatest.csproj (in 76 ms).
... publish:   ClassLibrary1 -> C:\lambdatest\src\ClassLibrary1\bin\Release\net5.0\ClassLibrary1.dll
... publish:   ClassLibrary1 -> C:\lambdatest\src\ClassLibrary1\bin\Release\net5.0\linux-x64\ClassLibrary1.dll
... publish:   ClassLibrary1 -> C:\lambdatest\src\lambdatest\bin\Release\net5.0\linux-x64\publish\
... publish:   lambdatest -> C:\lambdatest\src\lambdatest\bin\Release\net5.0\linux-x64\lambdatest.dll
... publish:   lambdatest -> C:\lambdatest\src\lambdatest\bin\Release\net5.0\linux-x64\publish\
Inspecting Dockerfile to figure how to build project and docker image
Executing docker build
... invoking 'docker build', working folder 'C:\lambdatest\src\lambdatest, docker file C:\lambdatest\src\lambdatest\Dockerfile, image name awslambda1:latest'
... docker build -f "C:\lambdatest\src\lambdatest\Dockerfile" -t awslambda1:latest .
... docker build: #1 [internal] load build definition from Dockerfile
... docker build: #1 sha256:e06b2e093749648c68e2377b5a35e7e961a8e9ceb26c363f3a7cef631105cfd8
... docker build: #1 transferring dockerfile: 890B done
... docker build: #1 DONE 0.0s
... docker build: #2 [internal] load .dockerignore
... docker build: #2 sha256:2da88ae294b4c1f6b9b81ab5be2b2d3934c753221c369439730af5a8dc594d30
... docker build: #2 transferring context: 2B done
... docker build: #2 DONE 0.0s
... docker build: #3 [internal] load metadata for public.ecr.aws/lambda/dotnet:5.0
... docker build: #3 sha256:4274afd762d97e4babe0b28bef11b464cc69d25cae3320462e6250dc533d1f3a
... docker build: #3 DONE 1.4s
... docker build: #4 [1/3] FROM public.ecr.aws/lambda/dotnet:5.0@sha256:cfc17d8c5e1226034f4c6711bdb1cd54f7f19d361d60a1c5ad81a69a22134953
... docker build: #4 sha256:f72a36af055209d69630ec4f953bd574ef330697554b8317cb98f1c6292c60e7
... docker build: #4 DONE 0.0s
... docker build: #6 [internal] load build context
... docker build: #6 sha256:60bd4c6f2cd6e22c53095c7989af461672cbd7cee20644b70230e4257c69c20a
... docker build: #6 transferring context: 33.36kB done
... docker build: #6 DONE 0.0s
... docker build: #5 [2/3] WORKDIR /var/task
... docker build: #5 sha256:a0d6c357f1b83409154566573ee76a28cfcb3c7e5e9a5a70cabe198aa537eaaf
... docker build: #5 CACHED
... docker build: #7 [3/3] COPY bin/Release/net5.0/linux-x64/publish  .
... docker build: #7 sha256:2b99d0bd067aff82e523a43506526f4aacce66b28d60f18c2aca6d6782ed1f59
... docker build: #7 DONE 0.1s
... docker build: #8 exporting to image
... docker build: #8 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
... docker build: #8 exporting layers 0.1s done
... docker build: #8 writing image sha256:607e52024552a8333a53836e59657a614791c939863061d548a8750a047dda7c done
... docker build: #8 naming to docker.io/library/awslambda1:latest done
... docker build: #8 DONE 0.1s
Fetching ECR authorization token to use to login with the docker CLI
Executing docker CLI login command
... invoking 'docker login'
... docker login: WARNING! Using --password via the CLI is insecure. Use --password-stdin.
... docker login: Login Succeeded
Found existing ECR Repository awslambda1
Taging image awslambda1:latest with awslambda1:latest
... invoking 'docker tag'
Pushing image to ECR repository
... invoking 'docker push'
Image 721691334083.dkr.ecr.eu-north-1.amazonaws.com/awslambda1:latest Push Complete. 
Creating new Lambda function LambdaTest1
New Lambda function created
Config settings saved to C:\lambdatest\src\lambdatest\aws-lambda-tools-defaults.json

Below is the contents of aws-lambda-tools-defaults.json file

{
    "Information" : [
        "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
        "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
        "dotnet lambda help",
        "All the command line options for the Lambda command can be specified in this file."
    ],
    "profile"     : "Foo",
    "region"      : "eu-north-1",
    "configuration" : "Release",
    "package-type"  : "Image",
    "function-memory-size" : 128,
    "function-timeout"     : 30,
    "image-command"        : "lambdatest::lambdatest.Function::FunctionHandler",
    "docker-host-build-output-dir" : "./bin/Release/net5.0/linux-x64/publish",
    "function-name"                : "LambdaTest1",
    "function-description"         : "",
    "function-role"                : "arn:aws:iam::721691334083:role/lambda_exec_Lambda1-0",
    "tracing-mode"                 : "PassThrough",
    "environment-variables"        : "",
    "dockerfile"                   : "Dockerfile",
    "image-tag"                    : "awslambda1:latest"
}

Below you can see the lambdatest.deps.json file contents (this is in the publish folder on my machine)

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v5.0/linux-x64",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v5.0": {},
    ".NETCoreApp,Version=v5.0/linux-x64": {
      "lambdatest/1.0.0": {
        "dependencies": {
          "Amazon.Lambda.Core": "2.0.0",
          "Amazon.Lambda.Serialization.SystemTextJson": "2.1.0",
          "ClassLibrary1": "1.0.0"
        },
        "runtime": {
          "lambdatest.dll": {}
        }
      },
      "Amazon.Lambda.Core/2.0.0": {
        "runtime": {
          "lib/netstandard2.0/Amazon.Lambda.Core.dll": {
            "assemblyVersion": "1.0.0.0",
            "fileVersion": "1.0.0.0"
          }
        }
      },
      "Amazon.Lambda.Serialization.SystemTextJson/2.1.0": {
        "dependencies": {
          "Amazon.Lambda.Core": "2.0.0"
        },
        "runtime": {
          "lib/netcoreapp3.1/Amazon.Lambda.Serialization.SystemTextJson.dll": {
            "assemblyVersion": "0.0.0.0",
            "fileVersion": "0.0.0.0"
          }
        }
      },
      "ClassLibrary1/1.0.0": {
        "runtime": {
          "ClassLibrary1.dll": {}
        }
      }
    }
  },
  "libraries": {
    "lambdatest/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    },
    "Amazon.Lambda.Core/2.0.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-V4fw3pYuP1LyUJThpbrbLwy2b4TH3CPYh5oId/CgJvGkX7lSAVyOEmkdGvDhpjegxnS4rgw6tP7faftVBO9jwQ==",
      "path": "amazon.lambda.core/2.0.0",
      "hashPath": "amazon.lambda.core.2.0.0.nupkg.sha512"
    },
    "Amazon.Lambda.Serialization.SystemTextJson/2.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-q2R4I77NrrAHDpcaWby9VA/XhE1tjG/2kpredwucRq7k4dTPCqqB7OFmbiFdlfuErrp2/9+X1l62c4unwVicpw==",
      "path": "amazon.lambda.serialization.systemtextjson/2.1.0",
      "hashPath": "amazon.lambda.serialization.systemtextjson.2.1.0.nupkg.sha512"
    },
    "ClassLibrary1/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    }
  }
}

Below you can see the contents of lambdatest.runtimeconfig.json file

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.0"
    }
  }
}

The publish folder contains following files. Not sure why there is a runtimeconfig file for ClassLibrary1.dll

Amazon.Lambda.Core.dll
Amazon.Lambda.Serialization.SystemTextJson.dll
ClassLibrary1.dll
lambdatest.dll
ClassLibrary1.deps.json
ClassLibrary1.runtimeconfig.json
lambdatest.deps.json
lambdatest.runtimeconfig.json
ClassLibrary1.pdb
lambdatest.pdb

Environment

  • AWS Toolkit version: 1.20.2.0
  • OS Info: Windows 10
  • Build Environment: Visual Studio 2019 v16.9.3
  • Targeted .NET Platform: .NET 5 x64 linux

Resolution


This is a :bug: bug-report

tparvi avatar Apr 10 '21 10:04 tparvi

Had some lunch and came up with the following workaround for this issue. Say your lambda function project is lambdatest.csproj. Edit it and add something like this:

 <Target Name="CustomTarget" AfterTargets="_PublishNoBuildAlternative">
  <Delete Files="$(PublishDir)\ClassLibrary1.runtimeconfig.json"/>
  <Delete Files="$(PublishDir)\ClassLibrary1.deps.json"/>
</Target>	

This will delete the runtime config and deps file for the class library and after that everything works. After this you are only left with single runtime config and deps file for the actual lambda function.

tparvi avatar Apr 10 '21 13:04 tparvi

This is the entry point https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.RuntimeSupport/lambda-entrypoint.sh. If LAMBDA_DOTNET_MAIN_ASSEMBLY not set then it seems to pick the first one.

pekkah avatar Apr 11 '21 05:04 pekkah

@tparvi @pekkah Thanks for your inputs. The issue is easily reproducible.

ashishdhingra avatar Apr 12 '21 20:04 ashishdhingra

We have noticed this issue has not received attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

github-actions[bot] avatar May 04 '22 00:05 github-actions[bot]

No auto close.

ashishdhingra avatar May 04 '22 04:05 ashishdhingra