(aws_lambda ): DockerImageFunction.fromImageAsset fails to include image assets
See original GitHub issueDescribe 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:
- Created a year ago
- Comments:6 (2 by maintainers)
@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
And have your lambda function code assets path point to the upper lambda directory like:
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.
⚠️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.