Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda: runtime must be Runtime.FROM_IMAGE when using image asset for Lambda function #29502

Closed
LajosPolya opened this issue Mar 15, 2024 · 3 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@LajosPolya
Copy link

LajosPolya commented Mar 15, 2024

Describe the bug

Neither aws_lambda.Handler.FROM_IMAGE nor cdk.aws_lambda.Runtime.FROM_IMAGE resolve when deploying a lambda using the aws_lambda.Code.fromDockerBuild.

They produce the following errors:

  1. "Error: handler must be Handler.FROM_IMAGE when using image asset for Lambda function"
    and
  2. "Error: runtime must be Runtime.FROM_IMAGE when using image asset for Lambda function"

Expected Behavior

Neither aws_lambda.Handler.FROM_IMAGE nor cdk.aws_lambda.Runtime.FROM_IMAGE should produce an error when deploying a lambda using the aws_lambda.Code.fromDockerBuild.

Current Behavior

Neither aws_lambda.Handler.FROM_IMAGE nor cdk.aws_lambda.Runtime.FROM_IMAGE resolve when deploying a lambda using the aws_lambda.Code.fromDockerBuild.

Note that I was able to resolve the first error but replacing cdk.aws_lambda.Handler.FROM_IMAGE with the string "Handler.FROM_IMAGE".

I wasn't able to resolve the runtime error this way since FunctionProps.runtime is of type Runtime not string.

Reproduction Steps

I have a repo to repsoduce the steps (use this exact commit since it's not the most recent commit):
LajosPolya/aws-cdk-templates@b92f222

Possible Solution

The interesting this about this is that both cdk.aws_lambda.Runtime.FROM_IMAGE and cdk.aws_lambda.Handler.FROM_IMAGE resolve correctly when using aws_lambda.Code.fromEcrImage instead of aws_lambda.Code.fromDockerBuild.

I have an example in this repo:
https://github.com/LajosPolya/aws-cdk-templates/tree/main/deploy-lambda-from-ecr

Another interesting fact about this is I'm able to deploy the Lambda successfully if I set the runtime to cdk.aws_lambda.Runtime.NODEJS_20_X and the handler to "index.handler". This seems to go against what the docs say: "Use Handler.FROM_IMAGE when defining a function from a Docker image."

This can be reproduced here:
LajosPolya/aws-cdk-templates@fc76814

Additional Information/Context

This bug was already opened once but was never fixed:
#25758

CDK CLI Version

2.133.0 (build dcc1e75)

Framework Version

No response

Node.js Version

v20.11.1

OS

Windows 10

Language

TypeScript

Language Version

"typescript": "~5.3.3"

Other information

No response

@LajosPolya LajosPolya added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 15, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Mar 15, 2024
@pahud
Copy link
Contributor

pahud commented Mar 15, 2024

Let's simplify the code. I guess you should use

    const dockerImageCode = lambda.Code.fromDockerBuild(
      path.join(__dirname, '../docker-function.d')
    );

    const fn = new lambda.Function(this, 'Fn', {
      runtime: lambda.Runtime.FROM_IMAGE,
      code: dockerImageCode,
      handler: lambda.Handler.FROM_IMAGE,
    });

But if you look at the source code here, DockerImageFunction is generally recommended in your use case.

/**
* Create a lambda function where the handler is a docker image
*/
export class DockerImageFunction extends Function {
constructor(scope: Construct, id: string, props: DockerImageFunctionProps) {
super(scope, id, {
...props,
handler: Handler.FROM_IMAGE,
runtime: Runtime.FROM_IMAGE,
code: props.code._bind(props.architecture),
});
}
}

Why not just use DockerImageFunction in your use case?

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Mar 15, 2024
@LajosPolya
Copy link
Author

I wasn't aware of this option. I was able to test this and it works! Thanks for the suggestion @pahud

Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

2 participants