Building AWS Lambda functions with .NET Core 3.1 on Amazon Linux 2 using Windows Terminal
Tags: csharp, dotnetcore, windows, aws
Published: 2020-04-02 22:46:46

.NET Core 3.1 is now officially supported by [AWS Lambda](https://aws.amazon.com/lambda/). That's a big deal, because .NET Core 3.1 introduces [_ReadyToRun_](https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#readytorun-images), 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.](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
1. Download the _Amazon Linux 2_ image for WSL from https://github.com/yosukes-dev/AmazonWSL
1. Unzip the _Amazon2.zip_ into a permanent location (e.g. _C:\Amazon2_).
1. Run _Amazon.exe_ to extract _rootfs_ and register it with WSL.

## Add Amazon Linux 2 to Windows Terminal
[Windows Terminal](https://www.microsoft.com/en-us/p/windows-terminal-preview/9n0dx20hk701) 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.
1. Add the following snippet to _Windows Terminal_ settings. The icon path is assuming _Amazon2.zip_ was extracted into _C:\Amazon2_ folder.
    ```json
    {
        "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_.
1. Register the Microsoft package repository.
    ```bash
    rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
    ```
1. Install .NET Core 3.1 and misc. required utilities
    ```bash
    yum install -y dotnet-sdk-3.1 dotnet-runtime-3.1 git zip
    ```

## (Optional) Install VS Code Remote
Visual Studio Code supports [remote development](https://code.visualstudio.com/docs/remote/remote-overview), 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_.
1. Install utilities required by _VS Code Remote_ extension
    ```bash
    yum install -y wget glibc libgcc libstdc++ python ca-certificates tar
    ```
1. Invoke `code` command to trigger the installation of the _VS Code Remote_ extension
    ```bash
    code
    ```
1. 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](https://aws.amazon.com/blogs/compute/announcing-aws-lambda-supports-for-net-core-3-1/) and see how to take advantage of the _ReadyToRun_ capability. 

Starting with [LambdaSharp](https://lambdasharp.net/) 0.7.0.12, all code compiled on _Amazon Linux 2_ will take advantage of _ReadyToRun_ automatically.

Happy Hacking!