skip to Main Content

I previously asked this question and got a solution to expanding arguments in a dockerfile.

Expand ARG/ENV in CMD dockerfile

I think with AWS Lambdas the handler name has to be the first argument. I have a parameterized jenkins pipeline that takes in a parameter named LAMBDA_NAME and I append the handler to it.

When I hard-code the lambda name, there are no issues.

ARG LAMBDA_NAME
ENV LAMBDA_HANDLER="${LAMBDA_NAME}.handler"
RUN echo "${LAMBDA_HANDLER}"
CMD [ "sourceproducer.handler" ]

But what I am looking to do is to use the LAMBDA_HANDLER variable above. When echo is called it correctly spits out "sourceproducer.handler"

At first, I tried to run it in exec form and ran into this issue
([ERROR] Runtime.MalformedHandlerName: Bad handler ‘${LAMBDA_HANDLER}’: not enough values to unpack (expected 2, got 1)

ARG LAMBDA_NAME
ENV LAMBDA_HANDLER="${LAMBDA_NAME}.handler"
RUN echo "${LAMBDA_HANDLER}"
CMD [ "${LAMBDA_HANDLER}" ]

The question answered above, said to use exec form instead, so I tried both of these ways of running it

CMD "${LAMBDA_HANDLER}"

And 

CMD [ "/bin/sh", "-c", "${LAMBDA_HANDLER}" ]

However, now because I am using exec form "/bin/sh" is the first argument and it doesn’t conform with AWS Lambda wanting the handler as the first argument.

I thought perhaps switching the order of the arguments would help, but it didn’t.

CMD ["${LAMBDA_HANDLER}", "/bin/sh", "-c" ]

So, the question is how do I pass in LAMBDA_HANDLER argument AND have it be the first argument? Use entrypoint or custom shell script? Open to ideas about the approach

2

Answers


  1. Chosen as BEST ANSWER

    I did end up solving this doing something similar to what Hans Kilian suggested. Hardcoded a lambda name, and had that call the specific handler that I required.


  2. I ran across this answer when suffering from the same issue, but came to a possibly simpler solution.
    As I could not provide the handler through CMD I simply overrode the ENTRYPOINT – this is perhaps not ideal, but having looked at the contents of the /lambda-entrpoint.sh I don’t see any concerning impact

    The /lambda-entrpoint.sh provided in the image looks like this

    sh-4.2# cat /lambda-entrypoint.sh 
    
    #!/bin/sh
    # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    
    if [ $# -ne 1 ]; then
      echo "entrypoint requires the handler name to be the first argument" 1>&2
      exit 142
    fi
    export _HANDLER="$1"
    
    RUNTIME_ENTRYPOINT=/var/runtime/bootstrap
    if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
      exec /usr/local/bin/aws-lambda-rie $RUNTIME_ENTRYPOINT
    else
      exec $RUNTIME_ENTRYPOINT
    fi
    

    So I override the ENTRYPOINT in my final stage as follows

    FROM runtime
    ARG FUNCTION_HANDLER
    ENV FUNCTION_HANDLER=${FUNCTION_HANDLER}
    COPY --from=publish ./app/out .
    ENTRYPOINT /lambda-entrypoint.sh ${FUNCTION_HANDLER}
    

    The ARG sets the stage to use the build arg provided. The ENV then sets the value in the runtime environment. The ENTRYPOINT then overrides the default entrypoint passing the handler name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search