Introduction

Sometimes, we want a script to run automatically, like sending a reminder, checking something, or logging a message every few minutes. In this article, we’ll learn how to schedule a small Python script on AWS Lambda using CloudWatch Events step by step. No complex setup, just simple clicks and code.

Step 1. Create a Simple Python Script.

Let’s first create a small Python function that just prints a message.

def lambda_handler(event, context):
    print("Hello! This Lambda ran on a schedule.")

This function will be triggered by AWS on a schedule we set. For now, it just prints a message.

Step 2. Create a Lambda Function.

Now, scroll down to the Code Source section and replace the default code with our script.

def lambda_handler(event, context):
    print("Hello! This Lambda ran on a schedule.")

Click Deploy to save the changes.

Deploy

Step 3. Test Your Lambda Function.

Before scheduling it, let’s make sure it works.

You should see the output in the console area saying.

Hello! This Lambda ran on a schedule.

Log Output

Step 4. Schedule It with CloudWatch Events.

Now, it’s time to make this function run automatically.

That’s it. Your Python script is now scheduled to run automatically every few minutes.

Step 5. Check If It’s Working.

After you’ve created the schedule, AWS will automatically trigger your Lambda function based on the time you set (like every 5 minutes).

To check if it actually ran, follow these steps.

Go to CloudWatch

In the AWS Console, type CloudWatch in the search bar and click on it.

Click on Logs

Find Your Lambda's Log Group

Open the Most Recent Log Stream

Check the Log Messages

Hello! This Lambda ran on a schedule.

Output

That means your Lambda function ran successfully at the scheduled time.

Conclusion

That’s it. You just created a Python script, set it up on AWS Lambda, and scheduled it to run on its own. This is a great way to automate small tasks without keeping your computer on. And the best part it’s all free if you’re using the AWS Free Tier.