Looking to host a new web app that was built in NextJS, we set out to find a way to host it on a Lambda rather than via a docker image (which can get rather expensive). These are the steps we took to do so.
Create a static bucket
First thing you will need to do is create an S3 bucket to host your static files that NextJS creates. No need hosting these in the lambda, and S3 is dirt cheap.
For the sake of demo we will call the bucket
nextjs-web-static-bucket. Make sure that you have this bucket set up to host websites.
Compile your NextJS app
You’re going to want to add a couple of values your next.config.js file.
The output makes the NextJS compiler output everything you need to run in a production instance.
The assetPrefix option makes sure that the paths to the staic files are correct, linking to your new bucket rather than the lambda function.
You are now ready to run a
next build command in your terminal.
Upload your static assets
NextJS will have created a
.next/static folder. Upload the contents of that folder to
Create your Lambda
Add a file to the
.next/standalone folder called
run.sh, and put the following in it.
# Execute server.js, which is output using "next build"
exec node server.js
Now, zip up the contents of the
In the AWS console, create your lambda. Upload the zip you just created as the contents of the lambda. You should also under advanced options select the
Enable function URL option, to save you needing to set up an API gateway.
Now comes the magic part. Under code, you want to add a layer. You will be adding the
aws-lambda-web-adapter. Click Add Layer, specify ARN, and enter the following into the box.
Then, go to configuration, and add a couple of environment variables.
- AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap
Save your lambda, and then visit the Function URL.
You can see some example Terraform code on the Gist below