AWS CDK create Lambda from image

Viewed 875

I am a new to AWS world. I am working on a project to build a server less application and as part of that I have created 4 lambda which works fine.

Next I am trying to create a deployment pipe line using CDK; below is what I am trying to do.

  1. create an docker image with code that includes all lambda code

  2. create 4 different lambdas from same image just override the CMD in docker image and mention the lambda handler

I have setup CDK locally and able to create stack, everything works fine.

Below is my code snippet

--create the docker image

asset_img = aws_ecr_assets.DockerImageAsset(
    self,
    "test_image",
    directory=os.path.join(os.getcwd(), "../mysrc")
)

--create lambda from docker image

aws_lambda.DockerImageFunction(
   self,
   function_name="gt_from_image",
   code=_lambda.DockerImageCode.from_ecr(
       self,
       repository=asset_img.repository,
       tag="latest")
)

Below is the error I am getting

TypeError: from_ecr() got multiple values for argument 'repository'

I am not sure how I can reference the image that was created and define the lambda.

Solved: Below is the solution

asset_img = _asset.DockerImageAsset(self, "test_image", directory=os.path.join(os.getcwd(), "../gt"))

_lambda.DockerImageFunction(self, id='gt_from_image', function_name="gt_from_image_Fn",
                                    code=_lambda.DockerImageCode.from_ecr(
                                        repository=asset_img.repository,
                                        tag=asset_img.source_hash))
1 Answers

From the documentation for DockerImageCode.from_ecr(), it does not expect a scope argument, so your self argument is what is causing the error.

Another issue is that DockerImageAsset will not tag the image as latest, as that is against AWS best practices.

The easy way to achieve what you are doing is to use DockerImageCode.from_image_asset().

Related