Javascript 测试Lambda函数时,index.handler未定义或未导出

Javascript 测试Lambda函数时,index.handler未定义或未导出,javascript,amazon-web-services,aws-lambda,Javascript,Amazon Web Services,Aws Lambda,我有一个JS脚本index.JS,它接受一个变量输入ipAddress,但它给了我以下错误: { "errorType": "Runtime.HandlerNotFound", "errorMessage": "index.handler is undefined or not exported", "trace": [ "Runtime.HandlerNotFoun

我有一个JS脚本
index.JS
,它接受一个变量输入
ipAddress
,但它给了我以下错误:

{
  "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"
  ]
}
该脚本在本地工作:

> node index.js 158.140.127.123
Found ipAddress match
Bucket uploaded successfully at /blocklist-158.140.127.123
但在将zip导入Lambda时不是这样

更新:index.js内容:

// 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)

我认为您不能通过process.arg访问变量。尝试将exports.handler与事件参数一起使用。默认情况下,lambda函数返回/执行处理程序。您可以找到文档

您可以发布
index.js
的内容吗?您是否在
index.js
文件中定义了
处理程序
函数?@jellycsc刚刚用index.js更新了它contents@JohnRotenstein我不相信我有你必须出口处理程序。我认为这是必须的。我还尝试了以下的一些变体
exports.handler=async function(event){return getIpMatchBool(ipAddress)}
和receive
找不到模块“exports”
。。。真奇怪。尝试在GUI中修改一个空白lambda,而不是上传自己的lambda,看看它是否仍然抛出该错误。可能您没有正确导出它。只需修改现有模板,因为它已经正确定义了导出。
// 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)