Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lambda: allow bundling of lambda layer #26332

Open
2 tasks
JonWallsten opened this issue Jul 12, 2023 · 9 comments
Open
2 tasks

lambda: allow bundling of lambda layer #26332

JonWallsten opened this issue Jul 12, 2023 · 9 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@JonWallsten
Copy link

JonWallsten commented Jul 12, 2023

Describe the feature

We are missing the bundling functionality when creating LayerVersions in the CDK v2.

Use Case

We have a lambda layer that uses imports from sibling/root folders (let say a shared util folder in root), but since they will not be included in the same output folder as the layer, these imports will be missing in runtime.
The same goes for node_modules. The modules used would have to be part of the assets target folder.
Bundling the layer takes care of this issue by making sure the imports are included in the bundle.
It also just includes the modules use since esbuild offers some treeshaking.

Proposed Solution

Offer the same bundling as the NodeJsFunction (also requested for Lambda @ Edge here: #14215).

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.87.0

Environment details (OS name and version, etc.)

Windows 10 x64. Node 18.12.x.

@JonWallsten JonWallsten added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jul 12, 2023
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Jul 12, 2023
@pahud pahud changed the title (aws-cdk-lib/aws-lambda): allow bundling of lambda layer lambda: allow bundling of lambda layer Jul 12, 2023
@pahud
Copy link
Contributor

pahud commented Jul 12, 2023

Thank you for your feature request. Can you share more details about what your directory structure looks like?

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jul 12, 2023
@JonWallsten
Copy link
Author

JonWallsten commented Jul 12, 2023

@pahud: It could look something like this:

.
├── .dist/
│   ├── assets/
│   │   ├── lambda-edge/
│   │   │   └── auth-guard.js
│   │   └── lambda-layer/
│   │       ├── auth.js
│   │       ├── common.js
│   │       └── index.js
│   ├── lib/
│   │   ├── api-stack.js
│   │   └── lambda-stack.js
│   └── utils/
│       ├── auth.js
│       └── common.js
├── assets/
│   ├── lambda-edge/
│   │   └── auth-guard.ts
│   └── lambda-layer/
│       ├── auth.ts
│       ├── common.ts
│       └── index.ts
├── lib/
│   ├── api-stack.ts
│   └── lambda-stack.ts
├── utils/
│   ├── auth.ts
│   └── common.ts
└── README.md

Where in this case the .dist/assets/lambda-layer/index.js would have local imports to ``.dist/utils/common.jslooking like this:import * from '../../utils/common';`
But the `Code.fromAssets('./dist/assets/lambda-layer')` would not include that file.

But the same goes for any node_modules. They would not be part of the assets folder either. So I guess I should update the ticket.

@pahud
Copy link
Contributor

pahud commented Jul 14, 2023

OK now I get it.

Unfortunately lambda layer only support Code with no bundling capabilities. Please help us prioritize with upvotes 👍.

@pahud
Copy link
Contributor

pahud commented Jul 14, 2023

btw

Why not just Code.fromAssets('./dist') ?

@pahud
Copy link
Contributor

pahud commented Jul 14, 2023

probably related to #26107 (comment)

@JonWallsten
Copy link
Author

@pahud: Using the full ./dist folder with code from all different stacks/utils/etc for a single LambdaLayer would probably not be a good idea.

@jk2l
Copy link

jk2l commented Jul 29, 2024

not sure is this issue outdated, i have bundling work for layer

        self.oracle_19_1_layer = lambda_.LayerVersion(
            self,
            "OracleDependency",
            layer_version_name="oracle-dependency-19_1",
            code=lambda_.Code.from_asset(
                "./assets/lambda/oracle-19.1",
                bundling=cdk.BundlingOptions(
                    image=lambda_.Runtime.PYTHON_3_9.bundling_image,
                    command=[
                        "bash",
                        "-c",
                        " && ".join(
                            [
                                f"pip install -r requirements.txt -t /{cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/python/lib/python3.9/site-packages",
                                f"cp -r ./lib /{cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/lib",
                            ]
                        ),
                    ],
                ),
            ),
            compatible_runtimes=[lambda_.Runtime.PYTHON_3_9],
            license="Available under the MIT-0 license.",
            description="A layer of oracle dependency",
        )

@pahud
Copy link
Contributor

pahud commented Jul 30, 2024

@jk2l Awesome!

@JonWallsten does it work for you?

@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 30, 2024
@JonWallsten
Copy link
Author

I'm using Typescript so this particular code would not work. I'm on vacation, but I think I'm using esbuild to generate the bundle and then use Code.fromAsset.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

3 participants