-
Notifications
You must be signed in to change notification settings - Fork 27.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add serverless example hosted on AWS Lambda #4609
Conversation
Can we use |
@timneutkens Updated example with simple hello-world. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the i18n code can be removed then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove i18n code from the example and updated README instructions.
|
||
const dev = process.env.NODE_ENV !== 'production' | ||
const app = next({ dev }) | ||
const handle = app.getRequestHandler() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.prepare()
is missing here.
functions: | ||
nextJsRootApp: | ||
handler: lambda.handler | ||
events: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can merge nextJsRootApp
and nextJsAnyApp
events - just change the events to this:
events:
- http: ANY {proxy+}
+ - http: ANY /
service: nextjs-aws-serverless | ||
|
||
package: | ||
exclude: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add yarn.lock
as well.
"plugins": [ | ||
"transform-decorators-legacy" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove space before }
|
||
provider: | ||
name: aws | ||
runtime: nodejs6.10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should change this to Node 8.10 (better performance & cost less)
I'm assuming we'll be waiting for #4496 in order for this to work, right? :D |
@Skaronator I tried to test this out.. however it didn't work? After I deployed the application and went to the site.. and tried to click the link.. it gave a "Forbidden" message? Any ideas on that? |
Check out this repo: https://github.com/mitchellhuang/future You need these files: lambda.js, next.config.js and server.js + serverless config |
@Skaronator Is the domain with the api gateway required? :) |
Yes you need API Gateway which trigger Lambda. You can either use your own domain or use the AWS Gateway URL. |
CloudFront is not required. (I think it doesn't even work since it is just a CDN) Looks like you're trying to deploy mitchellhuang/future but I've never done that. Only copyed the relevant parts out of it. |
@Skaronator idk still couldn't get it working even after putting your stuff in.. the index page works fine..but after I click a link to go to a new route it just says Forbidden. I guess i'll just wait until an official example is released. |
@@ -0,0 +1,31 @@ | |||
{ | |||
"name": "react-i18next-nextjs-example", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should update the name
field without i18n-next
I think I've found the culprit here @PR1YANKPAT3L . If you open up your browsers dev tools you'll notice that scripts fail to load from Note that this is not needed if you would use a custom domain via I've deployed the material-ui nextjs example to Lambda in this repo Best |
@skriems Your repo worked great for me! Note: To get the deployment working through Cloudfront and a custom domain, I needed to remove the following from the server.js file:
When setting up Cloudfront I made the My working version here: https://sls.vgo2x.com :) |
@lolcoolkat That looks cool :) I've updated the repo now to utilizes
This obviously depends on having setup a domain and certificate (which imho should be mandatory nowadays so I hardcoded it here). So only if the app is deployed on Lambda it should set an Now, thinking this further in case of the custom domain we should also be appending the I think I'm only updating my docs in this regard and explain the hurdles I experienced. Note that you could simply opt-out of using a custom domain by specifying @PR1YANKPAT3L and/or @Skaronator I could either make the necessary commits here or create a new PR if you guys have no objections. |
Weird... I'm using a custom domain via |
How odd. I just tried again without adding a prefix and with |
exactly that's the problem. Any opinion on how this should be included/documented in the example? |
It might be better to use Lambda@Edge instead of Lambda with APIGateway but Serverless doesn't support it yet due to a bug in CloudFormation. |
@skriems next-material serverless example worked great. If the requested changes in this pull are not being acted on promptly, can we just get that into the next.js examples section? Also, I see a few serverless example request tickets imply that there might be good reason for a next.js example to handle the "scenario where the express server is not initialized right away." Can any of the serverless hackers in this thread confirm if a serverless example based on "with-react-i18next" would be different in any fundamental way than one based on "hello-world"? Does the next-material example use an express server not initialized right away? It would also be considerate to provide a cautionary note for new users that serverless may not be the optimal choice for consumer facing products due to the unavoidable latency it adds. A traditional deployment can consistently return sub 50ms response time but AWS Lambda, the same example varies wildly between 150ms and 2.5s when "warm". |
If you have 2.5s of response time, you did something wrong with your lambda. Waiting for #4496 though ! |
also, why serverless-http over aws-serverless-express ? |
@romainquellec It is certainly possible and testing was extremely unscientific. So everyone can do their own testing I will elaborate slightly: If I refresh my serverless next.js example quickly (once a second) the responses definitely tend to average closer to 150ms. I think this indicates my lambda was fine. If I wait a few minutes between each refresh (small enough interval that containers should be warm, trying to replicate my actual site traffic pattern), responses vary wildly between 150ms and 2.5s. I assume amazon knows my traffic pattern is insignificant and not giving me a "keep warm" status. YMMV and this is just food for thought as I was just messing around. (There does seem to be some evidence that warm up just reduces chances of cold start) |
I'm going to close this PR in favor of the work we're doing towards making Next.js output single-route lambdas. |
I was able to get next.js working with serverless as requested by #4482.
This example uses with-react-i18next as a scenario where the express server is not initialized right away.