Home > other >  I am trying to build an AWS Lambda function from a dockerfile deployed over CDK
I am trying to build an AWS Lambda function from a dockerfile deployed over CDK

Time:11-04

lambdaFunction = _lambda.DockerImageFunction(self, f'{client_id}-prefect-lambda-handler',   
                                        code=_lambda.DockerImageCode.from_image_asset(
                                            directory="cumulus_devops_cdk/prefect-lambda-handler"
                                        ),
                                        )

I am trying to create a lambda function from a docker image in CDK as shown above. The problem is that my company's CDK runs in a docker image and thus has trouble building a docker image inside of itself.

I know that the docker image works because it succeeded when I manually built and pushed the image to ECR and had CDK pull from that, however I would like to have it get built every time I CDK deploy.

Whenever I try to cdk deploy the stack I get this error

[100%] fail: docker build --tag cdkasset-d4a61d4806d68e3a7b9589a1e161b40523d2a3bc5be6506aaf6bb4b45fd5cc07 . exited with error code 1: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

How can I successfully build the docker image in cdk and have it deployed to the lambda function?

CodePudding user response:

You can build and publish the image to Amazon ECR outside of the AWS CDK, and then reference that image using _lambda.DockerImageCode.fromEcr(repository, props?) [1]

[1] https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-lambda.DockerImageCode.html#static-fromwbrecrrepository-props

CodePudding user response:

This is as Docker in Docker issue[1]. If you have access to the configuration of the outer Docker Container (ie. the one doing the bundling) you should mount the Docker socket to this image like using -v/--volume:

docker run -v /var/run/docker.sock:/var/run/docker.sock <image>

This allows <image> to use the host Docker Daemon.

[1] https://devopscube.com/run-docker-in-docker/

  • Related