To build a lambda function, we need to produce a binary that is compatible with Amazon Linux. You can use three methods to build a corresponding binary:
- Build it with a Linux distribution (compatible with x86_64).
- Build it in a Docker container of Amazon Linux.
- Build it with the musl standard C library.
We will use the latter method, because it minimizes the external dependencies of the produced binary. First, you have to install the musl library, which you can get here: https://www.musl-libc.org/.
I did this with the following commands:
git clone git://git.musl-libc.org/musl
cd musl
./configure
make
sudo make install
But if there is a package for your operating system, you should to install that instead.
To build the code with the musl library we have to use x86_64-unknown-linux-musl as the target value. But we can set this target as the default for this project with a configuration file for cargo. Add a .cargo/config file to the project's folder and add the following configuration to it:
[build]
target = "x86_64-unknown-linux-musl"
Make sure the compiler supports musl or add it using rustup:
rustup target add x86_64-unknown-linux-musl
Now you can simply build the lambda using the cargo build command. That produces a binary that's compiled with the musl library that we can upload to AWS.