All Projects → mattdamon108 → Nextjs With Lambda

mattdamon108 / Nextjs With Lambda

Next.js example with deploying to AWS Lambda

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Nextjs With Lambda

Torchlambda
Lightweight tool to deploy PyTorch models to AWS Lambda
Stars: ✭ 83 (+72.92%)
Mutual labels:  lambda, deployment
Terraform Nextjs Plugin
A plugin to generate terraform configuration for Nextjs 8 and 9
Stars: ✭ 41 (-14.58%)
Mutual labels:  lambda, nextjs
Up
Up focuses on deploying "vanilla" HTTP servers so there's nothing new to learn, just develop with your favorite existing frameworks such as Express, Koa, Django, Golang net/http or others.
Stars: ✭ 8,439 (+17481.25%)
Mutual labels:  lambda, deployment
Next Starter
Next.js Starter using GraphQL, MobX (Next.js, TypeScript, Babel, Express.js, Apollo Client, React Apollo, React Apollo Hooks, GraphQL Codegen, MobX, mobx-state-tree, styled-components, next-optimized-images, Serverless Framework, AWS Lambda, Dotenv)
Stars: ✭ 90 (+87.5%)
Mutual labels:  lambda, nextjs
Json Serverless
Transform a JSON file into a serverless REST API in AWS cloud
Stars: ✭ 108 (+125%)
Mutual labels:  lambda, deployment
hyperform
⚡ Lightweight serverless framework for NodeJS
Stars: ✭ 156 (+225%)
Mutual labels:  lambda, deployment
Serverless Next.js
⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
Stars: ✭ 2,977 (+6102.08%)
Mutual labels:  lambda, nextjs
Lambda Deployment Example
Automated Lambda Deployments with Terraform & CodePipeline
Stars: ✭ 25 (-47.92%)
Mutual labels:  lambda, deployment
Serverless Next
How to use Serverless to provide the frontend with a full API with minimal effort and max. scalability
Stars: ✭ 41 (-14.58%)
Mutual labels:  nextjs
Aws Event Sources
Knative event sources for AWS services
Stars: ✭ 45 (-6.25%)
Mutual labels:  lambda
Polydeploy
A Bulk Deployment tool for the DNN CMS. PolyDeploy is focussed on security, reliability and auditability.
Stars: ✭ 40 (-16.67%)
Mutual labels:  deployment
Ridge
AWS Lambda HTTP Proxy integration event bridge to Go net/http.
Stars: ✭ 45 (-6.25%)
Mutual labels:  lambda
Next On Netlify Demo
Demo of a Next.js app with Server-Side Rendering on Netlify
Stars: ✭ 40 (-16.67%)
Mutual labels:  nextjs
Seodeploy
SEODeploy: Flexible and Modular Python Library for Automating SEO Testing in Deployment Pipelines.
Stars: ✭ 41 (-14.58%)
Mutual labels:  deployment
Nextjs Multiple Domains
Example using SSR to handle multiple domains on the same project.
Stars: ✭ 46 (-4.17%)
Mutual labels:  nextjs
Sentry
Kubernetes Object Validating Admission Controller
Stars: ✭ 40 (-16.67%)
Mutual labels:  deployment
Getlink Next
Get Link!
Stars: ✭ 48 (+0%)
Mutual labels:  nextjs
Intercom Clone
Intercom clone built with NextJS and TailwindCSS
Stars: ✭ 48 (+0%)
Mutual labels:  nextjs
Corgi
AWS Lambda / API Gateway native, fast and simple web framework
Stars: ✭ 44 (-8.33%)
Mutual labels:  lambda
Lambda Coding Round Evaluator
lambda-coding-round-evaluator is a Serverless application to automate coding round submission and evaluation. It helps you get rid of emails and easily filter out bad candidates. Yay!
Stars: ✭ 43 (-10.42%)
Mutual labels:  lambda

Next.js with AWS Lambda

This is a tiny example to show how to deploy the Next.js app on AWS Lambda using Apex Up.

