Skip to content

Commit 5544c5a

Browse files
authored
Merge pull request #1 from aws-samples/dev
add build doc for macOS and setup GitHub Actions
2 parents 703dee1 + 587f52e commit 5544c5a

File tree

24 files changed

+104
-121
lines changed

24 files changed

+104
-121
lines changed

.cargo/config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[target.x86_64-unknown-linux-musl]
2-
linker = "musl-gcc"
2+
linker = "x86_64-unknown-linux-musl-gcc"

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
examples
1+
examples

.github/workflows/pipeline.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ $default-branch, dev ]
6+
pull_request:
7+
branches: [ $default-branch ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- name: Configure AWS credentials
21+
uses: aws-actions/configure-aws-credentials@v1
22+
with:
23+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
24+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
25+
aws-region: us-east-1
26+
27+
- name: Login to Amazon ECR
28+
id: login-ecr
29+
uses: aws-actions/amazon-ecr-login@v1
30+
31+
- name: Build
32+
run: DOCKER_BUILDKIT=1 docker build -f Dockerfile.x86 -t aws-lambda-adapter:latest .

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ log = "0.4.14"
1111
env_logger = "0.8.3"
1212
tokio = { version = "1.4", features = ["macros", "io-util", "sync", "rt-multi-thread", "process"] }
1313
tokio-retry = "0.3"
14-
lambda_http = "0.4.0"
14+
lambda_http = "0.4.1"
1515
reqwest = { version = "0.11", features = ["json"] }
1616
http = "0.2.4"
1717
openssl-sys = "0.9.61"

Dockerfile.mac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM scratch
2+
COPY target/x86_64-unknown-linux-musl/release/bootstrap /opt/bootstrap

Dockerfile renamed to Dockerfile.x86

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ RUN rpm --rebuilddb && yum install -y yum-plugin-ovl openssl-devel
33
RUN yum groupinstall -y "Development tools"
44
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
55
RUN source $HOME/.cargo/env && rustup target add x86_64-unknown-linux-musl
6-
RUN curl -o /etc/yum.repos.d/ngompa-musl-libc-epel-7.repo https://copr.fedorainfracloud.org/coprs/ngompa/musl-libc/repo/epel-7/ngompa-musl-libc-epel-7.repo
7-
RUN yum install -y musl-devel musl-gcc
6+
RUN curl -o /musl-1.2.2.tar.gz https://musl.libc.org/releases/musl-1.2.2.tar.gz \
7+
&& tar zxf /musl-1.2.2.tar.gz && cd musl-1.2.2/ \
8+
&& ./configure && make install && ln -s /usr/local/musl/bin/musl-gcc /usr/local/bin/x86_64-unknown-linux-musl-gcc
89
WORKDIR /app
910
ADD . /app
10-
RUN source $HOME/.cargo/env && cargo build --release --target=x86_64-unknown-linux-musl --features vendored
11+
RUN source $HOME/.cargo/env && CC=x86_64-unknown-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl --features vendored
1112

1213
FROM scratch AS package-stage
1314
COPY --from=build-stage /app/target/x86_64-unknown-linux-musl/release/bootstrap /opt/bootstrap

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ clean:
22
rm -rf target
33

44
build:
5-
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/awsguru
6-
DOCKER_BUILDKIT=1 docker build -t aws-lambda-adapter:latest .
5+
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
6+
DOCKER_BUILDKIT=1 docker build -f Dockerfile.x86 -t aws-lambda-adapter:latest .
7+
8+
build-mac:
9+
CC=x86_64-unknown-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl --features vendored
10+
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
11+
DOCKER_BUILDKIT=1 docker build -f Dockerfile.mac -t aws-lambda-adapter:latest .

README.md

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,64 @@ It will start lambda runtime client after receiving 200 response from the applic
1919
## How to build it?
2020

2121
AWS Lambda Adapter is written in Rust and based on [AWS Lambda Rust Runtime](https://github.com/awslabs/aws-lambda-rust-runtime).
22-
You can use GNU Make to compile it as static linked binary and package into a docker image. We provide a [Dockerfile](Dockerfile) including all the required rust toolchain and dependencies.
23-
You need to install [AWS CLI](https://aws.amazon.com/cli/) and [Docker](https://www.docker.com/get-started) to run the build.
22+
AWS Lambda executes functions in x86_64 Amazon Linux Environment. We need to cross compile the adapter to that environment.
23+
24+
### Compiling on macOS
25+
26+
First, install [rustup](https://rustup.rs/) if you haven't done it already. Then, add the `x86_64-unknown-linux-musl` target:
27+
28+
```shell
29+
$ rustup target add x86_64-unknown-linux-musl
30+
```
31+
32+
And we have to install macOS cross-compiler toolchains. `messense/homebrew-macos-cross-toolchains` can be used on both Intel chip and Apple M1 chip.
33+
34+
```shell
35+
$ brew tap messense/macos-cross-toolchains
36+
$ brew install x86_64-unknown-linux-musl
37+
```
38+
39+
And we need to inform Cargo that our project uses the newly-installed linker when building for the `x86_64-unknown-linux-musl` platform.
40+
Create a new directory called `.cargo` in your project folder and a new file called `config` inside the new folder.
41+
42+
```shell
43+
$ mkdir .cargo
44+
$ echo '[target.x86_64-unknown-linux-musl]
45+
linker = "x86_64-unknown-linux-musl-gcc"' > .cargo/config
46+
```
47+
48+
Now we can cross compile AWS Lambda Adapter.
49+
50+
```shell
51+
$ CC=x86_64-unknown-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl --features vendored
52+
```
53+
54+
Lambda Adapter binary will be placed at `target/x86_64-unkonw-linux-musl/release/bootstrap`.
55+
56+
Finally, run the following command to package lambda adapter into a docker image named "aws-lambda-adapter:latest".
57+
58+
```shell
59+
$ aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
60+
$ DOCKER_BUILDKIT=1 docker build -f Dockerfile.mac -t aws-lambda-adapter:latest .
61+
```
62+
63+
### Compiling with Docker
64+
On x86_64 Windows, Linux and macOS, you can run one command to compile Lambda Adapter with docker.
2465

2566
```shell
26-
make build
67+
$ make build
2768
```
28-
This will create a docker image called "aws-lambda-adapter:latest". In this docker image, AWS Lambda Adapter is packaged as a file "/opt/bootstrap".
69+
70+
Once the build completes, it creates a docker image called "aws-lambda-adapter:latest". AWS Lambda Adapter binary is packaged as '/opt/bootstrap' inside the docker image.
2971

3072
## How to use it?
3173

32-
To use it, copy the bootstrap binary from "aws-lambda-adapter:latest" to your container, and use it as ENTRYPOINT.
74+
To use it, copy the bootstrap binary to your container, and use it as ENTRYPOINT.
3375
Below is an example Dockerfile for packaging a nodejs application.
3476

3577
```dockerfile
3678
FROM public.ecr.aws/lambda/nodejs:14
37-
COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
79+
COPY --from=aws-lambda-adapter:latest /opt/bootstrap
3880
ENTRYPOINT ["/opt/bootstrap"]
3981
EXPOSE 8080
4082
WORKDIR "/var/task"

examples/expressjs/app/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
33
ENTRYPOINT ["/opt/bootstrap"]
44
EXPOSE 8080
55
WORKDIR "/var/task"
6-
ADD extensions/ /opt
76
ADD src/package.json /var/task/package.json
87
ADD src/package-lock.json /var/task/package-lock.json
98
RUN npm install --production

0 commit comments

Comments
 (0)