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

lambdaFunctionUrl is undefined #243

Open
mobob opened this issue Feb 18, 2025 · 0 comments
Open

lambdaFunctionUrl is undefined #243

mobob opened this issue Feb 18, 2025 · 0 comments

Comments

@mobob
Copy link

mobob commented Feb 18, 2025

Hey there!

I'm trying to attach an additional behaviour to the cloudfront that is created by NextJS that will route to the existing server function (to route analytics ingest for posthog directly to nextjs).

My initial try was to access lambdaFunctionUrl on the NextJS structure which seems handy, yet it is always null. Searching the code naively too looks like its dangling there and isn't populated (but i'm not really sure how the magic happens).

    const nextJsApp = new Nextjs(this, ...);

    if (!nextJsApp.lambdaFunctionUrl) {
      throw new Error('lambdaFunctionUrl is not available');
    }

    // going down this route didn't help either as it fails with a duplicate construct name...
    // const functionUrl = nextJsApp.serverFunction.lambdaFunction.addFunctionUrl();

    const origin = new origins.FunctionUrlOrigin(nextJsApp.lambdaFunctionUrl, { originId: 'posthog-ingest' });

    const posthogCachePolicy = new cloudfront.CachePolicy(this, 'PosthogCachePolicy', {
      cachePolicyName: `posthog-cors-${props.stageEnv}`,
      headerBehavior: cloudfront.CacheHeaderBehavior.allowList('Origin', 'Authorization'),
      queryStringBehavior: cloudfront.CacheQueryStringBehavior.all(),
      enableAcceptEncodingGzip: true,
      enableAcceptEncodingBrotli: true,
    });

    // Copy any other behavior settings you need
    nextJsApp.distribution.distribution.addBehavior('/ingest', origin, {
      viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.HTTPS_ONLY,
      allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
      cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
      cachePolicy: posthogCachePolicy,
      originRequestPolicy: cloudfront.OriginRequestPolicy.CORS_CUSTOM_ORIGIN,
      responseHeadersPolicy: cloudfront.ResponseHeadersPolicy.CORS_ALLOW_ALL_ORIGINS_WITH_PREFLIGHT_AND_SECURITY_HEADERS,
    });

But this fails right off the bat.

Trying to create my own FunctionUrl, to then attach a new origin too, also fails, i'm guessing because i cannot have multiple function urls.

Ultimately if i could access the origin created for the server function that would be ideal, but i'm not sure how to do that either!

Let me know if you have suggestions! Happy to decompose the Nextjs use into its requisite parts to make this easier if thats the suggestion. TIA!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant