{"id":22351470,"url":"https://github.com/garysassano/cdk-aws-hybrid-esm-lambda","last_synced_at":"2026-05-02T13:34:46.847Z","repository":{"id":234732174,"uuid":"763254823","full_name":"garysassano/cdk-aws-hybrid-esm-lambda","owner":"garysassano","description":"CDK app that deploys a Lambda function from a hybrid ESM bundle","archived":false,"fork":false,"pushed_at":"2025-01-03T14:02:17.000Z","size":546,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T12:09:07.654Z","etag":null,"topics":["aws","aws-cdk","aws-cdk-ts","aws-cdk-typescript","cdk","cdk-ts","esm","esm-lambda","esm-nodejs-function","hybrid","hybrid-esm-lambda","lambda-function-ts","node-lambda","nodejsfunction","optimization","ts-lambda","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/garysassano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-25T23:41:53.000Z","updated_at":"2025-01-03T14:02:20.000Z","dependencies_parsed_at":"2024-07-21T10:51:17.175Z","dependency_job_id":null,"html_url":"https://github.com/garysassano/cdk-aws-hybrid-esm-lambda","commit_stats":null,"previous_names":["garysassano/cdk-aws-hybrid-esm-lambda"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/garysassano/cdk-aws-hybrid-esm-lambda","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garysassano%2Fcdk-aws-hybrid-esm-lambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garysassano%2Fcdk-aws-hybrid-esm-lambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garysassano%2Fcdk-aws-hybrid-esm-lambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garysassano%2Fcdk-aws-hybrid-esm-lambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/garysassano","download_url":"https://codeload.github.com/garysassano/cdk-aws-hybrid-esm-lambda/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/garysassano%2Fcdk-aws-hybrid-esm-lambda/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32536579,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T12:25:33.646Z","status":"ssl_error","status_checked_at":"2026-05-02T12:24:51.733Z","response_time":132,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-cdk","aws-cdk-ts","aws-cdk-typescript","cdk","cdk-ts","esm","esm-lambda","esm-nodejs-function","hybrid","hybrid-esm-lambda","lambda-function-ts","node-lambda","nodejsfunction","optimization","ts-lambda","typescript"],"created_at":"2024-12-04T12:14:09.379Z","updated_at":"2026-05-02T13:34:46.826Z","avatar_url":"https://github.com/garysassano.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cdk-aws-hybrid-esm-lambda\n\nCDK app that deploys a Lambda function from a hybrid ESM bundle.\n\n## Optimizing Node.js Lambda: A Hybrid ESM Approach\n\nThis repository demonstrates a hybrid approach to optimize Node.js Lambda functions by leveraging the advantages of ES modules (ESM) for size and performance improvements, while maintaining compatibility with existing CommonJS (CJS) codebases.\n\n**The Dilemma:** Transitioning a CDK project entirely to ESM can be a daunting task, often hindered by dependencies on older CJS libraries and the complexities of updating project configurations. This typically involves modifications to `tsconfig.json`, `package.json`, and `cdk.json` files, which can be time-consuming and error-prone.\n\n**The Hybrid Approach:** By leveraging the AWS CDK's [NodejsFunction](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunction.html) construct and its built-in `esbuild` bundling capabilities, we can create Lambda functions that prioritize ESM modules while gracefully falling back to CJS when needed. This enables us to achieve the following benefits::\n\n- **Significant Size Reduction:**  Witness dramatic reductions in package size compared to traditional CJS bundling, thanks to ESM features like tree-shaking, which eliminates unused code. For instance, in the provided example, a CJS bundle reduces [from 2.9 MB to 1.3 MB](./src/assets/cjs-bundle.png), while the hybrid ESM approach achieves an even more impressive reduction [from 2.5 MB to a mere 585 KB](./src/assets/esm-bundle.png).\n- **Faster Cold Starts:** Smaller package size directly translates to faster Lambda initialization and significantly reduced cold start latency, improving the responsiveness and efficiency of your serverless applications.\n- **Enhanced Runtime Efficiency:** ESM, with its support for top-level await and asynchronous module loading, can improve how your code executes and utilizes resources during runtime compared to CJS.\n\n**Key Benefits:**\n\n- **Minimal Effort:**  No need to refactor your existing CJS codebase, saving you time and effort.\n- **Reduced Complexity:** Avoid the intricacies of configuring your project for full ESM adoption.\n- **Optimized Lambda Functions:** Achieve smaller package sizes and faster cold starts without sacrificing compatibility.\n\n**Implementation Details:**\n\nThe provided example showcases how to configure the CDK's `NodejsFunction` construct with specific bundling options to achieve this hybrid approach. Key properties include:\n\n- `format`: Sets the output format to ESM for optimal bundling.\n- `mainFields`: Specifies the order in which module formats are resolved, prioritizing ESM.\n- `banner`: Includes a code snippet to ensure seamless compatibility with CJS modules.\n\n**This hybrid solution offers the best of both worlds: it unlocks ESM's size and performance benefits for your Lambda functions, while ensuring seamless integration with your existing CJS codebase, all without requiring a full migration.**\n\n## Prerequisites\n\n- **_AWS:_**\n  - Must have authenticated with [Default Credentials](https://docs.aws.amazon.com/cdk/v2/guide/cli.html#cli_auth) in your local environment.\n  - Must have completed the [CDK bootstrapping](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html) for the target AWS environment.\n- **_Node.js + npm:_**\n  - Must be [installed](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) in your system.\n\n## Installation\n\n```sh\nnpx projen install\n```\n\n## Deployment\n\n```sh\nnpx projen deploy\n```\n\n## Cleanup\n\n```sh\nnpx projen destroy\n```\n\n## Architecture Diagram\n\n![Architecture Diagram](./src/assets/arch.svg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgarysassano%2Fcdk-aws-hybrid-esm-lambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgarysassano%2Fcdk-aws-hybrid-esm-lambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgarysassano%2Fcdk-aws-hybrid-esm-lambda/lists"}