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
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.
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 theENTRYPOINT
– this is perhaps not ideal, but having looked at the contents of the/lambda-entrpoint.sh
I don’t see any concerning impactThe
/lambda-entrpoint.sh
provided in the image looks like thisSo I override the
ENTRYPOINT
in my final stage as followsThe
ARG
sets the stage to use the build arg provided. TheENV
then sets the value in the runtime environment. TheENTRYPOINT
then overrides the default entrypoint passing the handler name.