1

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))
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Rahul
  • 47
  • 7

1 Answers1

0

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().

gshpychka
  • 8,523
  • 1
  • 11
  • 31
  • Thanks.removed the self and CDK is uploading the Stack but fails to create Lambda with below error message. Code --> asset_img = _asset.DockerImageAsset(self, "galactus_transformer_image", directory=os.path.join(os.getcwd(), "../galactus_transformer")) _lambda.DockerImageFunction(self, id='gt_from_image', function_name="gt_from_image_Fn", code=_lambda.DockerImageCode.from_ecr(repository=asset_img.repository)) Resource handler returned message: "Source image xxxxxx.dkr.amazonaws.com/aws-cdk/assets:latest does not exist. Provide a valid source image. – Rahul Aug 17 '21 at 20:54
  • As I've stated in the answer, this will not work because `DockerImageAsset` will not use the `latest` tag. – gshpychka Aug 17 '21 at 20:59
  • I removed the latest sset_img = _asset.DockerImageAsset(self, "galactus_transformer_image", directory=os.path.join(os.getcwd(), "../galactus_transformer")) _lambda.DockerImageFunction(self, id='gt_from_image', function_name="gt_from_image_Fn", code=_lambda.DockerImageCode.from_ecr(repository=asset_img.repository)) – Rahul Aug 17 '21 at 21:00
  • If you don't specify a `tag` argument in `from_ecr()`, you can see from documentation that it will just use `latest` as the default value. – gshpychka Aug 17 '21 at 21:00
  • I am ok with the default tag but fails create Lambda because image doesn't exist under /aws-cdk/assets. Do I have to use /assets in the docker file as a home folder – Rahul Aug 17 '21 at 21:04
  • It doesn't exist because `DockerImageAsset` does not tag any images with the `latest` tag, as stated above. There is no `latest` tag in the repository. Consider using `DockerImageCode.from_image_asset()`. – gshpychka Aug 17 '21 at 21:07
  • DockerImageCode.from_image_asset() -- using this, will it create a new image, that is what I understood from reading the was document. is there anyway I can use the image created by DockerImageAsset() to create a lambda. – Rahul Aug 17 '21 at 21:16
  • You can create one `DockerImageCode` object and reuse it across your lambdas. – gshpychka Aug 17 '21 at 21:18
  • is there any way to create a lambda using image created by DockerImageAsset – Rahul Aug 17 '21 at 21:25
  • 1
    I am able to finally create the lambda from the DockerImageAsset image. Below is the code. 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)) – Rahul Aug 17 '21 at 22:14
  • Consider adding that as an answer, explaining why it works, and accepting it. – gshpychka Aug 18 '21 at 06:04