I am trying to use aws cloudwatch events for task scheduling in Node js, So , I created a cloudwatch event rule for this, Then I added a lambda function as a target. I am using event patterns to create a cloudwatch event rule here instead of scheduled expression. Now I am trying to add events to it at a scheduled time , but the event triggers instantly as soon as a put a event on cloudwatch. I don't know why this is happening, I researched a lot but didn't find anything relevant.
I used these official docs as reference :- https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchEvents.html#putEvents-property https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/cloudwatch-examples-sending-events.html
I am new to AWS , please help me here.
This is the code performing the above mentioned action.
var params = {
Name: 'DEMO_EVENT',
RoleArn: 'iamroleArn',
EventPattern: JSON.stringify({
source: ['myapp.events'],
}),
State: 'ENABLED'
};
cwevents.putRule(params, function(err, rule) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", rule.RuleArn);
var params = {
Rule: 'DEMO_EVENT',
Targets: [
{
Arn: 'lambda function arn',
Id: "default"
}
]
};
cwevents.putTargets(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
var params = {
Entries: [
{
Detail: JSON.stringify({
"meetingNo": "82565852285"
}),
DetailType: 'meeting',
Resources: [
rule.RuleArn
],
Source: 'myapp.events',
Time: new Date("2022-01-07T08:00:00Z"), // here I am sending the schedule time in utc
}
]
};
cwevents.putEvents(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Entries);
}
});
}
});
}
});