I write CMD ["main.handler"] in Dockerfile
, but I’m trying to change handler function for reusing same container image.
How can you fix my CDK code?
I found CodeImageConfig interface, but have no idea how to use this option.
CDK code
const fn = new lambda.Function(scope, `lambda-fn`, {
code: new lambda.AssetImageCode("./lambda/myapp", {
ignoreMode: IgnoreMode.DOCKER,
file: "lambda.Dockerfile",
}),
handler: lambda.Handler.FROM_IMAGE,
runtime: lambda.Runtime.FROM_IMAGE,
tracing: lambda.Tracing.ACTIVE,
environment: {
SAMPLE_ENV_VAR: "sample_env_var",
},
});
lambda.Dockerfile
FROM public.ecr.aws/lambda/python:3.9
COPY src/ /var/task/
CMD ["main.handler"]
2
Answers
You’re using
AssetImageCode
, which has acmd
prop. From the docs:Using CDK v2 with Python and
DockerImageFunction
for a project currently – the accepted answer did not work for me.I am using an AWS managed image in my case – specifically, the
public.ecr.aws/lambda/python
image.Dockerfile
This line is at the top of my
Dockerfile
(source):To be on the safe side, I also specified
AMD64
as the platform in CDK code:CDK – Attempt #1
In my CDK code, the suggested syntax did not work for me.
Error from CloudWatch logs:
CDK – Attempt #2
I also tried the following, after consulting from the official
Dockerfile
for the image:The same error was observed from the CloudWatch logs when attempting to run the affected AWS Lambda function.
CDK – Attempt #3
What did work, was just passing in
cmd
with the handler as the first argument, as I originally had in myDockerfile
:All works fine now. Everything is good 🙂