Using rust in AWS lambda

Hello All,

I'm trying to use Rust for a lambda function running in AWS. To build the binary I'm using the following docker builder image.

FROM lambci/lambda:build-provided.al2


ARG RUST_VERSION=1.55.0

RUN yum install -y jq openssl-devel

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \

| CARGO_HOME=/cargo RUSTUP_HOME=/rustup sh -s -- -y --profile minimal --default-toolchain $RUST_VERSION --target x86_64-unknown-linux-gnu

ADD build.sh /usr/local/bin/

VOLUME ["/code"]

WORKDIR /code

RUN ["chmod", "+x", "/usr/local/bin/build.sh"]

ENTRYPOINT ["/usr/local/bin/build.sh"]

This compiles fine but when I invoke the lambda I get the error /var/task/bootstrap: /lib64/libc.so.6: version GLIBC_2.18' not found (required by /var/task/bootstrap)`

Was wondering if any one here has come across this issue before and might be able to tell me where I've gone wrong.

Thanks in advance,

Rich

You need x86_64-unknown-linux-musl target for the AWS lambda.

4 Likes

So I tried musl originally but then I got an openssl error

What's the full error you've got? Maybe the environment running the lambda have incompatible version of the openssl library.

It fails to build with:

error: failed to run custom build command for `openssl-sys v0.9.67`

Caused by:
  process didn't exit successfully: `/code/target/lambda/release/build/openssl-sys-aee1993a25f6e374/build-script-main` (exit status: 101)

  --- stdout

  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR = /usr/bin/openssl
  --- stderr
  thread 'main' panicked at 'OpenSSL library directory does not exist: /usr/bin/openssl/lib', /cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.67/build/main.rs:66:9

Also just to add at first I didn't set the OpenSsl directory but when I didn't it said it couldn't find it

Once I managed to fix the issue relating to actually finding openssl it was able to build and deploy to a lambda, thank you Hyeonu for validating the musl approach was correct.

1 Like

I wanted to comment as I ran into this same issue, albeit building from an AWS EC2 instance rather than Docker. This Link was very helpful, and I essentially followed it's approach:

  • added to Crates.toml: openssl = { version = "*", features = ["vendored"] }
  • Build process:
# install libssl binaries
sudo apt-get install pkg-config libssl-dev

# add unknown-linux-musl as a target
sudo apt-get install pkg-config musl-tools
rustup target add x86_64-unknown-linux-musl

# compile and zip
cargo build --target x86_64-unknown-linux-musl --release --bin my_cool_lambda
cp target/x86_64-unknown-linux-musl/release/my_cool_lambda bootstrap
zip lambda.zip bootstrap

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.