question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws_lambda ): DockerImageFunction.fromImageAsset fails to include image assets

See original GitHub issue

Describe the bug

In a construct library repo, if you create a lambda function using DockerImageFunction.fromImageAsset and then run npm run build, neither the Dockerfile nor the lambda function code is included in the packaged lib directory.

If you then import and use this construct in a different repo, you get a file not found error as the Dockerfile and lambda function code is not present in the build directory (lib) of the construct library.

The error is:

Error: Cannot find image directory at /Path/to/my/repo/lib/function

Expected Behavior

When running npm run build (or in my case npx projen build) the Dockerfile and Python files should be included in the “compiled” lib directory.

Current Behavior

The image assets are not included in the “compiled” lib directory and so when trying to use the construct in a stack results in a file-not-found error.

Reproduction Steps

In my construct library repository I define a lambda function construct and reference a local directory containing the assets for the lambda function.

.
├── MyLambda.ts
└── function
    ├── Dockerfile
    ├── README.md
    ├── WorkerFunction.py
    └── requirements.txt

where in MyLambda.ts the lambda function is defined like this:

    const workerFunction = new lambda.DockerImageFunction(this, 'worker-function', {
      functionName: 'lambda_handler',
      code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, 'function'),
        {
          cmd: ['WorkerFunction.lambda_handler'],
          entrypoint: ['/lambda-entrypoint.sh'],
        }),
      memorySize: 128,
      role: assumeFunctionRole,
      timeout: Duration.seconds(60),
    });

However if you then run npm run build on this construct library, the following gets built into the lib directory and we can see that the Dockerfile, python code, requirements.txt, and README.md are all missing.

    ├── MyLambda.d.ts
    └── MyLambda.js

Then you can attempt to use this construct in a different CDK project by linking them using npm:

cd ../my-other-repo
npm link ../my-construct-repo

Then if you attempt to reference the MyLambda construct and run cdk synth myLambdaStack you get a file-not-found error:

Error: Cannot find image directory at /Path/to/my/repo/lib/function

Possible Solution

A possible solution that I haven’t tried yet is separately defining the image separately using ecr assets and then referencing ECR repo image/tag in the Lambda function construct using the DockerImageFunction.fromEcr method.

Additional Information/Context

No response

CDK CLI Version

2.50.0 (build 4c11af6)

Framework Version

No response

Node.js Version

v16.14.0

OS

MacOS Catalina 10.15.7

Language

Typescript

Language Version

TypeScript (4.8.4)

Other information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pahudcommented, Nov 22, 2022

@mvs5465 I am not very sure but AFAIK projen would compile and copy typescript files under src to lib and leave everything outsides of src untouched. I would prefer having the lambda directories out of src like this

.
├── LICENSE
├── README.md
├── cdk.context.json
├── lambda
│   └── index.py
├── package.json
├── src
│   └── main.ts
├── test
│   └── main.test.ts
├── tsconfig.dev.json
└── yarn.lock

And have your lambda function code assets path point to the upper lambda directory like:

code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '../lambda'),

I hope this would be helpful but definitely worth a ticket in https://github.com/projen/projen/issues to bring their attention for recommended practice. As this issue is not related to AWS CDK I am closing it for now but feel free to reopen it if you think it is still relevant to AWS CDK.

0reactions
github-actions[bot]commented, Nov 22, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda source code updating issue during its container ...
I am making a lambda function through `DockerImageFunction` with `DockerImageCode.fromImageAsset()` from `aws-cdk-lib/aws-lambda`.
Read more >
class DockerImageAsset (construct) · AWS CDK
An asset that represents a Docker image. The image will be created in build time and uploaded to an ECR repository. Example. import...
Read more >
AWS CDK: How to pass build_args to DockerImageCode ...
So in one of the stacks I use: DockerImageFunction(self, "SomeId", code=DockerImageCode.from_image_asset( directory='some-dir', build_args={ ...
Read more >
AWS CDK: Deploy Lambda with Docker - sbstjn.com
With supporting docker images, AWS Lambda has immutable deployment ... and use the DockerImageFunction class with AWS Cloud Development Kit.
Read more >
@aws-cdk/aws-lambda - npm
fromDockerBuild(path, options) - use the result of a Docker build as code. The runtime code is expected to be located at /asset in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found