Lambda source code updating issue during its container image building via cdk

0

I am making a lambda function through DockerImageFunction with DockerImageCode.fromImageAsset() from aws-cdk-lib/aws-lambda. The Dockerfile contains a collection of codec tools, and the lambda handler function code. Moreover, those codec tools takes 5 min to build, and hardly ever needs to be updated. The problem is that there are lot of lambda functions use the same codec tools, and the same codec tools get rebuilded everytime my lambda handler function code has been commited. Is there a way to craete those codec tools first, and for those lambda functions which need the tools during image building, just import it? Thanks!

질문됨 2년 전844회 조회
1개 답변
0
수락된 답변

You could create a base image that contains all your shared dependencies (i.e. codec tools) and then reference this base image in the Dockerfile on your lambda functions. The base image should be built as a pre-requisite step and pushed to an ECR repository.

profile pictureAWS
답변함 2년 전
profile picture
전문가
검토됨 25일 전
  • Would you mind pointing out how exactly to create a base image and then reference it in the lambda? I was making the base image by the DockerImageAsset from aws-cdk-lib/aws-ecr-assets, and then using asset.imageUri to reference it when creating a lambda, like the following:

    new DockerImageFunction(this, "lambda-handler", {code: DockerImageCode.fromImageAsset(path.join("/dockerfile/path/"), {buildArgs: { CODEC_SOURCE: asset.imageUri} })})
    

    But I got an Error said:Cannot use tokens in keys or values of "buildArgs" since they are needed before deployment.

    Thanks!

  • The error you get from CDK is because the imageUri is only known at execution time, but the buildArgs are needed at synth time. You may need to separate the build of the base image in a separate process to have the base image pushed to a known ECR registry that can then be referenced from the lambda stack. The following construct could be used to publish the base image to ECR: https://github.com/cdklabs/cdk-ecr-deployment

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