aws / aws-lambda-nodejs-runtime-interface-client

Apache License 2.0
180 stars 56 forks source link

Is it possible to install this tool globally (outside the function dir)? #50

Open danwashusen opened 2 years ago

danwashusen commented 2 years ago

In an attempt to reduce the time it takes to re-package, I'm looking at installing this tool globally (npm install -g aws-lambda-ric) before my function code it copied into the image to take advantage of dockers caching/layering.

The suggested solution (below) has no way of taking advantage of dockers layering. As we have to endure constant cURL rebuilds...

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY myFunction/* ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

# If the dependency is not in package.json uncomment the following line
# RUN npm install aws-lambda-ric

RUN npm install

It would be great to install it globally in its own step. Docker could then cache the cURL build and developers could spend their time doing something productive...

# https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/28
RUN mkdir -p /usr/local/lib/node_modules && \
    npm install -g aws-lambda-ric && \
    rm -rf \
    ./deps/curl-*/autom4te.cache \
    ./deps/curl-*/config.log \
    ./deps/curl-*/configure \
    ./deps/curl-*/docs \
    ./deps/curl-*/projects \
    ./deps/curl-*/tests \
    ./deps/curl-*/README

RUN mkdir -p "${FUNCTION_DIR}"
COPY ./build/js "${FUNCTION_DIR}"

However this results in errors from the preinstall scripts:

...
#0 22.38 tar: curl-7.78.0/CMake/OtherTests.cmake: Cannot open: No such file or directory
#0 22.38 tar: curl-7.78.0: Cannot mkdir: Permission denied
...
danwashusen commented 2 years ago

For anyone interested the following works:

...

RUN mkdir -p /tmp/ric && cd /tmp/ric && \
    npm install aws-lambda-ric && \
    rm -rf \
        node_modules/aws-lambda-ric/deps/curl-*/autom4te.cache \
        node_modules/aws-lambda-ric/deps/curl-*/config.log \
        node_modules/aws-lambda-ric/deps/curl-*/configure \
        node_modules/aws-lambda-ric/deps/curl-*/docs \
        node_modules/aws-lambda-ric/deps/curl-*/projects \
        node_modules/aws-lambda-ric/deps/curl-*/tests \
        node_modules/aws-lambda-ric/deps/curl-*/README
...

COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
COPY --from=build-image /tmp/ric/node_modules ${FUNCTION_DIR}/node_modules
e-tip commented 1 year ago

Thank's! I'll give it a try

florentGuiot commented 1 year ago

Can you explain why are you installing in the /tmp folder ?

lstellway commented 1 year ago

I had a bit of trouble getting my setup working - just sharing a more recent / complete example in case it can be helpful to anybody:

# Define custom function directory
ARG FUNCTION_DIR="/function"
ARG IMAGE_TAG="3.16.5"

FROM alpine:${IMAGE_TAG} as build-step

# Set working directory to function root directory
ARG FUNCTION_DIR
WORKDIR ${FUNCTION_DIR}

# Install dependencies
RUN apk --upgrade --no-cache add \
    --virtual build-deps \
        g++ \
        make \
        cmake \
        unzip \
        curl-dev \
        automake \
        autoconf \
        tar \
        gzip \
        libtool \
        libexecinfo-dev \
        python3 \
    && apk --no-cache add nodejs npm \
    && npm install -g aws-lambda-ric \
    # Clean unused artifacts to reduce image size
    && apk del build-deps \
    && rm -rf \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*/autom4te.cache \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*/config.log \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*/configure \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*/docs \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*/projects \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*/tests \
        /usr/local/lib/node_modules/aws-lambda-ric/deps/curl-*.tar.gz

COPY app.js package.json ${FUNCTION_DIR}

RUN npm install

ENTRYPOINT ["/usr/bin/npx", "aws-lambda-ric"]

CMD [ "app.handler" ]