-
-
Save DavidWells/99216c20cdb3df334d5b98ff19644fa2 to your computer and use it in GitHub Desktop.
exports.handler = (event, context, callback) => { | |
const response = { | |
statusCode: 301, | |
headers: { | |
Location: 'https://google.com', | |
} | |
}; | |
return callback(null, response); | |
} |
@diegopamio, and the following
WARNING:
You have to check the choice of "Use lambda proxy integration" while binding the method to lambda in API gateway, or you will get only JSON.
Following are the snapshot.
Further to this -
if you're looking for the docs on why a lambda like this "works", see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html. Lambdas are not "HTTP enabled" by default, but a key way they can be is through a proxy integration with an API Gateway.
This gist is an example how a lambda can conform to an API Gateway's expectations around proxy integrations.
Those docs describe how the output of a lambda is mapped to an HTTP response
(and also describes how HTTP requests are mapped into the event).
To check if you understand: is the use of callback
in the gist necessary?
thank you!
Great for:
- Download URLS
- Link shortening tools
- Unauthorized calls
- Rick rolls
Rick Rolls 😂
Tried out this piece of code, but receiving this error:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'index'",
"Require stack:",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:951:17)",
" at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:976:21)",
" at async start (file:///var/runtime/index.mjs:1137:23)",
" at async file:///var/runtime/index.mjs:1143:1"
]
Anyone else come across this Runtime.ImportModuleError
error?
Tried out this piece of code, but receiving this error:
{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module 'index'", "Require stack:", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:951:17)", " at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:976:21)", " at async start (file:///var/runtime/index.mjs:1137:23)", " at async file:///var/runtime/index.mjs:1143:1" ]
Anyone else come across this
Runtime.ImportModuleError
error?
Can you show the code causing the problem?
Came across this recently. The proper Lambda Edge function should look like:
exports.handler = (event, context, callback) => {
const response = {
status: 301,
headers: {
location: [{
key: 'Location',
value: 'https://google.com',
}],
},
};
return callback(null, response);
}
Furthermore, the Role Trust Relationship for your Lambda should include:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com",
"edgelambda.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
Tried out this piece of code, but receiving this error:
{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module 'index'", "Require stack:", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:951:17)", " at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:976:21)", " at async start (file:///var/runtime/index.mjs:1137:23)", " at async file:///var/runtime/index.mjs:1143:1" ]
Anyone else come across this
Runtime.ImportModuleError
error?
Have you tried naming the js
file as 'index.js'?
For those using API Gateway + Lambda, I followed this: https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/
const response = {
"statusCode": 200,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
The four fields of statusCode, headers, body, and isBase64Encoded were required
Thanks David!!
This helped me a lot!