serverless-nextjs / serverless-next.js

⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
MIT License
4.47k stars 457 forks source link

Bucket existence validated before bucket actually created #119

Open RichiCoder1 opened 5 years ago

RichiCoder1 commented 5 years ago

Howdy! I'm using the assetPrefix option to let the plugin deploy the assets bucket. However, even after serverless remove with no pre-existing stack whatsoever, I guess this error:

Serverless Nextjs: Copying next pages to tmp build folder
Serverless Nextjs: Found 2 next page(s)
Serverless Nextjs: Creating compat handler for page: _error.js
Serverless Nextjs: Creating compat handler for page: index.js
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless Nextjs: Cleaning up tmp build folder ...
Serverless Nextjs: Found bucket "serverless-nextjs-richard-dev-assets-bucket" from serverless.yml
Serverless Nextjs: Proxying NextJS assets -> https://s3-us-east-2.amazonaws.com/serverless-nextjs-richard-dev-assets-bucket/_next/{proxy}
Serverless Nextjs: Proxying static files -> https://s3-us-east-2.amazonaws.com/serverless-nextjs-richard-dev-assets-bucket/static/{proxy}

  Error --------------------------------------------------

  The assets bucket "serverless-nextjs-richard-dev-assets-bucket" does not exist. Create it manually or delete the stack and recreate it.

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              10.15.3
     Serverless Version:        1.49.0
     Enterprise Plugin Version: 1.3.8
     Platform SDK Version:      2.1.0

I'm very confused too as I thought serverless-nextjs-plugin handled this? And it seems like this check is hapenning before serverless actually has a change to deploy the cloudformation stack that creates bucket for the first time?

RichiCoder1 commented 5 years ago

I (partially) figured it out. If deploymentBucket is set, createStack doesn't actually, well, create the initial stack. Not sure how to fix this.

RichiCoder1 commented 5 years ago

Code for reference: https://github.com/serverless/serverless/blob/v1.50.0/lib/plugins/aws/deploy/lib/createStack.js#L85

Deadleg commented 5 years ago

Looks like the issue arises because the checking of the bucket happens before the stack is update. The offending code is here https://github.com/danielcondemarin/serverless-nextjs-plugin/blob/fcd19e026d5c9fc098be79986142f9108732bf6c/packages/serverless-nextjs-plugin/index.js#L29 and you can see the order in which hooks are run here https://gist.github.com/HyperBrain/50d38027a8f57778d5b0f135d80ea406.

I believe if the bucket is created as part of the first sls deploy then createStack will create the bucket and not cause an issue, but if you run sls deploy first then add the bucket later the check will fail.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.