首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试Lambda函数时index.handler未定义或未导出

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

Stack Overflow用户
提问于 2020-09-22 08:57:57
回答 1查看 2K关注 0票数 0

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

代码语言:javascript
复制
{
  "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"
  ]
}

我的压缩存档在解压后看起来是这样的:

代码语言:javascript
复制
.
├── index.js
├── node_modules/
└── package.json

该脚本在本地工作:

代码语言:javascript
复制
> node index.js 158.140.127.123
Found ipAddress match
Bucket uploaded successfully at /blocklist-158.140.127.123

但在将zip导入到Lambda时不会

更新: index.js内容:

代码语言:javascript
复制
// 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)
EN

回答 1

Stack Overflow用户

发布于 2020-09-22 09:54:45

我不认为你可以通过process.arg访问你的变量。尝试使用带有exports.handler参数的事件。默认情况下,这个处理程序是lambda函数返回/执行的。您可以找到文档here.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64001692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档