UNPKG

cdk-nextjs-standalone

Version:

Deploy a NextJS app to AWS using CDK and OpenNext.

94 lines (63 loc) 4.4 kB
# Deploy NextJS with CDK [![View on Construct Hub](https://constructs.dev/badge?package=cdk-nextjs-standalone)](https://constructs.dev/packages/cdk-nextjs-standalone) ## What is this? A CDK construct to deploy a NextJS app using AWS CDK. Supported NextJs versions: >=12.3.0+ (includes 13.0.0+) Uses the [standalone output](https://nextjs.org/docs/advanced-features/output-file-tracing) build mode. ## Quickstart ```ts import { App, Stack, StackProps } from 'aws-cdk-lib'; import { Construct } from 'constructs'; import { Nextjs } from 'cdk-nextjs-standalone'; class WebStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const nextjs = new Nextjs(this, 'Nextjs', { nextjsPath: './web', // relative path from your project root to NextJS }); new CfnOutput(this, "CloudFrontDistributionDomain", { value: nextjs.distribution.distributionDomain, }); } } const app = new App(); new WebStack(app, 'web'); ``` ## Important Notes - Due to CloudFront's Distribution Cache Behavior pattern matching limitations, a cache behavior will be created for each top level file or directory in your `public/` folder. CloudFront has a soft limit of [25 cache behaviors per distribution](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html#limits-web-distributions). Therefore, it's recommended to include all assets that can be under a top level folder like `public/static/`. Learn more in open-next docs [here](https://github.com/sst/open-next/blob/main/README.md#workaround-create-one-cache-behavior-per-top-level-file-and-folder-in-public-aws-specific). ## Documentation Available on [Construct Hub](https://constructs.dev/packages/cdk-nextjs-standalone/). ## Examples See example CDK apps [here](./examples) including: - App Router - Pages Router - App/Pages Router - High Security - Multiple Sites To deploy an example, make sure to read the [README.md](./examples/README.md) ### Discord Chat We're in the #open-next channel on the [Serverless Stack Discord](https://discord.gg/sst). ## About Deploys a NextJs static site with server-side rendering and API support. Uses AWS lambda and CloudFront. There is a new (since Next 12) [standalone output mode which uses output tracing](https://nextjs.org/docs/advanced-features/output-file-tracing) to generate a minimal server and static files. This standalone server can be converted into a CloudFront distribution and a lambda handler that handles SSR, API, and routing. The CloudFront default origin first checks S3 for static files and falls back to an HTTP origin using a lambda function URL. ## Benefits This approach is most compatible with new NextJs features such as ESM configuration, [middleware](https://nextjs.org/docs/advanced-features/middleware), next-auth, and React server components ("appDir"). The unmaintained [@serverless-nextjs project](https://github.com/serverless-nextjs/serverless-next.js) uses the deprecated `serverless` NextJs build target which [prevents the use of new features](https://github.com/serverless-nextjs/serverless-next.js/pull/2478). This construct was created to use the new `standalone` output build and newer AWS features like lambda function URLs and fallback origins. You may want to look at [Serverless Stack](https://sst.dev) and its [NextjsSite](https://docs.sst.dev/constructs/NextjsSite) construct for an improved developer experience if you are building serverless applications on CDK. ## Dependencies Built on top of [open-next](https://open-next.js.org/), which was partially built using the original core of cdk-nextjs-standalone. ## Heavily based on - [Open-next](https://open-next.js.org/) - <https://github.com/iiroj/iiro.fi/commit/bd43222032d0dbb765e1111825f64dbb5db851d9> - <https://github.com/sladg/nextjs-lambda> - <https://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/compat-layers/apigw-lambda-compat> - [Serverless Stack](https://github.com/serverless-stack/sst) - [RemixSite](https://github.com/serverless-stack/sst/blob/master/packages/resources/src/NextjsSite.ts) construct - [NextjsSite](https://github.com/serverless-stack/sst/blob/master/packages/resources/src/RemixSite.ts) construct ## Contribute See [Contribute](./docs/contribute.md). ## Breaking changes See [Major Changes](./docs/major-changes.md).