DEV Community

Steve Bjorg for LambdaSharp

Posted on • Updated on

Building AWS Lambda functions with .NET Core 3.1 on Amazon Linux 2 using Windows Terminal

.NET Core 3.1 is now officially supported by AWS Lambda. That's a big deal, because .NET Core 3.1 introduces ReadyToRun, an ahead-of-time compilation technology that can reduce JIT overhead for cold starts. However, to take advantage of it, the code must be compiled under Amazon Linux 2. This can be achieved by spinning up an EC2 instance, setting up a build job with CodeBuild, or, much more simply, by taking advantage of the Windows Subsystem for Linux (WSL) in Windows 10!

This article describes how to enable and configure WSL for building .NET Core 3.1 code under Amazon Linux 2 and take advantage of the ReadyToRun feature.

Install Amazon Linux 2 for WSL

  1. Enable WSL on Windows 10.
  2. Download the Amazon Linux 2 image for WSL from https://github.com/yosukes-dev/AmazonWSL
  3. Unzip the Amazon2.zip into a permanent location (e.g. C:\Amazon2).
  4. Run Amazon.exe to extract rootfs and register it with WSL.

Add Amazon Linux 2 to Windows Terminal

Windows Terminal is a terminal emulator for Windows 10 written by Microsoft. It includes support for the Command Prompt, PowerShell, WSL, SSH, and more. The following settings make it trivial to open a bash shell directly in Amazon Linux 2.

  1. Open Windows Terminal settings.
  2. Add the following snippet to Windows Terminal settings. The icon path is assuming Amazon2.zip was extracted into C:\Amazon2 folder.

    {
        "guid": "{3dffc929-1f2e-44cc-8253-9635e0298f6b}",
        "hidden": false,
        "name": "Amazon Linux 2",
        "commandline": "wsl.exe -d Amazon2",
        "startingDirectory" : "C:\\",
        "icon": "C:\\Amazon2\\assets\\AWS-icon.png"
    }
    

Install .NET Core 3.1 on Amazon Linux 2

The following steps install .NET Core 3.1 and some utilities.

  1. Open Amazon Linux 2 in Windows Terminal.
  2. Register the Microsoft package repository.

    rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
    
  3. Install .NET Core 3.1 and misc. required utilities

    yum install -y dotnet-sdk-3.1 dotnet-runtime-3.1 git zip
    

(Optional) Install VS Code Remote

Visual Studio Code supports remote development, which allows files to be edited from the VS Code in Windows, while all commands are executed on Amazon Linux 2.

  1. Open Amazon Linux 2 in Windows Terminal.
  2. Install utilities required by VS Code Remote extension

    yum install -y wget glibc libgcc libstdc++ python ca-certificates tar
    
  3. Invoke code command to trigger the installation of the VS Code Remote extension

    code
    
  4. Click Allow Access when prompted by Windows Defender.

Conclusion

Now that Amazon Linux 2 is setup on WSL, head over to the official .NET Core 3.1 announcement for AWS Lambda and see how to take advantage of the ReadyToRun capability.

Starting with LambdaSharp 0.7.0.12, all code compiled on Amazon Linux 2 will take advantage of ReadyToRun automatically.

Happy Hacking!

Top comments (0)