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: override cmd in DockerImageFunctionProps #30330

Open
1 of 2 tasks
jstrunk opened this issue May 24, 2024 · 5 comments
Open
1 of 2 tasks

lambda: override cmd in DockerImageFunctionProps #30330

jstrunk opened this issue May 24, 2024 · 5 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved. p2

Comments

@jstrunk
Copy link
Contributor

jstrunk commented May 24, 2024

Describe the feature

Add a command prop to DockerImageFunctionProps to override the value specified in https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.AssetImageCodeProps.html#cmd .

Use Case

I have a Docker image with multiple Lambda handlers. I would like to create the DockerImageCode once so it will build the image just once and then when creating each DockerImageFunction, I can specify the function to execute with command.

Proposed Solution

The synthesized CloudFormation for the AWS::Lambda::Function will include the value from the command prop as Command in ImageConfig. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-imageconfig.html#cfn-lambda-function-imageconfig-command

Other Information

It is possible to override CMD from the dockerfile using https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.AssetImageCodeProps.html#cmd , but that will result in multiple Docker images.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.143.0

Environment details (OS name and version, etc.)

MacOS 14.4.1 intel

@jstrunk jstrunk added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels May 24, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label May 24, 2024
@jstrunk
Copy link
Contributor Author

jstrunk commented May 24, 2024

Now that I understand the code involved, there are some design questions to resolve first.

https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L1001-L1004

      imageConfig: undefinedIfNoKeys({
        command: code.image?.cmd,
        entryPoint: code.image?.entrypoint,
        workingDirectory: code.image?.workingDirectory,
      }),

DockerImageFunction extends Function and uses the same constructor with a code property. In order to enable reuse of the DockerImageCode object, I think we'd have to add the command prop to FunctionProps. We should probably allow overrides of entryPoint and workingDirectory, too.

I propose a new interface called ImageConfig from AssetImageCodeProps:

/**
 * Docker image options
 **/
export interface ImageConfig {
  /**
   * Specify or override the CMD on the specified Docker image or Dockerfile.
   * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`.
   * @see https://docs.docker.com/engine/reference/builder/#cmd
   * @default - use the CMD specified in the docker image or Dockerfile.
   */
  readonly cmd?: string[];

  /**
   * Specify or override the ENTRYPOINT on the specified Docker image or Dockerfile.
   * An ENTRYPOINT allows you to configure a container that will run as an executable.
   * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`.
   * @see https://docs.docker.com/engine/reference/builder/#entrypoint
   * @default - use the ENTRYPOINT in the docker image or Dockerfile.
   */
  readonly entrypoint?: string[];

  /**
   * Specify or override the WORKDIR on the specified Docker image or Dockerfile.
   * A WORKDIR allows you to configure the working directory the container will use.
   * @see https://docs.docker.com/engine/reference/builder/#workdir
   * @default - use the WORKDIR in the docker image or Dockerfile.
   */
  readonly workingDirectory?: string;
}

and update FunctionProps with:

  /**
   * Specify or override Docker image asset properties for this specific function.
   **/
  readonly imageConfig: ImageConfig;

Finally, we can update the CfnFunction call with:

      imageConfig: undefinedIfNoKeys({
        command: props.imageConfig?.cmd || code.image?.cmd,
        entryPoint: props.imageConfig?.entrypoint || code.image?.entrypoint,
        workingDirectory: props.imageConfig?.workingDirectory || code.image?.workingDirectory,
      }),

@jstrunk
Copy link
Contributor Author

jstrunk commented May 24, 2024

Or would it be better to use the generated interface: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.CfnFunction.ImageConfigProperty.html ?

@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels May 28, 2024
@khushail khushail added p2 and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Jun 17, 2024
@pahud
Copy link
Contributor

pahud commented Jun 17, 2024

Are you asking for multiple DockerImageFunction sharing the same image asset with different command. Is it correct?

image

And if you check the assets.json from cdk.out there will be only one image asset but they are having their own custom command.

% cat cdk.out/dummy-stack.assets.json | jq '.dockerImages | keys' 
[
  "5151a43fb2ec70a430a9af0b3c3c6a5eda05076c464ab1a8657d01a6a7f5b97f"
]

I think it already works as you expect, doesn't it?

@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 17, 2024
@pahud pahud changed the title (aws_lambda): override cmd in DockerImageFunctionProps lambda: override cmd in DockerImageFunctionProps Jun 17, 2024
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jun 20, 2024
@jstrunk
Copy link
Contributor Author

jstrunk commented Jun 20, 2024

That's neat! I think the documentation could be improved then. It's not clear to me that multiple calls to DockerImageCode.fromImageAsset with the same path will only build the image once.

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Jun 20, 2024
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 feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

3 participants