How to schedule a call API using node.js aws-sdk, cloudwatch and lambda function

Viewed 1578

I am working on a node.js back-end for an application where customers make requests for a certain service. Once they make a request, the payment amount get held on Stripe, then get captured once the service is delivered. But if the service was not delivered within a certain number of days, the request get canceled (on database) and the payment get released.

For that, I needed an external service which allows me to schedule a call (once) to an API (of mine) programmatically for each customer request.

This way I can schedule a call for each customer request to run on the final date of that request while holding its info. My API can then use that info to check the status of the request and make the required changes to the database as well as payment release in case the service was not delivered.

I Knew that I can do this by scheduling a rule (problematically via the put-rule command using the aws-sdk) that triggers a lambda function which execute against my API, but I need an example on how to do this Knowing that I'll be passing an API parameter (which is the request_id) from the cloudwatch event to the lambda function.

2 Answers

You can create a Table in Dynamodb to store the record of every payment that gets held on Stripe and set the TTL time limit on it.

You can subscribe to this table for all events (although you might need for INSERT and DELETE). So once the delete event happens you can check whether the service delivered or not.

If not you can trigger the lambda responsible to release the payment.

You can use CRON Job for this task which can schedule the Api call, So First of all what is CRON Job?

A cron job is a command used for scheduling tasks to be executed sometime in the future. This is normally used to schedule a job that is executed periodically – for example, to send out a notice every morning.

AWS Support CRON Job option with lambda, you can check out this example to create your task. https://www.freecodecamp.org/news/using-lambda-functions-as-cronjobs/

Have a nice day!

Related