2pac
2pac

Reputation: 73

Add shared library to a AWS Lambda Go binary

Context

I'm developing an AWS Lambda function using Go and one of my dependencies is gopkg.in/h2non/bimg.v1 which has a dependency: libvips 7.42+ or 8+ (8.4+ recommended).

Problem

The problem is that in my local machine the lambda handler is working, but when I deploy it this error occures:

START RequestId: b4becbd1-3fca-4aed-9574-8df0e3d13c9e Version: $LATEST
/var/task/main: error while loading shared libraries: libvips.so.42: cannot open shared object file: No such file or directory
END RequestId: b4becbd1-3fca-4aed-9574-8df0e3d13c9e
REPORT RequestId: b4becbd1-3fca-4aed-9574-8df0e3d13c9e  Duration: 42.36 ms  Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 12 MB  
RequestId: b4becbd1-3fca-4aed-9574-8df0e3d13c9e Process exited before completing request

My build command is:

GOOS=linux GOARCH=amd64 go build -o main main.go

What I tried

I tried to build it with c-shared option enabled:

GOOS=linux GOARCH=amd64 go build -buildmode=c-shared -o main main.go

But got an error too, a different one tho;

START RequestId: 9b90df21-1025-463b-89b1-1a4ee31f295c Version: $LATEST
fork/exec /var/task/main: permission denied: PathError
null
END RequestId: 9b90df21-1025-463b-89b1-1a4ee31f295c
REPORT RequestId: 9b90df21-1025-463b-89b1-1a4ee31f295c  Duration: 0.77 ms   Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 30 MB  Init Duration: 1.84 ms  

I have two options (?):

  1. Rewrite with a fully Go library
  2. Generate a library with the libvips library packed into the Go binary.

Upvotes: 3

Views: 1081

Answers (1)

Alexey Sviridov
Alexey Sviridov

Reputation: 3510

There is another option. Firstly, it is preferable to build dependent libraries within a container launched from the AWS-provided image for local Lambda testing. For example, in a container from public.ecr.aws/lambda/provided:latest (see comments below). Then, place all the resulting .so files into a zip archive with your binary and upload zip as lambda. So, your achive content should looks like that

╰─ unzip -l function.zip
Archive:  function.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
  6764336  10-08-2020 01:01   imgconvert
   284008  06-19-2020 09:16   libexif.so.12
   276072  08-22-2019 08:14   libjpeg.so.62
   155824  12-10-2015 02:17   libpng12.so.0
   468376  10-01-2019 03:37   libtiff.so.5
 12261600  10-08-2020 00:48   libvips.so.42
  3579016  10-08-2020 00:45   libwebp.so.7
    85328  10-08-2020 00:45   libwebpdemux.so.2
   205696  10-08-2020 00:45   libwebpmux.so.3

Upvotes: 5

Related Questions