aws lambda edge TypeError modifying headers - node.js

I have been following the guide here to try out lambda#edge. only difference is I am using node.js 10.x since 6.10 mentioned in the docs is not longer available.
when testing the code with the 'AWS Cloudfront Modify Response Header' the lambda executes successfully. However when actually calling the website over cloudfront, the lambda shows the following error.
2021-04-25T14:45:47.820Z 5ab6e11c-02eb-4c87-a9d2-bba1eca75698 ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'headers' of undefined",
"stack": [
"TypeError: Cannot read property 'headers' of undefined",
" at Runtime.exports.handler (/var/task/index.js:3:30)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
I am not a programmer, little experience I have is with python but suspect the event body it is receiving is not in the correct format needed to be?

Related

Error messages displayed when a test fails using MochaJs

I am using Mocha with chai to perform unit testing. But I can't comprehend the error messages displayed when a test fails.
For instance, I have a simple api, that only accepts alpha characters for certain inputs. If the user attempts to submit something like "3rror", the api will return a status of 403, or some other error code.
With Mocha I can assert something like : response.should.have.status(403); to check the server is catching and serving invalid requests appropriately.
However; with Mocha, I am completely lost when trying to reason out why or what caused an error to occur. Did I write a bad test, did my sanitization/validation middleware fail, am I even connected to my server, or am I failing to handle invalid requests and/or errors appropriately?
Below is an example of an error message Mocha provides for the mentioned case.
Uncaught TypeError: Cannot read properties of undefined (reading 'should')
at C:\...\test.js:53:43
at Test.Request.callback (node_modules\chai-http\node_modules\superagent\lib\node\index.js:716:12)
at IncomingMessage.<anonymous> (node_modules\chai-http\node_modules\superagent\lib\node\index.js:916:18)
at IncomingMessage.emit (node:events:539:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I understand it gave the wrong status code, but these error messages don't really help me understand what went wrong. Did I just goof an write a bad test, or did my app fail? So I am here to ask for help.
How can I better understand these errors reported when a test fails using Mocha?

NODE_OPTIONS='--insecure-http-parser' not being used with AWS Lambda

One of the web services my lambda connects to is periodically returning an invalid HTTP header and causing Node to throw with this error.
We are running lambda with the NodeJS 12.x runtime.
"name": "RequestError",
"message": "Error: Parse Error: Invalid header value char",
"cause": {
"bytesParsed": ,
"code": "HPE_INVALID_HEADER_TOKEN",
....
Following the instructions from this link it looks like the fix for 12.X would be to either use --insecure-http-parser or --http-parser=legacy as an option to starting Node.
Passing insecureHTTPParser to http.request() is not an option since we are using a http abstraction.
On Lambda the documented way to pass CLI options to use is to use the NODE_OPTIONS environment variable which is documented here
So I've tried setting either --insecure-http-parser or --http-parser=legacy and spawning a new lambda invocation and I'm still getting the HPE_INVALID_HEADER_TOKEN error.
Here's a screenshot of my Environment Variables in lambda.
Upgrading the NodeJS runtime is not an option too.
My high level question is how can I make this work?
Some other useful information in the comments would be
How can I verify that NODE_OPTIONS passed to Lambda in an environment variable are actually set at runtime?
How can I verify what HTTP parser is configured to be used in Node 12.x?

HERE Api "errorMessage": "Cannot read property 'status' of undefined",

I have just created a geocoding lambda service using the
AWS Serverless Application Repository. This uses the HERE api.
When testing the api in API Gateway using the following url
https://geocode.json.search.hereapi.com/6.2/geocode.json?apikey=XXXXX&searchcode=glasgow
Which is generated by the lambda code generated by AWS
I see the error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'status' of undefined",
"stack": [
"TypeError: Cannot read property 'status' of undefined",
" at getData (/var/task/geocode.js:38:36)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)"
]
}
I have probably made a simple error but can someone tell me how to resolve this. I cannot see in the Docs etc where the cause is.
Thanks
Check your lambda function where you have an undefined object :
You object is undefined before access to status attribut : yourObject.status

problem running simple demo example google-maps-services-js with Node

I have a problem running this simple demo example for google-maps-service with Node:
https://github.com/jpoehnelt/google-maps-services-js-demo
I am not sure what I am missing. When I run npm start, I get this error:
TypeError: Cannot read property 'elevation' of undefined
at D:\node_practice\google-maps-services-js-demo\index.js:14:35
at processTicksAndRejections (internal/process/task_queues.js:97:5)
I am using nvm and my node version is 13.9.0. I tried it on two different machines (one windows with above node version, one ubuntu) and both got the same error.
it seems the response coming back is empty. your r.data is empty, which means results[0] is undefined, so is the reason you are getting Cannot read property 'elevation' of undefined.
Below is the actual exception coming back from the APIs
"You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account"
Get the instruction of getting an API key from here https://developers.google.com/maps/documentation/directions/get-api-key
and use the same with your code.

hyperledger fabric rest API to smart contract

I am making a custom API endpoint to my hyperledger fabric smart contract and i am using postman to test. The post is status of 200 but throws and error of cant read submitTransaction() function how to fix this error
terminal error:
TypeError: Cannot read property 'submitTransaction' of undefined
at Object.exports.invoke(/home/hlf/dpt_old/webapp/api/network.js:133:50)
at router.post (/home/hlf/dpt_old/web-app/api/routes/members.js:32:34)at <anonymous>at process._tickCallback(internal/process/next_tick.js:188:7)
POST /members/ 200 88.465 ms - 386
postman error:
"message": "Cannot read property 'submitTransaction' of undefined",
"stack": "TypeError: Cannot read property 'submitTransaction' of undefined\n at Object.exports.invoke (/home/hlf/dpt_old/web-app/api/network.js:133:50)\n at router.post (/home/hlf/dpt_old/web-app/api/routes/members.js:32:34)\n at <anonymous>\n at process._tickCallback (internal/process/next_tick.js:188:7)"

Resources