Package: open-next-cdk
OpenNext CDKDeploy NextJS on AWS using CDK IaC and OpenNext packagingContentsWhat is this?QuickstartRequirementsAdvancedPre-built OpenNext packageAdditional securityAboutBenefitsDependenciesSimilar projectsFork from cdk-nextjsContributingUsing ProjenWhat is this?A building block for Amazon's infrastructure-as-code CDK toolkit to deploy a NextJS app using AWS serverless services.Your NextJS app is packaged using OpenNext to fit the serverless format on LambdaRequirementsNextJs versions: >=12.3.0+ (includes 13.0.0+)Platforms: darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-arm64, win32-x64QuickstartNextJS setupAdd a dev dependencyesbuild@0.17.16to your NextJS project.npminstall--save-devesbuild@0.17.16CDK ConstructUse this construct in your CDK application to deploy your NextJS app to AWS.TypescriptInstall the dependency using npm:npminstall--save-devesbuild@0.17.16open-next-cdkUse the construct in your CDK application:import{Nextjs}from'open-next-cdk';newNextjs(this,'Web',{nextjsPath:'./web',//relativepathtonextjsprojectroot});JavaInstall the dependency using Maven:<dependency><groupId>io.dataspray</groupId><artifactId>open-next-cdk</artifactId><version>x.y.z</version></dependency>Use the construct in your CDK application:Nextjs.Builder.create(this,getConstructId()).nextjsPath("./web").build();GoInstall the dependency:gogetgithub.com:datasprayio/open-next-cdk.git@goOr checkoutthe code in thegobranch.PythonInstall the dependency:pipinstallopen-next-cdk.NETInstall the dependency:dotnetaddpackageDataspray.OpenNextCdkThis 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:AdvancedPre-built OpenNext packageHow-toYou may also provide already pre-built OpenNext package directly by building it yourself first:open-nextbuildYou will find a new folder.open-nextwhich 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';newNextjs(this,'Web',{openNextPath:'./web/.open-next',//relativepathto.open-nextfolder});Additional securityHow-toimport{RemovalPolicy,Stack}from"aws-cdk-lib";import{Construct}from"constructs";import{CfnWebAcl}from"aws-cdk-lib/aws-wafv2";import{SecurityPolicyProtocol,typeDistributionProps}from"aws-cdk-lib/aws-cloudfront";import{Nextjs,typeNextjsDistributionProps}from"cdk-nextjs-standalone";import{Bucket,BlockPublicAccess,BucketEncryption}from"aws-cdk-lib/aws-s3";//Becauseof`WebAcl`,thisstackmustbedeployedinus-east-1.Ifyouwant//todeployNextjsinanotherregion,addWAFinseparatestackdeployedinus-east-1exportclassUiStack{constructor(scope:Construct,id:string){constwebAcl=newCfnWebAcl(this,"WebAcl",{...});newNextjs(this,"NextSite",{nextjsPath:"...",defaults:{assetDeployment:{bucket:newBucket(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,}asDistributionProps,},}satisfiesPartial<NextjsDistributionProps>,},});}}AboutDeploys 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 tracingto 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.BenefitsThis approach is most compatible with new NextJs features such as ESM configuration,middleware, next-auth, and React server components ("appDir").The unmaintained@serverless-nextjs projectuses the deprecatedserverlessNextJs build target whichprevents the use of new features.
This construct was created to use the newstandaloneoutput build and newer AWS features like lambda function URLs and fallback origins.You may want to look atServerless Stackand itsNextjsSiteconstruct for an improved developer experience if you are building serverless applications on CDK.DependenciesBuilt on top ofopen-next, which was partially built using the original core of cdk-nextjs-standalone.Similar projectsThis project is heavily based onOpen-nexthttps://github.com/iiroj/iiro.fi/commit/bd43222032d0dbb765e1111825f64dbb5db851d9https://github.com/sladg/nextjs-lambdahttps://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/compat-layers/apigw-lambda-compatServerless StackRemixSiteconstructNextjsSiteconstructFork from cdk-nextjsCompatible with:cdk-nextjs@3.2.1This project has been initially forked fromcdk-nextjsin order topublish the package to other langugages. So far notable changes are:Extended language support: TS, Java, Go, .NET, Python.Extended platform support: darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-arm64, win32-x64Use pre-built open-next packageContributingHey there, we value every new contribution a lot 🙏🏼 thank you. Open an issue or a PR and we'll gladly help you out.Using ProjenMost boilerplate files are pre-generated including package.json. Don't update it directly, rather update.projenrc.jsthen runyarn projento re-generate the files.