Creating a Scheduled Lambda Function on AWS: A Step-by-Step Guide

Creating a Scheduled Lambda Function on AWS: A Step-by-Step Guide

An AWS Lambda scheduled function is like a digital alarm clock. It runs a small piece of code (a function) automatically at a specific time or on a schedule. Think of it as an automated reminder on your phone that tells you to check your email every morning at 8 AM.

How Does It Help?

  1. It helps automate repetitive tasks. For example, you can use it to send daily reports, update data, or perform backups.

  2. You only pay for the time your code runs, making it cost-effective for tasks that don’t need 24/7 servers.

  3. It ensures tasks happen exactly when needed, reducing the chance of human error.

Prerequisites

Before you start, make sure you have the following:

  • An AWS account (If you don’t have one, you can sign up for a free account at https://aws.amazon.com/)

  • Basic knowledge of AWS Lambda (If you’re new to Lambda, you can start with the “Serverless Hello World with AWS Lambda” tutorial)

Step 1: Log in to AWS Console

  1. Log in to your AWS Management Console using your AWS account credentials.

Step 2: Create a New Lambda Function

  1. Navigate to the AWS Lambda service by clicking on “Services” in the top left corner and selecting “Lambda” under the “Compute” section.

2. Click the “Create function” button.

3. Select “Author from scratch.”

Fill in the basic function information:

. Function name: Give your Lambda function a name (e.g., “ScheduledTimeFunction”).

. Runtime: Choose a runtime (e.g., Python 3.8 or Node.js 12.x), I used python 3

4. In the “Permissions” section, create a new or choose an existing execution role that allows your Lambda function to log to Cloud Watch Logs. You can choose an existing role or create a new one with basic Lambda execution permissions.

Click the “Create function” button at the bottom right.

Step 3: Configure a Cloud Watch Events Rule

  1. In your Lambda function’s configuration page, scroll down to the “Function overview” section.

2. Under “Add triggers,” click on “Add trigger.”

3. Select “EventBridge (CloudWatch Events)” as the trigger type.

4. In the “Rule” dropdown, click “Create a new rule.”

5. In the “Create Rule” page:

  • Rule name: Give your rule a name (e.g., “MinuteIntervalRule”).

  • Rule type: Choose “Schedule” and configure it to run every minute. You can use a cron expression like cron(0/1 * * * ? *) to run every minute.

  • Click the “Create rule” button.

Step 4: Configure the Lambda Function Code

  1. In your Lambda function configuration, scroll down to the “Function code” section.

2. Replace the existing code with code to print the current time. Here’s a Python example:

3. Click the “Deploy” button to save your code changes.

Step 5: Monitor the Execution

Your Lambda function will now execute every minute according to the schedule you defined. You can monitor its executions and view the printed time in the CloudWatch Logs.

To view the logs:

  1. Go to the CloudWatch service in the AWS Console.

2. In the left sidebar, under “Logs,” click on “Log groups.”

3. Find and click on the log group associated with your Lambda function (usually named /aws/lambda/YourFunctionName).

4. You will see log streams for each execution, and you can click on them to view the printed output.

That’s it! You’ve successfully created a scheduled Lambda function that retrieves and prints the current time every minute.
You can drop your comments, and suggestions below and also connect with me :
LinkedIn
Reddit
Twitter
Medium
Hashnode