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?
It helps automate repetitive tasks. For example, you can use it to send daily reports, update data, or perform backups.
You only pay for the time your code runs, making it cost-effective for tasks that don't need 24/7 servers.
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
- Log in to your AWS Management Console using your AWS account credentials.
Step 2: Create a New Lambda Function
- Navigate to the AWS Lambda service by clicking on "Services" in the top left corner and selecting "Lambda" under the "Compute" section.
- Click the "Create function" button.
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.
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
In your Lambda function's configuration page, scroll down to the "Function overview" section.
Under "Add triggers," click on "Add trigger."
Select "EventBridge (CloudWatch Events)" as the trigger type.
-
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
In your Lambda function configuration, scroll down to the "Function code" section.
Replace the existing code with code to print the current time. Here's a Python example:
- 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:
- Go to the CloudWatch service in the AWS Console.
- In the left sidebar, under "Logs," click on "Log groups."
- Find and click on the log group associated with your Lambda function (usually named
/aws/lambda/YourFunctionName
). - 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
Top comments (0)