1
votes

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
If I had to do this, I probably won't use AWSLambda since it seems clear there is no clean solution to achieve this. That said, if you really want to do it, maybe that you can use the AWS SDK to change the schedule at 2 key moments : - after your cron runs, by setting the next cron date to run (or a very far date to ensure it won't run before another schedule is added) - on the POST call if : there is no schedule or the current schedule is later than the actual added schedule You will have to store schedules somewhere to do so.nfroidure

2 Answers

0
votes

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.

-1
votes

You can integrate Lambda with API Gateway proxy integration to achieve this. API Gateway will invoke your lambda on your behalf & pass URL parameter as part of event object during Lambda execution. Please refer to

https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-create-api-as-simple-proxy

Detailed tutorial here https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html