简体   繁体   中英

index.handler is undefined or not exported when testing Lambda function

I have a JS script index.js that takes one variable input ipAddress but it's giving me the following error:

{
  "errorType": "Runtime.HandlerNotFound",
  "errorMessage": "index.handler is undefined or not exported",
  "trace": [
    "Runtime.HandlerNotFound: index.handler is undefined or not exported",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1137:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)",
    "    at Module.load (internal/modules/cjs/loader.js:985:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:878:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

my zipped archive looks like this once unzipped:

.
├── index.js
├── node_modules/
└── package.json

The script does work locally:

> node index.js 158.140.127.123
Found ipAddress match
Bucket uploaded successfully at /blocklist-158.140.127.123

But not when importing the zip to Lambda

UPDATE: index.js content:

// Usage: index.js x.x.x.x
'use strict';

const AWS = require('aws-sdk');
AWS.config.update({
    region: "us-east-1"
});

// Get ipAddress from user argument (will eventually get input from sns)
var ipAddress = process.argv[2]

// Get geolocation from ipAddress
function getIpAddressGeo(ipAddress) {
    const geoip = require('geoip-lite');
    var geo = geoip.lookup(ipAddress);

    return geo;
};

async function getIpMatchBool() {
    const request = require('request');
    const promise = require('promise');

    var lookupUrl = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/bi_sip_0_1d.ipset";

    // This should only create a bucket if getIpMatchBool returns true
    function createS3IpListing(ipAddress) {
        var date = Date.now();
        const s3 = new AWS.S3({
            apiVersion: '2006-03-01',
            accessKeyId: process.env.AWS_ACCESS_KEY,
            secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
        });
        const bucketParams = {
            Bucket: `blocklist-${ipAddress}`
        };
        s3.createBucket(bucketParams, function(err, data) {
            if (err) {
                console.log("Error", err);
            } else {
                console.log(`Bucket uploaded successfully at ${data.Location}`);
            }
        });

        // createFile not returning - working on this now
        const createFile = (geo = getIpAddressGeo()) => {
            const fs = require('fs');

            fs.readFile(date, (err, data) => {
                if (err) throw err;
                const params = {
                    Bucket: bucketParams.Bucket,
                    Key: date,
                    Body: JSON.stringify(data, geo)
                };
                s3.upload(params, function(s3Err, data) {
                    if (s3Err) throw s3Err
                    console.log(`File uploaded successfully at ${data.Location}`)
                });
            });
            return createFile(geo)

        };

    };

    // This RegEx does not account for the differance between 10 & 10.x.x.x - need to fix
    var re = new RegExp(ipAddress.replace(/\./g, '\\.'), 'gi');

    const issueRequest = () => {
        return new promise(() => {
            request(lookupUrl, function(error, response, body) {
                if (!error && response.statusCode == 200) {
                    if (body.search(re) == -1) {
                        console.log("No ipAddress match");
                    } else {
                        createS3IpListing(ipAddress);
                        console.log("Found ipAddress match");
                    }
                }
            })
        });
    }
    return await issueRequest();
}

getIpMatchBool(ipAddress)

I don't think you can access your variable through process.arg. Try using exports.handler the with the event parameter. The handler is what the lambda function returns/executes by default. You can find the documentation here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM