Sign inSign up

smashedtoatoms/al2-dotnet8-preview-aot-builder

By smashedtoatoms

Updated about 3 years ago

AWS AL2 image for building aot-compiled arm dotnet 8 lambdas.

Image
0

10K+

smashedtoatoms/al2-dotnet8-preview-aot-builder repository overview

This is an AWS AL2 image for building AOT-compiled arm dotnet 8 lambdas via Docker on an m1 or m2 mac or an arm AWS CodeBuild instance.

To use it:

  • install AWS Dotnet Lambda Tools
dotnet tool install -g Amazon.Lambda.Tools
  • install AWS Dotnet Lambda Templates
dotnet new install Amazon.Lambda.Templates
  • create a project
dotnet new lambda.NativeAOT -n MyFn
  • update the csproj file to target net8.0 instead of net7.0
  • use docker to kick off your build with something like this:
    docker run --rm --volume .:/tmp/source/ -i -u 1000:1000 -e DOTNET_CLI_HOME=/tmp/dotnet -e XDG_DATA_HOME=/tmp/xdg \
        smashedtoatoms/al2-dotnet8-preview-aot-builder:latest \
		dotnet publish "/tmp/source/src/MyFn" \
			--output "/tmp/source/src/MyFn/bin/Release/net8.0/publish"\
			--configuration "Release" \
			--framework "net8.0" /p:GenerateRuntimeConfigurationFiles=true \
			--runtime linux-arm64 \
            --self-contained True
  • Then deploy the generated bootstrap binary sitting in your bin/Release/net8.0/public directory via your preferred method. Some might zip up that bootstrap file and manually do it. The smart ones will point to it via CDK with something like this
        var myFn = new Function(this, "MyFn", new FunctionProps
        {
            Architecture = Architecture.ARM_64,
            Runtime = Runtime.PROVIDED_AL2,
            Code = Code.FromAsset("../MyFn/bin/Release/net8.0/publish"),
            Handler = "bootstrap",
            MemorySize = 512,
            Timeout = Duration.Seconds(10),
        });
  • Start enjoying those quicker cold starts, assuming you don't get burned by something in your code that slows them down... like the actual AWS SDK itself or the dotnet crypto libraries that get used everywhere... sigh.

The contents of the Dockerfile that created this are as follows:

FROM public.ecr.aws/amazonlinux/amazonlinux:2

# Set up environment variables for package management
ENV \
    DOTNET_GENERATE_ASPNET_CERTIFICATE=false \
    DOTNET_NOLOGO=true \
    DOTNET_SDK_VERSION=8.0.100-preview.7.23376.3 \
    DOTNET_USE_POLLING_FILE_WATCHER=true \
    NUGET_XMLDOC_MODE=skip \
    POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-Debian-12-arm64 \
    DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true \
    PATH="/usr/share/dotnet:${PATH}"

# Install dependencies
RUN set -ex \
    && yum groupinstall -yq "Development Tools" \
    && yum install -yq wget clang zlib-devel openssl-devel

# Install .NET SDK
RUN wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh \
    && chmod +x dotnet-install.sh \
    && ./dotnet-install.sh --version $DOTNET_SDK_VERSION --install-dir /usr/share/dotnet \
    && rm dotnet-install.sh \
    && dotnet help

# Install PowerShell global tool
RUN powershell_version=7.4.0-preview.3 \
    && curl -fSL --output PowerShell.Linux.arm64.$powershell_version.nupkg https://pwshtool.blob.core.windows.net/tool/$powershell_version/PowerShell.Linux.arm64.$powershell_version.nupkg \
    && powershell_sha512='4a5d48199ed8b66446b7cff109f9864a9b0cea138d28f9aef7211accbf246ce2ee1f8f3aae23e2d6be2b532dd3b548516dacdb11f83794a17575e6a7335a73d2' \
    && echo "$powershell_sha512  PowerShell.Linux.arm64.$powershell_version.nupkg" | sha512sum -c - \
    && mkdir -p /usr/share/powershell \
    && dotnet tool install -g Amazon.Lambda.Tools \
    && dotnet tool install --add-source / --tool-path /usr/share/powershell --version $powershell_version PowerShell.Linux.arm64 \
    && dotnet nuget locals all --clear \
    && rm PowerShell.Linux.arm64.$powershell_version.nupkg \
    && ln -s /usr/share/powershell/pwsh /usr/bin/pwsh \
    && chmod 755 /usr/share/powershell/pwsh \
    # To reduce image size, remove the copy nupkg that nuget keeps.
    && find /usr/share/powershell -print | grep -i '.*[.]nupkg$' | xargs rm

There are probably better ways to do this, but I wanted to try out dotnet 8 preview, and this lets me do it.

Tag summary

Content type

Image

Digest

sha256:d89666c7c

Size

665.1 MB

Last updated

about 3 years ago

docker pull smashedtoatoms/al2-dotnet8-preview-aot-builder