Deploy a NextJS app using OpenNEXT packaging to serverless AWS using CDK
Project description
OpenNEXT CDK
TS | Java | Go | Python | .NET
Deploy NextJS using OpenNEXT packaging to serverless AWS using CDK in any language
Contents
What is this?
A building block for Amazon's infrastructure-as-code CDK toolkit to deploy a NextJS app using AWS serverless services. Supported NextJs versions: >=12.3.0+ (includes 13.0.0+)
Your NextJS app is packaged using OpenNEXT to fit the serverless format on Lambda
Quickstart
Add the dependency esbuild@0.17.16
to your project along with open-next-cdk
.
npm install --save-dev esbuild@0.17.16 open-next-cdk
Add the following CDK construct to your CDK application
import { Nextjs } from 'open-next-cdk';
new Nextjs(this, 'Web', {
nextjsPath: './web', // relative path to nextjs project root
});
This will automatically build your NextJS app and package it for you as part of the CDK construct.
If you would prefer to package it separately, see below:
Build manually
Build NextJs using open-next:
open-next build
You will find a new folder .open-next
which contains the packaging for your NextJS App. Now you can use the construct by instructing it not to build your app, just use the OpenNEXT folder directly:
import { Nextjs } from 'open-next-cdk';
new Nextjs(this, 'Web', {
nextjsPath: './web', // relative path to nextjs project containing .open-next folder
isPlaceholder: true, // Do not build, assume .open-next folder already exists
});
Additional security
import { RemovalPolicy, Stack } from "aws-cdk-lib";
import { Construct } from "constructs";
import { CfnWebAcl } from "aws-cdk-lib/aws-wafv2";
import { SecurityPolicyProtocol, type DistributionProps } from "aws-cdk-lib/aws-cloudfront";
import { Nextjs, type NextjsDistributionProps } from "cdk-nextjs-standalone";
import { Bucket, BlockPublicAccess, BucketEncryption } from "aws-cdk-lib/aws-s3";
// Because of `WebAcl`, this stack must be deployed in us-east-1. If you want
// to deploy Nextjs in another region, add WAF in separate stack deployed in us-east-1
export class UiStack {
constructor(scope: Construct, id: string) {
const webAcl = new CfnWebAcl(this, "WebAcl", { ... });
new Nextjs(this, "NextSite", {
nextjsPath: "...",
defaults: {
assetDeployment: {
bucket: new Bucket(this, "NextjsAssetDeploymentBucket", {
autoDeleteObjects: true,
removalPolicy: RemovalPolicy.DESTROY,
encryption: BucketEncryption.S3_MANAGED,
enforceSSL: true,
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
}),
},
distribution: {
functionUrlAuthType: FunctionUrlAuthType.AWS_IAM,
cdk: {
distribution: {
webAclId: webAcl.attrArn,
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2_2021,
} as DistributionProps,
},
} satisfies Partial<NextjsDistributionProps>,
},
});
}
}
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 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, next-auth, and React server components ("appDir").
The unmaintained @serverless-nextjs project uses the deprecated serverless
NextJs build target which prevents the use of new features.
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 and its NextjsSite construct for an improved developer experience if you are building serverless applications on CDK.
Dependencies
Built on top of open-next, which was partially built using the original core of cdk-nextjs-standalone.
Similar projects
Heavily based on
-
https://github.com/iiroj/iiro.fi/commit/bd43222032d0dbb765e1111825f64dbb5db851d9
-
- RemixSite construct
- NextjsSite construct
Fork from cdk-nextjs
This project has been initially forked from cdk-nextjs in order to publish the package to other langugages including Java, Go, .NET, Python using JSII.
Contributing
Hey there, we value every new contribution a lot 🙏🏼 thank you.
Projen
Don't manually update package.json or use npm CLI. Update dependencies in .projenrc.js then run yarn projen
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for open_next_cdk-0.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a4779c84738206bf7fdbd65dda800aa7452239e1aa83bb7c9123f1e633ab19c |
|
MD5 | 678cd52eb5f1cf4b787a119789fbb2b3 |
|
BLAKE2b-256 | 57dfcf9b33242d2f3646cc2f86f65e3c618096ed8e0eefa2575bc4ddee71bf75 |