Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created June 28, 2018 20:48
Show Gist options
  • Star 93 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save DavidWells/99216c20cdb3df334d5b98ff19644fa2 to your computer and use it in GitHub Desktop.
Save DavidWells/99216c20cdb3df334d5b98ff19644fa2 to your computer and use it in GitHub Desktop.
How to do a 301 redirect from an AWS lambda function
exports.handler = (event, context, callback) => {
const response = {
statusCode: 301,
headers: {
Location: 'https://google.com',
}
};
return callback(null, response);
}
@samirm
Copy link

samirm commented Sep 6, 2022

thank you!

@Divuzki
Copy link

Divuzki commented Sep 12, 2022

Great for:

  • Download URLS
  • Link shortening tools
  • Unauthorized calls
  • Rick rolls

Rick Rolls 😂

@lzhou0
Copy link

lzhou0 commented Sep 26, 2022

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?

@Divuzki
Copy link

Divuzki commented Sep 27, 2022

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?

@BuffMcBigHuge
Copy link

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"
        }
    ]
}

@anandakumarpalanisamy
Copy link

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'?

@renchris
Copy link

renchris commented Nov 21, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment