How do I run a scheduled AWS Lambda function, where scheduled time is a parameter?

Viewed 2461

I am wondering if it's possible to run a cron job using AWS Lambda with input parameters.

Example: I call my API endpoint: api.example.com/LambdaFunction5?timestamp=1571299919&someOtherVariable=NetworkBytes

As you can see, it's a get request to my API will which takes two parameters, an Epoch timestamp (1 day from now) and another parameter (can be anything). This API call will then make a con job that will be executed on the given timestamp using the other parameter as a variable in the lambda function.

How would I achieve this with AWS Lambda? I know that AWS allows me to schedule lambdas for specific times:

https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html

But the problem is that I don't want to create a new lambda function every time I want a cron job.

Is it a way to do this so when I call my API endpoint, it will create a cron job based on the time I give and run only once and "delete" itself after that job is run, so I don't end up with a million different functions or CloudWatch rules?

2 Answers

you may define custom json attributes to be sent with the cloudwatch event:

  • Go to Amazon EventBridge > Events > Rules
  • clic on your rule
  • click "Edit" button on the top right corner
  • scroll down to "Select targets"
  • clic on "Configure Inputs“
  • select "Constant (JSON text)" radio button
  • Add in the editable field the json data with your parameters.

You may encounter a bug in the console that forbids you to edit via the console. You may need to use the CLI (I don't have the syntax for that yet, sorry). Bug is here: https://github.com/concurrencylabs/aws-pricing-tools/issues/8

bug is closed but still present nonetheless.

Related