NetworkBytes
NetworkBytes

Reputation: 51

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

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?

Upvotes: 5

Views: 5459

Answers (2)

Brice Miramont
Brice Miramont

Reputation: 160

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.

Upvotes: 4

Ethan Nguyen
Ethan Nguyen

Reputation: 114

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

Upvotes: -1

Related Questions