Next-auth: Can explain how to deploy serverless/api part on AWS Lambda?

Created on 23 Jun 2020  路  7Comments  路  Source: nextauthjs/next-auth

Please refer to the documentation, the example project and existing issues before creating a new issue.

Your question
Can explain how to deploy serverless/api part on AWS Lambda?

What are you trying to do
I want to be able to run serverless code on some cloud providers, namely AWS but Google Cloud & Azure would be great too.

Documentation feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [ ] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [x] Could not find relevant documentation
  • [ ] Found the example project helpful
  • [ ] Did not find the example project helpful
question stale

All 7 comments

There is nothing specific to NextAuth.js required to deploy to AWS that I'm aware of (I haven't checked this is still true with 2.0 release build, but it should be!).

I think how to deploy Next.js applications and API routes to AWS Lambda is a wider topic than the scope of this project and not something we (or I) can answer here.

If you'd like an example of one way to do it, you might find this repo helpful: https://github.com/iaincollins/serverless-on-aws

You might also want to check out the work by @danielcondemarin which goes further and supports Lambda at Edge (which is excellent, although comes with caveats): https://github.com/danielcondemarin/serverless-next.js

I'm not sure if NextAuth.js works with Daniel's package and 'Lambda At Edge' (vs regular Lambda), due to the limitations of Lambda at Edge, but it should work great with a regular AWS Lambda service!

You might also want to check out the work by @danielcondemarin which goes further and supports Lambda at Edge (which is excellent, although comes with caveats): https://github.com/danielcondemarin/serverless-next.js

I'm not sure if NextAuth.js works with Daniel's package and 'Lambda At Edge' (vs regular Lambda), due to the limitations of Lambda at Edge, but it should work great with a regular AWS Lambda service!

I had a go at deploying the getting started example via serverless-next.js and worked fine 馃帀
Next up, I'd like to put together an example connecting to a real database rather than in memory.

In terms of Lambda@Edge caveats the main one I've come across is not being able to use a VPC which some AWS Databases need. That's why at some point in future I'd like to resurrect the API GW + plain AWS Lambda version of the project so users have more options.

@danielcondemarin Thank you! That's great to hear! Appreciate you taking the time to try it out!

I am looking forward to trying it out myself and having a tutorial for it at some point.

Impact of v3 release

Just FYI v3 beta is out and will probably replace v2 soon.

It addresses some security issues but it also introduces a shift to using an Environment Variable called NEXTAUTH_URL to set the canonical site URL, so it's easier to configure globally across all pages / routes in a serverless app - so doesn't need to be configured in code in every route or server side rendered page that need it.

However, on reading up I think this may be a problem for Lambda@Edge and I didn't realise that environment variables are apparently not supported on Lambda@Edge (!)

The good news is it is still possible to set the site name explicitly as it was with v2 but the bad news is there are some limitations and it probably needs a tutorial / explainer article to show folks how to set their app up (the documentation for the old methods is being stripped from the docs for v3 to avoid confusing folks).

I'd appreciate any input from you on this if you have any ideas or are familiar with any workarounds. I'd be happy to make changes to the codebase to accommodate supporting Lambda.

RE: VPC

Interesting about the VPC limitations! It sounds like an impact of that is they can't use AWS Aurora Serverless databases then (because they can only be accessed via a a VPC)? I guess a traditional Postgres / MySQL instance on AWS sounds like a better option in that case.

RE: API GW + Plain AWS version - I'd love to see that too! I really like the Lambda at Edge option but for some types of services I still need the slightly more powerful / flexible regular Lambda instances.

I've re-opened this as change in v3 which reduce complexity and edge case problems for most use cases also open up some problems for Lambda@Edge specifically. It seems worth flagging and discussing further.

At very least, I think this could be addressed with a short tutorial, but it would be nice to come up with something that might work better for Lambda@Edge out of the box.

However, on reading up I think this may be a problem for Lambda@Edge and I didn't realise that environment variables are apparently not supported on Lambda@Edge (!)

We've been using Next.js's build time environment variables to work around this. It works by doing inline replacement of your process.env. variables at build time. I believe that's also how the new dotenv support works, so unless I'm missing something it should be fine.

I could try putting together an example using the @beta version instead if it's ready enough?

Interesting about the VPC limitations! It sounds like an impact of that is they can't use AWS Aurora Serverless databases then (because they can only be accessed via a a VPC)?

Interestingly enough, AWS introduced Data API for Aurora which may get around the VPC limitation. From the docs:

_The API also provides a more secure way to use AWS Lambda. It enables you to access your DB cluster without your needing to configure a Lambda function to access resources in a virtual private cloud (VPC)_

I haven't tried it but sounds like it could work.

Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep ot open. Thanks!

Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loonskai picture loonskai  路  3Comments

iaincollins picture iaincollins  路  3Comments

alephart picture alephart  路  3Comments

ryanbahan picture ryanbahan  路  3Comments

bscaspar picture bscaspar  路  3Comments