Skip to content

How do I set up scheduled tasks in AWS

0

I need to get information from URL (for example temperature in Springfield or the value of my stock portfolio) and send it to phone or email. I can do it literally it in one line:

 aws sns publish --message $(curl https://api.example.com) --phone-number "digits"

for some reason re:post doesn't allow actual digits in phone number field. Weird!

Works fine. Now I want to schedule this to happen once a day, at 3AM (or once an hour - doesn't matter). I thought it would be a simple exercise. I am looking at (1) EventBridge Scheduler, but it only connects to AWS Services; not API. Next, at (2) EventBridge Rules. I set up a connection and API destination. But Rules seem to want to POST payload to the target, rather than GET information from the source. (3) I am looking at EventBridge Bus with API Gateway / REST API as a source. But it wants me to define or discover schema, when all I want to do is to passthrough whatever I get.

Of course, I can just use cron or Windows Task Scheduler. But I was trying to go serverless. Does it have to be that difficult, or I am missing something obvious?

2 Answers
2

Hi,

EventBridge scheduler is definitely the way to go.

Please see this article for all details: https://levelup.gitconnected.com/using-aws-eventbridge-scheduler-to-build-a-serverless-reminder-application-ba3086cf8e

The corresponding Github repo: https://github.com/ziedbentahar/aws-eventbridge-scheduler-sample?tab=readme-ov-file

Note: this article goes a bit beyond what you want to do: it gives you a programmatic way to create such programmed notifications.

Best,

Didier

EXPERT
answered 10 months ago
EXPERT
reviewed 10 months ago
  • Thank you. All this to replace a one-liner seems a little excessive 🤪

1

You're right that scheduling tasks in AWS can sometimes seem more complex than expected, especially for seemingly simple operations. However, there are a few ways to accomplish what you're trying to do using AWS services. Let's explore a solution using EventBridge Scheduler, which is the recommended approach for scheduling tasks in AWS.

To set up your scheduled task:

  1. Create a Lambda function: First, create a Lambda function that performs your desired task. This function will make the HTTP GET request to your API and then send the result via SNS. You can use libraries like 'axios' for HTTP requests and the AWS SDK for sending SNS messages.

  2. Set up EventBridge Scheduler: Use EventBridge Scheduler to invoke your Lambda function on a schedule. Here's how:

    a. Open the Amazon EventBridge Scheduler console. b. Create a new schedule, providing a name and description. c. Choose the schedule type (recurring) and set up your desired schedule (e.g., daily at 3 AM) using a cron expression. d. Select AWS Lambda as the target and choose your function. e. Configure any additional settings like retry policy or dead-letter queue if needed. f. Review and create the schedule.

This approach allows you to run your task serverlessly on a schedule. The Lambda function acts as an intermediary, making the API call and then using AWS services (SNS in this case) to send the message.

While it may seem more complex than a simple cron job, this serverless approach offers benefits like improved scalability, easier management of AWS resources, and integration with other AWS services.

If you prefer a simpler setup and don't mind managing a server, you could indeed use a small EC2 instance with a cron job. However, the serverless approach with Lambda and EventBridge Scheduler is generally more cost-effective and easier to manage in the long run for such periodic tasks.
Sources
Invoke a Lambda function on a schedule - AWS Lambda
Using Amazon EventBridge Scheduler with Amazon SNS - Amazon Simple Notification Service

answered 10 months ago
EXPERT
reviewed 10 months ago
EXPERT
reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.