I tried creating a cloudwatch event rule with the below code. However, it doesn't seem to be working. I checked AWS CloudWatch Events, and the rule is there with my attached lambda function. However, the event never gets triggered as it should. How can I fix this problem?
async function createCloudWatchEvent(zip){
if(zip){
try{
await putRule(zip);
await putTarget(zip);
} catch (e) {
console.log(`Can't create [${zip}] cloudwatch event`);
}
}
}
function putRule (ruleName) {
let cloudWatchParams = {
Name: ruleName, // required
ScheduleExpression: 'rate(1 minute)',
State: 'ENABLED'
};
return new Promise(resolve => {
cloudwatchevent.putRule(cloudWatchParams, async function(err, data) {
if(err){
console.log(err);
resolve(err);
}else{
console.log('Successfully created rule: ' + ruleName);
resolve();
}
});
});
}
function putTarget (ruleName) {
let cloudWatchParams = {
Rule: ruleName, // required
Targets: [ // required
{
Arn: 'xxxxxxxxxx',
Id: 'Determine_State',
Input: JSON.stringify({zip: ruleName})
}
]
};
return new Promise(resolve => {
// Attach "Determine_State" target to cloudwatch event
cloudwatchevent.putTargets(cloudWatchParams, function(err, data) {
if(err){
console.log(err);
resolve(err);
}else{
console.log('Successfully attached target [' + cloudWatchParams.Targets[0].Id + '] to rule: ' + ruleName);
resolve();
}
});
});
}