{"id":13423693,"url":"https://github.com/mununki/nextjs-with-lambda","last_synced_at":"2025-05-07T08:13:32.344Z","repository":{"id":143922203,"uuid":"165061613","full_name":"mununki/nextjs-with-lambda","owner":"mununki","description":"Next.js example with deploying to AWS Lambda","archived":false,"fork":false,"pushed_at":"2019-04-11T14:40:51.000Z","size":87,"stargazers_count":59,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-02T08:06:49.487Z","etag":null,"topics":["deployment","lambda","nextjs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mununki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-01-10T13:07:12.000Z","updated_at":"2024-07-16T01:26:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"16ed7a57-2139-4595-8376-cfcdd5751c9a","html_url":"https://github.com/mununki/nextjs-with-lambda","commit_stats":null,"previous_names":["mattdamon108/nextjs-with-lambda"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fnextjs-with-lambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fnextjs-with-lambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fnextjs-with-lambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mununki%2Fnextjs-with-lambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mununki","download_url":"https://codeload.github.com/mununki/nextjs-with-lambda/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252609494,"owners_count":21775826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deployment","lambda","nextjs"],"created_at":"2024-07-31T00:00:40.780Z","updated_at":"2025-05-07T08:13:32.318Z","avatar_url":"https://github.com/mununki.png","language":"JavaScript","funding_links":[],"categories":["Boilerplates"],"sub_categories":[],"readme":"# Next.js with AWS Lambda\n\nThis is a tiny example to show how to deploy the Next.js app on AWS Lambda using [Apex Up](https://up.docs.apex.sh/#introduction).\n\n- Post : https://moondaddi.dev/post/2019-01-11-Deploy-Nextjs-app-to-AWS-Lambda/\n\n\u003e You may encounter the cold start issue, then refer to the lambda warmer.\n\u003e https://github.com/mattdamon108/lambda-warmer\n\n- Post : https://moondaddi.dev/post/2019-03-23-how-to-warm-lambda-function/\n\n## Next.js app with custom server\n\nThe custom server for Next.js app is needed to run your app on AWS Lambda. In this example, `express` will be used.\n\n```javascript\n// server.js\n\nconst express = require(\"express\");\nconst next = require(\"next\");\n\nconst port = parseInt(process.env.PORT, 10) || 3000;\nconst dev = process.env.NODE_ENV !== \"production\";\nconst app = next({ dev });\nconst handle = app.getRequestHandler();\n\napp\n  .prepare()\n  .then(() =\u003e {\n    const server = express();\n\n    server.get(\"/\", (req, res) =\u003e {\n      return app.render(req, res, \"/\", req.params);\n    });\n\n    server.get(\"/about\", (req, res) =\u003e {\n      return app.render(req, res, \"/about\", req.params);\n    });\n\n    server.get(\"*\", (req, res) =\u003e {\n      return handle(req, res);\n    });\n\n    server.listen(port, err =\u003e {\n      if (err) throw err;\n      console.log(`\u003e Ready on http://localhost:${port}`);\n    });\n  })\n  .catch(ex =\u003e {\n    console.log(ex);\n    process.exit(1);\n  });\n```\n\n## update `package.json` with custom `server.js`\n\n```json\n{\n  \"scripts\": {\n    \"dev\": \"node server.js\",\n    \"build\": \"next build\",\n    \"start\": \"NODE_ENV=production node server.js\"\n  }\n}\n```\n\n## Install Apex Up\n\n```shell\n$ curl -sf https://up.apex.sh/install | sh\n\n# verify the installation\n$ up version\n```\n\n## AWS credentials\n\nYou 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.\n\n```shell\n# ~/.aws/credentials\n\n[my-aws-account-for-lambda]\naws_access_key = xxxxxxxxxxxxx\naws_secret_access_key = xxxxxxxxxxxxxxxxxxxx\n```\n\n## IAM policy for Up CLI\n\nIAM policy allows the Up to access your AWS resources in order to deploy your Next.js app on Lambda. Go to [AWS IAM](https://aws.amazon.com/iam/) and make the new policy and link it up to your AWS account.\n\n```json\n{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Effect\": \"Allow\",\n      \"Action\": [\n        \"acm:*\",\n        \"cloudformation:Create*\",\n        \"cloudformation:Delete*\",\n        \"cloudformation:Describe*\",\n        \"cloudformation:ExecuteChangeSet\",\n        \"cloudformation:Update*\",\n        \"cloudfront:*\",\n        \"cloudwatch:*\",\n        \"ec2:*\",\n        \"ecs:*\",\n        \"events:*\",\n        \"iam:AttachRolePolicy\",\n        \"iam:CreatePolicy\",\n        \"iam:CreateRole\",\n        \"iam:DeleteRole\",\n        \"iam:DeleteRolePolicy\",\n        \"iam:GetRole\",\n        \"iam:PassRole\",\n        \"iam:PutRolePolicy\",\n        \"lambda:AddPermission\",\n        \"lambda:Create*\",\n        \"lambda:Delete*\",\n        \"lambda:Get*\",\n        \"lambda:InvokeFunction\",\n        \"lambda:List*\",\n        \"lambda:RemovePermission\",\n        \"lambda:Update*\",\n        \"logs:Create*\",\n        \"logs:Describe*\",\n        \"logs:FilterLogEvents\",\n        \"logs:Put*\",\n        \"logs:Test*\",\n        \"route53:*\",\n        \"route53domains:*\",\n        \"s3:*\",\n        \"ssm:*\",\n        \"sns:*\"\n      ],\n      \"Resource\": \"*\"\n    },\n    {\n      \"Effect\": \"Allow\",\n      \"Action\": \"apigateway:*\",\n      \"Resource\": \"arn:aws:apigateway:*::/*\"\n    }\n  ]\n}\n```\n\n## Create `up.json` file\n\n```js\n{\n  \"name\": \"nextjs-example\",\n  // aws account profile in ~/.aws/credentials\n  \"profile\": \"my-aws-account-for-lambda\",\n  \"regions\": [\"ap-northeast-2\"],\n  \"lambda\": {\n    // min 128, default 512\n    \"memory\": 256,\n    // AWS Lambda supports node.js 8.10 latest\n    \"runtime\": \"nodejs8.10\"\n  },\n  \"proxy\": {\n    \"command\": \"npm start\",\n    \"timeout\": 25,\n    \"listen_timeout\": 15,\n    \"shutdown_timeout\": 15\n  },\n  \"stages\": {\n    \"development\": {\n      \"proxy\": {\n        \"command\": \"yarn dev\"\n      }\n    }\n  },\n  \"environment\": {\n    // you can hydrate env variables as you want.\n    \"NODE_ENV\": \"production\"\n  },\n  \"error_pages\": {\n    \"variables\": {\n      \"support_email\": \"admin@my-email.com\",\n      \"color\": \"#2986e2\"\n    }\n  }\n}\n```\n\n## Build the Next.js app before deploy\n\n```shell\n$ yarn build\n```\n\n## Create `.upignore` file\n\nThe 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.\n\n```\n# .upignore\n\n!.next\n```\n\n## Deploy\n\n```shell\n$ up\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmununki%2Fnextjs-with-lambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmununki%2Fnextjs-with-lambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmununki%2Fnextjs-with-lambda/lists"}