question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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-report

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tparvicommented, Apr 10, 2021

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.

0reactions
ashishdhingracommented, May 4, 2022

No auto close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referenced class library projects are not found when using ...
I am trying to deploy a .NET6 based AWS Lambda function - "ABC.DynamoDBConnectLambda" using AWS CDK in Typescript. "ABC.DynamoDBConnectLambda" ...
Read more >
Troubleshoot deployment issues in Lambda
This issue can occur when you specify an Amazon S3 object in a call to UpdateFunctionCode, or use the package and deploy commands...
Read more >
Best practices for working with AWS Lambda functions
Best practices for using AWS Lambda.
Read more >
Basic AWS Lambda Project - AWS Toolkit for Visual Studio
Working on AWS Lambda projects in Visual Studio. ... The field function-handler specifies the method that runs when the Lambda function runs.
Read more >
Introducing the .NET 6 runtime for AWS Lambda
Open the aws-lambda-tools-defaults. json file, if it exists: Set the function-runtime field to dotnet6. Set the framework field to net6.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found