|
| 1 | +# Compile for AWS Lambda |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +For a native executable to run in an AWS Lambda function, it must be compatible |
| 6 | +with the runtime's operating system. This repository is an example of using |
| 7 | +Docker to build a custom C++ executable that runs on AWS Lambda. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- [Docker](https://docs.docker.com/) |
| 12 | +- [Make](https://www.gnu.org/software/make/) |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Enter this directory and run `make`: |
| 17 | + |
| 18 | +```console |
| 19 | +$ cd /path/to/compile-for-aws-lambda |
| 20 | +$ make |
| 21 | +``` |
| 22 | + |
| 23 | +The `build` directory now contains binaries that can be packaged in the ZIP file |
| 24 | +for an AWS Lambda function: |
| 25 | + |
| 26 | +``` |
| 27 | +$ tree build |
| 28 | +build |
| 29 | +├── libsndfile.so -> libsndfile.so.1 |
| 30 | +├── libsndfile.so.1 -> libsndfile.so.1.0.29 |
| 31 | +├── libsndfile.so.1.0.29 |
| 32 | +└── sine |
| 33 | +``` |
| 34 | + |
| 35 | +The example `sine` program generates a sine wave at the specified frequency (Hz) |
| 36 | +and writes it as a WAV file: |
| 37 | +```console |
| 38 | +$ cd build |
| 39 | +$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. |
| 40 | +$ ./sine 2600 /tmp/2600.wav |
| 41 | +$ file /tmp/2600.wav |
| 42 | +2600.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 24 bit, mono 44100 Hz |
| 43 | +``` |
| 44 | + |
| 45 | +To run the program in a Lambda function, use the |
| 46 | +[child_process](https://nodejs.org/docs/latest-v12.x/api/child_process.html) |
| 47 | +module in Node.js, or the |
| 48 | +[subprocess](https://docs.python.org/3.8/library/subprocess.html) module in |
| 49 | +Python. |
| 50 | + |
| 51 | +## Details |
| 52 | + |
| 53 | +From the [Running Arbitrary Executables in AWS |
| 54 | +Lambda](https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/) |
| 55 | +blog post: |
| 56 | + |
| 57 | +> If you compile your own binaries, ensure that they’re either statically linked |
| 58 | +> or built for the matching version of Amazon Linux. |
| 59 | +
|
| 60 | +The example `sine` program in this repository uses the LGPL-licensed |
| 61 | +[libsndfile](https://github.com/erikd/libsndfile) library. To comply with the |
| 62 | +LPGL, libsndfile must be used as a shared library; it cannot be statically |
| 63 | +linked. |
| 64 | + |
| 65 | +### Dockerfile |
| 66 | + |
| 67 | +To compile binaries that are compatible with Amazon Linux 2, the |
| 68 | +[`Dockerfile`](./Dockerfile) starts from an Amazon Linux 2 base image: |
| 69 | + |
| 70 | +```docker |
| 71 | +FROM amazonlinux:2.0.20200406.0 |
| 72 | +``` |
| 73 | + |
| 74 | +The Dockerfile then builds and installs `libsndfile` and `sine`. |
| 75 | + |
| 76 | +Binaries compiled in this environment will run on any AWS Lambda runtime that's |
| 77 | +based on Amazon Linux 2, such as the Node.js 12 and Python 3.8 runtimes. See |
| 78 | +[AWS Lambda |
| 79 | +runtimes](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html) for |
| 80 | +a complete list. |
| 81 | + |
| 82 | +### Makefile |
| 83 | + |
| 84 | +The [`Makefile`](./Makefile) creates a container from the Docker image and |
| 85 | +copies the built artifacts from the container to the host system. |
0 commit comments