Deploy Next JS App to AWS Lambda using AWS SAM

To Nha Notes | Dec. 23, 2024, 10:27 p.m.

Projects using Prisma Client can be deployed to many different cloud platforms. Given the variety of cloud platforms and different names, it's noteworthy to mention the different deployment paradigms, as they affect the way you deploy an application using Prisma Client.

General considerations when deploying to AWS Lambda

AWS Lambda Web Adapter allows developers to build web apps (http api) with familiar frameworks (e.g. Express.js, Next.js, Flask, SpringBoot, ASP.NET and Laravel, anything speaks HTTP 1.1/1.0) and run it on AWS Lambda. The same docker image can run on AWS Lambda, Amazon EC2, AWS Fargate, and local computers.

Configure SAM template

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  serverless app to deploy next app to aws lambda

Globals:
  Function:
    Timeout: 30

Resources:
  NextAppStackFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: app/
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        HttpEvent:
          Type: HttpApi
    Metadata:
      DockerTag: v1
      DockerContext: ./app
      Dockerfile: Dockerfile
Outputs:
  NextAppStackUrl:
    Description: "API Gateway endpoint URL for servering the next app"
    Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/"

Running Next.js applications with Serverless services on AWS

Either CDK or SAM create a demo environment showing how you can use Amazon API Gateway, AWS Lambda, Amazon CloudFront, and Amazon S3 to run a Next.js application.

Solution Architecture

Next.js Serverless Architecture

References

https://www.prisma.io/docs/orm/prisma-client/deployment/deploy-prisma

https://dev.to/taranjeetsingh/deploy-next-js-app-to-aws-lambda-using-aws-sam-5ael

https://github.com/singh-taranjeet/next-aws-lambda

https://github.com/aws-samples/aws-lambda-nextjs

https://github.com/aws-samples/aws-lambda-nextjs/tree/main/nextjs-lambda-sam

https://dev.to/aws-builders/serverless-simplicity-deploy-run-your-nodejs-framework-app-on-aws-lambda-14hn

https://repost.aws/questions/QUV8zfe-ESSLqAu1Cr9HeISg/using-amplify-with-next-js-and-existing-aws-serverless-sam-app

https://github.com/awslabs/aws-lambda-web-adapter/tree/main/examples

https://www.linkedin.com/pulse/deploying-nextjs-applications-aws-lambda-step-by-step-juan-soares-g3y6f

https://www.freecodecamp.org/news/deployment-strategies-for-nextjs-apps/

https://docs.aws.amazon.com/amplify/latest/userguide/deploy-nextjs-app.html

https://medium.com/@redrobotdev/next-js-deployment-using-ecs-with-fargate-1a730a8d0cb1

https://medium.com/@mudasirhaji/deploying-a-next-js-app-manually-on-aws-ec2-a-step-by-step-guide-58b266ff1c52

https://redrobot.dev/learn/articles/nextjs-containerization#containerizing-a-nextjs-application