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.

Can't package lambda for .NET 7 AOT from within a Docker Container

See original GitHub issue

Describe the bug

Our CI build pipelines all run inside docker contains so that we can easily control versions of the toolkits that we are using.

It seems that the newest version of the Lambda build tookit tries to start up it’s own docker container which doesn’t seem to work very well when running already from within docker. This has worked great up until now, but the .NET 7 process seems to have broken.

Starting container for native AOT build using build image: public.ecr.aws/sam/build-dotnet7:latest-x86_64.
  Unknown error executing command: Failed to locate docker CLI executable. Make sure the docker CLI is installed in the environment PATH.
     at Amazon.Common.DotNetCli.Tools.DockerCLIWrapper..ctor(IToolLogger logger, String workingDirectory) in C:\build\src\Amazon.Common.DotNetCli.Tools\DockerCLIWrapper.cs:line 23
     at Amazon.Lambda.Tools.LambdaPackager.CreateApplicationBundle(LambdaToolsDefaults defaults, IToolLogger logger, String workingDirectory, String projectLocation, String configuration, String targetFramework, String msbuildParameters, String architecture, Boolean disableVersionCheck, LayerPackageInfo layerPackageInfo, Boolean isNativeAot, Nullable`1 useContainerForBuild, String containerImageForBuild, String codeMountDirectory, String& publishLocation, String& zipArchivePath) in C:\build\src\Amazon.Lambda.Tools\LambdaPackager.cs:line 144
     at Amazon.Lambda.Tools.Commands.PackageCommand.PerformActionAsync() in C:\build\src\Amazon.Lambda.Tools\Commands\PackageCommand.cs:line 231
     at Amazon.Common.DotNetCli.Tools.Commands.BaseCommand`1.ExecuteAsync() in C:\build\src\Amazon.Common.DotNetCli.Tools\Commands\BaseCommand.cs:line 47
  Process exited with code 255
  Process exited with code 255 (Step: Command Line)
  Step Command Line failed

Expected Behavior

The Amazon.Lambda.Tools toolkit can be ran from within a docker container

Current Behavior

the dotnet lambda package command crashes when ran in docker

Reproduction Steps

  1. Create a .NET 7 lambda project using AOT following this AWS guide
  2. docker run -v ${PWD}:/build -it mcr.microsoft.com/dotnet/sdk:7.0 sh
  3. apt-get update && apt-get -y install zip
  4. dotnet tool install -g Amazon.Lambda.Tools
  5. export PATH="$PATH:/root/.dotnet/tools"
  6. dotnet lambda package -pl build/Examples.AWS.Lambda.S3

Resulting Error

Amazon Lambda Tools for .NET Core applications (5.6.2)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Found /etc/os-release
Architecture not provided, defaulting to x86_64 for container build image.
Executing publish command
Starting container for native AOT build using build image: public.ecr.aws/sam/build-dotnet7:latest-x86_64.
Unknown error executing command: Failed to locate docker CLI executable. Make sure the docker CLI is installed in the environment PATH.
   at Amazon.Common.DotNetCli.Tools.DockerCLIWrapper..ctor(IToolLogger logger, String workingDirectory) in C:\build\src\Amazon.Common.DotNetCli.Tools\DockerCLIWrapper.cs:line 23
   at Amazon.Lambda.Tools.LambdaPackager.CreateApplicationBundle(LambdaToolsDefaults defaults, IToolLogger logger, String workingDirectory, String projectLocation, String configuration, String targetFramework, String msbuildParameters, String architecture, Boolean disableVersionCheck, LayerPackageInfo layerPackageInfo, Boolean isNativeAot, Nullable`1 useContainerForBuild, String containerImageForBuild, String codeMountDirectory, String& publishLocation, String& zipArchivePath) in C:\build\src\Amazon.Lambda.Tools\LambdaPackager.cs:line 144
   at Amazon.Lambda.Tools.Commands.PackageCommand.PerformActionAsync() in C:\build\src\Amazon.Lambda.Tools\Commands\PackageCommand.cs:line 231
   at Amazon.Common.DotNetCli.Tools.Commands.BaseCommand`1.ExecuteAsync() in C:\build\src\Amazon.Common.DotNetCli.Tools\Commands\BaseCommand.cs:line 47

Possible Solution

Consider not trying to spin up docker containers if the tool is already running in docker?

Additional Information/Context

No response

Targeted .NET platform

.NET 7

CLI extension version

amazon.lambda.tools 5.6.2

Environment details (OS name and version, etc.)

mcr.microsoft.com/dotnet/sdk:7.0 sh

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
normjcommented, Nov 22, 2022

@Hammatt To your question on will there be a managed .NET 7 runtime, Lambda’s policy is to only support LTS versions for managed runtimes. So for .NET 7 the options are to deploy as a self contained or native aot function to the the provided.al2 runtime.

1reaction
normjcommented, Nov 22, 2022

@Hammatt Adding configuration to @ashishdhingra statement if the CLI tooling detects it is running in an AL2 environment like an AL2 container then it won’t try spin up a container. The tough part of the dev cycle of Native AOT is the .NET code needs to compiled on the same OS as where it will run so spinning up a container was our best option when packaging on a non-AL2 environment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build Failure For .NET 7 AOT When Using A Private ...
Describe the bug I've been following this AWS article to build a Lambda function target .net 7 AOT. I've a test function here....
Read more >
Running a .NET AWS Lambda locally in a Docker container
I'm having trouble running my .NET 7 Lambda locally as a Docker container. There seems to be loads online about running python or...
Read more >
Deploy .NET Lambda functions with container images
Deploy your .NET Lambda function code as a container image using an AWS provided base image or the runtime interface client.
Read more >
Using .NET 7 native AOT with AWS Lambda - YouTube
NET 7 today with AWS Lambda, and reduce your cold starts by taking ... AOT ) compilation to deploy your lambda functions as...
Read more >
.NET 7 on AWS Lambda with NativeAOT - YouTube
In this video you will learn how you can use . NET on AWS Lambda using Native Ahead of Time Compilation. AWS Lambda...
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