You may encounter the cold start issue, then refer to the lambda warmer. https://github.com/mattdamon108/lambda-warmer

Next.js app with custom server

The custom server for Next.js app is needed to run your app on AWS Lambda. In this example, express will be used.

// server.js

const express = require("express");
const next = require("next");

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app
  .prepare()
  .then(() => {
    const server = express();

    server.get("/", (req, res) => {
      return app.render(req, res, "/", req.params);
    });

    server.get("/about", (req, res) => {
      return app.render(req, res, "/about", req.params);
    });

    server.get("*", (req, res) => {
      return handle(req, res);
    });

    server.listen(port, err => {
      if (err) throw err;
      console.log(`> Ready on http://localhost:${port}`);
    });
  })
  .catch(ex => {
    console.log(ex);
    process.exit(1);
  });

update package.json with custom server.js

{
  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
  }
}

Install Apex Up

$ curl -sf https://up.apex.sh/install | sh

# verify the installation
$ up version

AWS credentials

You need to have one AWS account and recommend to use IAM with programmaic way for security and convinience. If you have already installed awscli or awsebcli, etc. You are having ~/.aws/credentials which is storing AWS credentials. It allows you to use AWS_PROFILE environment. If you don't please make one and save it with your account access key and security key in it.

# ~/.aws/credentials

[my-aws-account-for-lambda]
aws_access_key = xxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxx

IAM policy for Up CLI

IAM policy allows the Up to access your AWS resources in order to deploy your Next.js app on Lambda. Go to AWS IAM and make the new policy and link it up to your AWS account.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "acm:*",
        "cloudformation:Create*",
        "cloudformation:Delete*",
        "cloudformation:Describe*",
        "cloudformation:ExecuteChangeSet",
        "cloudformation:Update*",
        "cloudfront:*",
        "cloudwatch:*",
        "ec2:*",
        "ecs:*",
        "events:*",
        "iam:AttachRolePolicy",
        "iam:CreatePolicy",
        "iam:CreateRole",
        "iam:DeleteRole",
        "iam:DeleteRolePolicy",
        "iam:GetRole",
        "iam:PassRole",
        "iam:PutRolePolicy",
        "lambda:AddPermission",
        "lambda:Create*",
        "lambda:Delete*",
        "lambda:Get*",
        "lambda:InvokeFunction",
        "lambda:List*",
        "lambda:RemovePermission",
        "lambda:Update*",
        "logs:Create*",
        "logs:Describe*",
        "logs:FilterLogEvents",
        "logs:Put*",
        "logs:Test*",
        "route53:*",
        "route53domains:*",
        "s3:*",
        "ssm:*",
        "sns:*"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "apigateway:*",
      "Resource": "arn:aws:apigateway:*::/*"
    }
  ]
}

Create up.json file

{
  "name": "nextjs-example",
  // aws account profile in ~/.aws/credentials
  "profile": "my-aws-account-for-lambda",
  "regions": ["ap-northeast-2"],
  "lambda": {
    // min 128, default 512
    "memory": 256,
    // AWS Lambda supports node.js 8.10 latest
    "runtime": "nodejs8.10"
  },
  "proxy": {
    "command": "npm start",
    "timeout": 25,
    "listen_timeout": 15,
    "shutdown_timeout": 15
  },
  "stages": {
    "development": {
      "proxy": {
        "command": "yarn dev"
      }
    }
  },
  "environment": {
    // you can hydrate env variables as you want.
    "NODE_ENV": "production"
  },
  "error_pages": {
    "variables": {
      "support_email": "[email protected]",
      "color": "#2986e2"
    }
  }
}

Build the Next.js app before deploy

$ yarn build

Create .upignore file

The Up will inspect your files to compose and deploy to lambda. Firstly The up will read .gitignore and ignore files written in .gitignore. And after that, .upignore will be read. The Up, by default, ignores dotfiles, so needs to negate .next directory in .upignore in order for the Up will build the package with it.

# .upignore

!.next

Deploy

$ up
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].