Andrew Wei
Andrew Wei

Reputation: 2080

How to get curl in Lambda for Node 12.x (Amazon2)

The new nodejs runtimes in Lambda is based on Amazon2, which does not have curl preinstalled. Therefore giving a curl: command not found when trying to use it.

How do I get curl back in the new nodejs 10.x and 12.x runtimes?

I cannot find a curl lambda layer.

Upvotes: 0

Views: 2433

Answers (1)

Andrew Wei
Andrew Wei

Reputation: 2080

The recommended way is to use the request client for nodejs. However if you're too far invested into using curl while upgrading from node 8.x to node 12.x, you're going to need to include the curl binary, and required libraries yourself.

I created a lambda layer arn:aws:lambda:us-east-1:310788757301:layer:curl:15. You're free to use it, however I'm using this for personal use, therefore I wouldn't recommend using this in a production environment. Instead, I'll provide you a guide on building your own curl lambda layer.

I uploaded curl 7.69.1 (x86_64-redhat-linux-gnu), and used ldd /opt/bin/curl to figure out which libraries were missing. After including all the required libraries, the output is the following:

START RequestId: c9e7ed3f-f1b7-4e93-98fe-dea302385723 Version: $LATEST
2020-03-29T23:37:19.899Z    c9e7ed3f-f1b7-4e93-98fe-dea302385723    INFO        linux-vdso.so.1 (0x00007ffdf2f76000)
    libcurl.so.4 => /opt/lib/libcurl.so.4 (0x00007faa5f5c8000)
    libmetalink.so.3 => /opt/lib/libmetalink.so.3 (0x00007faa5f3b9000)
    libssl.so.10 => /lib64/libssl.so.10 (0x00007faa5f14a000)
    libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007faa5ecf6000)
    libz.so.1 => /lib64/libz.so.1 (0x00007faa5eae1000)
    librt.so.1 => /lib64/librt.so.1 (0x00007faa5e8d9000)
    libc.so.6 => /lib64/libc.so.6 (0x00007faa5e52e000)
    libcares.so.2 => /opt/lib/libcares.so.2 (0x00007faa5e31e000)
    libssh2.so.1 => /opt/lib/libssh2.so.1 (0x00007faa5e0f6000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007faa5deaa000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007faa5dbc6000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007faa5d995000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007faa5d791000)
    libldap-2.4.so.2 => /opt/lib/libldap-2.4.so.2 (0x00007faa5d540000)
    libexpat.so.1 => /opt/lib/libexpat.so.1 (0x00007faa5d318000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007faa5d114000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007faa5cef6000)
    /lib64/ld-linux-x86-64.so.2 (0x00007faa5f84d000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007faa5cce7000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007faa5cae3000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x00007faa5c8cd000)
    liblber-2.4.so.2 => /opt/lib/liblber-2.4.so.2 (0x00007faa5c6be000)
    libsasl2.so.2 => /opt/lib/libsasl2.so.2 (0x00007faa5c4a4000)
    libssl3.so => /opt/lib/libssl3.so (0x00007faa5c251000)
    libsmime3.so => /opt/lib/libsmime3.so (0x00007faa5c024000)
    libnss3.so => /opt/lib/libnss3.so (0x00007faa5bcdc000)
    libnssutil3.so => /lib64/libnssutil3.so (0x00007faa5baad000)
    libplds4.so => /lib64/libplds4.so (0x00007faa5b8a9000)
    libplc4.so => /lib64/libplc4.so (0x00007faa5b6a4000)
    libnspr4.so => /lib64/libnspr4.so (0x00007faa5b468000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00007faa5b241000)
    libcrypt.so.1 => /opt/lib/libcrypt.so.1 (0x00007faa5b00a000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007faa5ada6000)
    libfreebl3.so => /lib64/libfreebl3.so (0x00007faa5aba3000)

END RequestId: c9e7ed3f-f1b7-4e93-98fe-dea302385723
REPORT RequestId: c9e7ed3f-f1b7-4e93-98fe-dea302385723  Duration: 515.09 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 70 MB  Init Duration: 115.35 ms    

I downloaded a mix of RedHat 6, and Centos 6 libraries, and it seems to work. This is a list of what you're going to need.

.
├── bin
│   └── curl
└── lib
    ├── libcares.so.2 -> libcares.so.2.1.0
    ├── libcares.so.2.1.0
    ├── libcrypt-2.12.so
    ├── libcrypt.so.1 -> libcrypt-2.12.so
    ├── libcurl.so.4 -> libcurl.so.4.6.0
    ├── libcurl.so.4.6.0
    ├── libexpat.so.1 -> libexpat.so.1.5.2
    ├── libexpat.so.1.5.2
    ├── liblber-2.4.so.2 -> liblber-2.4.so.2.10.3
    ├── liblber-2.4.so.2.10.3
    ├── libldap-2.4.so.2 -> libldap-2.4.so.2.10.3
    ├── libldap-2.4.so.2.10.3
    ├── libmetalink.so.3 -> libmetalink.so.3.1.0
    ├── libmetalink.so.3.1.0
    ├── libnss3.so
    ├── libnsspem.so
    ├── libsasl2.so.2 -> libsasl2.so.2.0.23
    ├── libsasl2.so.2.0.23
    ├── libsmime3.so
    ├── libssh2.so.1 -> libssh2.so.1.0.1
    ├── libssh2.so.1.0.1
    └── libssl3.so

You may view & download a copy of my lambda layer here: https://github.com/andey/curl-lambda-layer

Upvotes: 4

Related Questions