Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Lambda Handler not being found when viewing the generated API Stage #118

Closed
connormcwood opened this issue Nov 5, 2019 · 13 comments · Fixed by #126
Closed

Lambda Handler not being found when viewing the generated API Stage #118

connormcwood opened this issue Nov 5, 2019 · 13 comments · Fixed by #126

Comments

@connormcwood
Copy link

connormcwood commented Nov 5, 2019

Issue

NodeJS 8.10 Handler 'render' missing on module 'contact'
NodeJS 10.x Contact.render is undefined or not exported

Expected Behavior

Page to be displayed.

Actual Behavior

Error message is shown and stack is logged to cloudwatch.

Steps to Reproduce the Problem

  1. Generate Example as shown in the integration section of the repository.
  2. Do a terraform apply
  3. Go to the generated link from the API Gateway and navigate to one of the routes.
  4. Review the cloudwatch log generated.

Specifications

  • Next 9.1.2
  • React 16.11.0
  • React-dom 16.11
  • Node 8.10 (Did try 10.x with updating the lambdas too)
  • Terraform v0.11.15
  • Version: latest
  • Platform: Ubuntu / AWS
  • Subsystem:

Example of lambda contents:

  • Contact.js
    "\n\nconst page = require('./contact.original.js');\nconst http = require('http')\n\nexports.render = (event, context, callback) => {\n\tconst server = new http.Server((req, res) => page.render(req, res));\n\tserver.listen(3000);\n};\n\n\n"

I have set the nextJs config to be serverless but it could be that it is not picking it up.

@ematipico
Copy link
Owner

Hi @connormcwood, would you be able to share a repro where I can reproduce the problem?

If not, could you please share the generated files and/or the generated terraform configuration?

@connormcwood
Copy link
Author

connormcwood commented Nov 7, 2019

Hi @connormcwood, would you be able to share a repro where I can reproduce the problem?

If not, could you please share the generated files and/or the generated terraform configuration?

Take a look at https://github.com/connormcwood/dr-frontend

I use a bash script to move the generated files and apply the terraform. Just go into the app directory run npm run build and then go to the root directory and run ./build/scripts/deploy.sh true.

I believe an improvement to be made for the generated terraform is to maybe improve the depends on for the resources since when I first deploy the script some of the resources fail as I believe they were not created in time.
2 errors occurred: * aws_api_gateway_integration.CustomKey-_error: 1 error occurred: * aws_api_gateway_integration.CustomKey-_error: Error creating API Gateway Integration: NotFoundException: Invalid Method identifier specified status code: 404, request id: fb5b5fca-0f35-4e75-9043-9ad2e1021e27

* aws_api_gateway_deployment.CustomKey: 1 error occurred: * aws_api_gateway_deployment.CustomKey: Error creating API Gateway Deployment: BadRequestException: The REST API doesn't contain any methods status code: 400, request id: eec0e4a6-2da6-41d4-9bbb-e62a553dd591
Second attempt at reapplying terraform apply
1 error occurred: * aws_api_gateway_stage.CustomKey: 1 error occurred: * aws_api_gateway_stage.CustomKey: Error creating API Gateway Stage: ConflictException: Stage already exists status code: 409, request id: 96bea696-9009-495b-8be0-7244ab633218
I manually deleted the created stage in this case.

Third attempt was met with success. Once we get the Gateway working maybe we should split these out into separate issues so that they can be worked on.

It is worth noting that I am using ubuntu to deploy and I have had issues with files not being able to be deleted. Anyway take a look, maybe there is something obvious I have done wrong. Thank you

@connormcwood
Copy link
Author

As the issue suggests. The handler is not being found when the main js file is exporting the handler.

"\n\nconst page = require('./contact.original.js');\nconst http = require('http')\n\nexports.render = (event, context, callback) => {\n\tconst server = new http.Server((req, res) => page.render(req, res));\n\tserver.listen(3000);\n};\n\n\n"

{ "errorType": "Runtime.HandlerNotFound", "errorMessage": "contact.render is undefined or not exported", "stack": [ "Runtime.HandlerNotFound: contact.render is undefined or not exported", " at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)", " at Object.<anonymous> (/var/runtime/index.js:45:30)", " at Module._compile (internal/modules/cjs/loader.js:778:30)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)", " at Module.load (internal/modules/cjs/loader.js:653:32)", " at tryModuleLoad (internal/modules/cjs/loader.js:593:12)", " at Function.Module._load (internal/modules/cjs/loader.js:585:3)", " at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)", " at startup (internal/bootstrap/node.js:283:19)", " at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)" ] }

@connormcwood
Copy link
Author

Changing the package dependencies to the following:

"next": "^9.0.7", "react": "^16.10.1", "react-dom": "^16.10.1"
Appears to allow the application to be built correctly within the Lambda although now it just times out.

@ematipico
Copy link
Owner

Yes, I found out that one of the latest version has broken the lambdas. I will revert that change and release a new version shortly

@connormcwood
Copy link
Author

Yes, I found out that one of the latest version has broken the lambdas. I will revert that change and release a new version shortly

Sorry for the constant messages. Are you able to provide me with a working example so I could have a play around until the fixes are implemented. Wanting to make sure the project works prior to contributing. Thank you

@ematipico
Copy link
Owner

Try using version 1.1.0, that should have a working lambda!

@ematipico
Copy link
Owner

I use the very same lambda code at work (v1.1.0) so it does work

@connormcwood
Copy link
Author

I use the very same lambda code at work (v1.1.0) so it does work

I should have mentioned that along with locking the exact versions of react, react-dom, and next I also locked the version of the plugin to 1.1.0. It did fix the issue with the lambda source code however when going to the created link and one of the endpoints set up the page just times out.

@ematipico
Copy link
Owner

@connormcwood could give a spin to 1.2.1 please? That should fix the problem

@connormcwood
Copy link
Author

connormcwood commented Nov 13, 2019

Hi @ematipico making progress. I believe a dependency has now been added since I updated and it required cosmiconfig to be installed.

Additionally, the lambda code is compiled like:
"\n\nconst compatLayer = require('./compatLayer.js');\nconst page = require('./contact.original.js');\n\nexports.render = (event, context, callback) => {\n\tconst { req, res } = compatLayer(event, callback);\n\tpage.render(req, res);\n};\n\n\n"
Instead of:
const compatLayer = require('./compatLayer.js'); const page = require('./contact.original.js'); exports.render = (event, context, callback) => { const { req, res } = compatLayer(event, callback); page.render(req, res); };

I manually removed the text line breaks.

After removing the added spaces and line breaks I can see the html although the static file doesnt work but I believe an S3 bucket would be used for that.

@ematipico
Copy link
Owner

ematipico commented Nov 13, 2019

About the breaking lines, that's probably due to the JSON.stringify. I will look into that.

About the static file, do you talk the static assets generated by Next.js? If so, they will have to be uploaded separately.
This package could help with this operation but I'd need some help

@ematipico
Copy link
Owner

1.3.0 fixed also the problem with the file. It should not have anymore \n

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants