Giter VIP home page Giter VIP logo

open-next-cdk's Introduction

Typescript Java Go Python .NET
OpenNext CDK

Deploy NextJS on AWS using CDK IaC and OpenNext packaging

Contents

What 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 Lambda

Requirements

NextJs versions: >=12.3.0+ (includes 13.0.0+)

Platforms: darwin-arm64, darwin-x64, linux-arm64, linux-x64, win32-arm64, win32-x64

Quickstart

NextJS setup

Add a dev dependency [email protected] to your NextJS project.

npm install --save-dev [email protected]

CDK Construct

Use this construct in your CDK application to deploy your NextJS app to AWS.

Typescript Typescript NPM release

Install the dependency using npm:

npm install --save-dev [email protected] open-next-cdk

Use the construct in your CDK application:

import { Nextjs } from 'open-next-cdk';

new Nextjs(this, 'Web', {
  nextjsPath: './web', // relative path to nextjs project root
});
Java Java Maven Central release

Install 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();
Go Go Go release

Install the dependency:

go get github.com:datasprayio/open-next-cdk.git@go

Or checkout the code in the go branch.

Python Python Pypi release

Install the dependency:

pip install open-next-cdk
.NET .NET Nuget release

Install the dependency:

dotnet add package Dataspray.OpenNextCdk

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:

Advanced

Pre-built OpenNext package

How-to

You may also provide already pre-built OpenNext package directly by building it yourself first:

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', {
  openNextPath: './web/.open-next', // relative path to .open-next folder
});

Additional security

How-to
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

This project is heavily based on

Fork from cdk-nextjs

Compatible with: cdk-nextjs@3.2.1

This project has been initially forked from cdk-nextjs in order to publish 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-x64
  • Use pre-built open-next package

Contributing

Hey there, we value every new contribution a lot πŸ™πŸΌ thank you. Open an issue or a PR and we'll gladly help you out.

Using Projen

Most boilerplate files are pre-generated including package.json. Don't update it directly, rather update .projenrc.js then run yarn projen to re-generate the files.

open-next-cdk's People

Contributors

revmischa avatar khuezy avatar matusfaro avatar bestickley avatar dependabot[bot] avatar ansgars avatar jadenv avatar durgaprasad-budhwani avatar kevin-mitchell avatar lagyu avatar keenankaufman avatar xinha-sh avatar

Stargazers

Laurence Armstrong avatar Cody avatar Sergey Monin avatar Anggit M Ginanjar avatar Iakhub Seitasanov avatar Don Onwunumah avatar Deysi Lopes avatar AdriΓ‘n Mouly avatar  avatar Jared Lunde avatar James Ulph avatar Vince Fulco--Bighire.tools avatar Rudger avatar  avatar

Watchers

 avatar

Forkers

rud5g

open-next-cdk's Issues

Installing on an M1 mac gives esbuild arm64 not compatible

On trying to install open-next-cdk using:

yarn add -D [email protected] open-next-cdk

I am getting:

yarn add v1.22.19
warning package.json: No license field
warning [email protected]: No license field
[1/4] πŸ”  Resolving packages...
warning open-next-cdk > @aws-sdk/[email protected]: This package has moved to @smithy/signature-v4
[2/4] 🚚  Fetching packages...
error @esbuild/[email protected]: The CPU architecture "arm64" is incompatible with this module.
error Found incompatible module.

It looks to be because the root package.json does not have an @esbuild/darwin-arm64 entry. Is it possible to add this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.