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-nodejs): NodejsFunction support EdgeFunction for Lambda@Edge

See original GitHub issue

If I wish to use NodejsFunction to create a Lambda@Edge, I need to make sure that my whole stack is currently in us-east.

If there was a prop or flag on NodejsFunction props, we could easily deploy our NodeJS functions to Lambda@Edge.

Something similar likely needs to happen with other lambda helpers.

It seems someone has already made a EdgeFunction construct: https://github.com/aws/aws-cdk/pull/10500

So it could be a case of just refactoring so that there was a class that inherited from that instead of lambda.Function.

This would probably need to be done with the go/python helpers, too.

Use Case

I would like to use NodejsFunction to create lambdas that can be used with Lambda@Edge with my stack in any region.

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

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
Shiroycommented, Jul 21, 2021

I think a better implementation would be to publicly expose the bundler in aws-lambda-nodejs as a top-level component instead of keeping it package private.

https://github.com/aws/aws-cdk/blob/464cdc0baa1fc5b0634e428a9c7a15de4ca93d50/packages/%40aws-cdk/aws-lambda-nodejs/lib/bundling.ts#L40

This would allow any kind of lambda function to rely on the esbuild bundler. This would benefit

https://github.com/aws/aws-cdk/blob/464cdc0baa1fc5b0634e428a9c7a15de4ca93d50/packages/%40aws-cdk/aws-cloudfront/lib/experimental/edge-function.ts#L37

and

https://github.com/aws/aws-cdk/blob/464cdc0baa1fc5b0634e428a9c7a15de4ca93d50/packages/%40aws-cdk/aws-cloudfront/lib/function.ts#L133

As an example, the following snippet works for deploying Lambda@Edges but it relies on the Bundling component which is internal to aws-lambda-nodejs

import { Runtime } from "@aws-cdk/aws-lambda";
import * as cdk from "@aws-cdk/core";
import { Bundling } from "@aws-cdk/aws-lambda-nodejs/lib/bundling"; 
// Not OK as we rely on the package private bundler. 
// This does not provide any stability guarantee in the long run.
import * as path from "path";
import { EdgeFunction } from "@aws-cdk/aws-cloudfront/lib/experimental";

export interface PrefixRemoverProps {
    prefixToRemove: string
}

export class PrefixRemover extends cdk.Construct {
    readonly lambda: EdgeFunction
    constructor(scope: cdk.Construct, name: string, props: PrefixRemoverProps) {
        super(scope, name)

        this.lambda = new EdgeFunction(this, `${name}-prefixRemover` , {
            code: Bundling.bundle({
                runtime: Runtime.NODEJS_14_X,
                depsLockFilePath: path.join(__dirname, "src" , "package-lock.json"),
                entry: path.join(__dirname, "src", "handler.ts"),
                projectRoot: path.join(__dirname, "src"),
                define: {
                    'process.env.PREFIX_TO_REMOVE': `"${props.prefixToRemove}"`
                },
                forceDockerBundling: true
            }),
            runtime: Runtime.NODEJS_14_X,
            handler: "index.handler"
        });
    } 
}
3reactions
hugomalletcommented, Jan 24, 2022

Cannot import Bundling with CDK v2 :

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './aws-lambda-nodejs/lib/bundling.js' is not defined by "exports"
Read more comments on GitHub >

github_iconTop Results From Across the Web

class EdgeFunction (construct) · AWS CDK
Convenience resource for requesting a Lambda function in the 'us-east-1' region for use with Lambda@Edge. Implements several restrictions enforced by Lambda@ ...
Read more >
Deploy Lambda@Edge with AWS CDK and TypeScript
I took a closer look at NodejsFunction and EdgeFunction and transferred the TypeScript functionality from NodejsFunction to EdgeFunction.
Read more >
Tutorial: Creating a simple Lambda@Edge function
This tutorial shows you how to get started with Lambda@Edge by helping you create and add a sample Node.js function that runs in...
Read more >
Making Lambda@Edge and executing `cdk deploy` occurred ...
I want to deploy lambda@edge source code by aws-cdk . new NodejsFunction(this, 'some-awesome-function', { role: new Role(this ...
Read more >
@aws-cdk/aws-lambda-nodejs | Yarn - Package Manager
Node.js Function. The NodejsFunction construct creates a Lambda function with automatic transpiling and bundling of TypeScript or Javascript code. This results ...
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