Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[target.x86_64-unknown-linux-musl]
linker = "musl-gcc"
linker = "x86_64-unknown-linux-musl-gcc"
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
examples
examples
32 changes: 32 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Rust

on:
push:
branches: [ $default-branch, dev ]
pull_request:
branches: [ $default-branch ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build
run: DOCKER_BUILDKIT=1 docker build -f Dockerfile.x86 -t aws-lambda-adapter:latest .
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ log = "0.4.14"
env_logger = "0.8.3"
tokio = { version = "1.4", features = ["macros", "io-util", "sync", "rt-multi-thread", "process"] }
tokio-retry = "0.3"
lambda_http = "0.4.0"
lambda_http = "0.4.1"
reqwest = { version = "0.11", features = ["json"] }
http = "0.2.4"
openssl-sys = "0.9.61"
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
COPY target/x86_64-unknown-linux-musl/release/bootstrap /opt/bootstrap
7 changes: 4 additions & 3 deletions Dockerfile → Dockerfile.x86
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ RUN rpm --rebuilddb && yum install -y yum-plugin-ovl openssl-devel
RUN yum groupinstall -y "Development tools"
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
RUN source $HOME/.cargo/env && rustup target add x86_64-unknown-linux-musl
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
RUN yum install -y musl-devel musl-gcc
RUN curl -o /musl-1.2.2.tar.gz https://musl.libc.org/releases/musl-1.2.2.tar.gz \
&& tar zxf /musl-1.2.2.tar.gz && cd musl-1.2.2/ \
&& ./configure && make install && ln -s /usr/local/musl/bin/musl-gcc /usr/local/bin/x86_64-unknown-linux-musl-gcc
WORKDIR /app
ADD . /app
RUN source $HOME/.cargo/env && cargo build --release --target=x86_64-unknown-linux-musl --features vendored
RUN source $HOME/.cargo/env && CC=x86_64-unknown-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl --features vendored

FROM scratch AS package-stage
COPY --from=build-stage /app/target/x86_64-unknown-linux-musl/release/bootstrap /opt/bootstrap
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ clean:
rm -rf target

build:
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/awsguru
DOCKER_BUILDKIT=1 docker build -t aws-lambda-adapter:latest .
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
DOCKER_BUILDKIT=1 docker build -f Dockerfile.x86 -t aws-lambda-adapter:latest .

build-mac:
CC=x86_64-unknown-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl --features vendored
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
DOCKER_BUILDKIT=1 docker build -f Dockerfile.mac -t aws-lambda-adapter:latest .
54 changes: 48 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,64 @@ It will start lambda runtime client after receiving 200 response from the applic
## How to build it?

AWS Lambda Adapter is written in Rust and based on [AWS Lambda Rust Runtime](https://github.com/awslabs/aws-lambda-rust-runtime).
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.
You need to install [AWS CLI](https://aws.amazon.com/cli/) and [Docker](https://www.docker.com/get-started) to run the build.
AWS Lambda executes functions in x86_64 Amazon Linux Environment. We need to cross compile the adapter to that environment.

### Compiling on macOS

First, install [rustup](https://rustup.rs/) if you haven't done it already. Then, add the `x86_64-unknown-linux-musl` target:

```shell
$ rustup target add x86_64-unknown-linux-musl
```

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.

```shell
$ brew tap messense/macos-cross-toolchains
$ brew install x86_64-unknown-linux-musl
```

And we need to inform Cargo that our project uses the newly-installed linker when building for the `x86_64-unknown-linux-musl` platform.
Create a new directory called `.cargo` in your project folder and a new file called `config` inside the new folder.

```shell
$ mkdir .cargo
$ echo '[target.x86_64-unknown-linux-musl]
linker = "x86_64-unknown-linux-musl-gcc"' > .cargo/config
```

Now we can cross compile AWS Lambda Adapter.

```shell
$ CC=x86_64-unknown-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl --features vendored
```

Lambda Adapter binary will be placed at `target/x86_64-unkonw-linux-musl/release/bootstrap`.

Finally, run the following command to package lambda adapter into a docker image named "aws-lambda-adapter:latest".

```shell
$ aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
$ DOCKER_BUILDKIT=1 docker build -f Dockerfile.mac -t aws-lambda-adapter:latest .
```

### Compiling with Docker
On x86_64 Windows, Linux and macOS, you can run one command to compile Lambda Adapter with docker.

```shell
make build
$ make build
```
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".

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.

## How to use it?

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

```dockerfile
FROM public.ecr.aws/lambda/nodejs:14
COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
COPY --from=aws-lambda-adapter:latest /opt/bootstrap
ENTRYPOINT ["/opt/bootstrap"]
EXPOSE 8080
WORKDIR "/var/task"
Expand Down
1 change: 0 additions & 1 deletion examples/expressjs/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
ENTRYPOINT ["/opt/bootstrap"]
EXPOSE 8080
WORKDIR "/var/task"
ADD extensions/ /opt
ADD src/package.json /var/task/package.json
ADD src/package-lock.json /var/task/package-lock.json
RUN npm install --production
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion examples/expressjs/app/extensions/cloudwatch/manifest.json

This file was deleted.

Binary file not shown.
Empty file.
1 change: 0 additions & 1 deletion examples/nginx/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ WORKDIR "/tmp"
COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
ADD config/ /etc/nginx/
ADD images/ /usr/share/nginx/html/images
ADD extensions/ /opt
ENTRYPOINT ["/opt/bootstrap"]
CMD ["nginx", "-g", "daemon off;"]

This file was deleted.

1 change: 0 additions & 1 deletion examples/nginx/app/extensions/cloudwatch/manifest.json

This file was deleted.

Binary file not shown.
Empty file.
1 change: 0 additions & 1 deletion examples/springboot/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
ENTRYPOINT ["/opt/bootstrap"]
EXPOSE 8080
WORKDIR /opt
ADD extensions/ /opt
COPY --from=build-image /task/target/petstore-0.0.1-SNAPSHOT.jar /opt
CMD ["java", "-jar", "petstore-0.0.1-SNAPSHOT.jar"]

This file was deleted.

This file was deleted.

Binary file not shown.
Empty file.