我有一个用于禁用AWS cloudwatchevents的aws lambda。当我尝试使用自定义模块时,我没有使用自定义模块时工作正常我在另一个模块中需要变量未定义。我做错了什么? var myRuleparams现在是未定义的我没有把整个代码只是一个片段所以它很容易理解
这是有效的index.js
let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
//var cloudrule = require('./cloudrule');
exports.handler = (event, context, callback) => {
let myRule;
const er = event.resources ;
//miRule is the name of the CloudWatchEventRule that started the lambda
myRule = er[0].slice(43);
var myRuleparams = { Name: myRule };
var rulePromise = cloudwatchevents.disableRule(myRuleparams).promise();
rulePromise.then(
function(data) {
console.log("cloudwatchevent has been disabled");
console.log(data);
}).catch(
function(err) {
console.error(err);
//this works
// more code
这是index.js与自定义模块cloudrule.js不起作用
index.js
let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
var cloudrule = require('./cloudrule');
exports.handler = (event, context, callback) => {
let myRule;
const er = event.resources;
//miRule is the name of the CloudWatchEventRule that started the lambda
myRule = er[0].slice(43);
var myRuleparams = { Name: myRule };
// here under custom module with functions and passing Variable myRuleparams
cloudrule.disableCloudWatchRuler(myRuleparams);
//more code
这是cloudrule.js
let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
module.exports = {
disableCloudWatchRuler: function(myRuleparams) {
var rulePromise = cloudwatchevents.disableRule(myRuleparams).promise();
rulePromise.then(
function(data) {
console.log("cloudwatchevent has been disabled");
console.log(data);
}).catch(
function(err) {
console.error(err);
});
}
};
我修好了需要放上myRule;在模块导出之前使其成为全局变量
所以代码看起来应该是这样的
let AWS = require("aws-sdk");
const cloudwatchevents = new AWS.CloudWatchEvents({region: 'us-east-1'});
var cloudrule = require('./cloudrule');
//add let myRule here
let myRule;
exports.handler = (event, context, callback) => {
//remove let myRulefrom here
const er = event.resources;
// more code