Creating a Scheduled Lambda Function on AWS: A Step-by-Step Guide
Tags: aws, lambda, functional, awslambda
Published: 2023-09-21 11:39:53

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7jq7i77mek2mdnmw9ln.PNG)


2. Click the "Create function" button.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2vkj5wbnk626gts5n5d.PNG)

3. Select "Author from scratch."

4. 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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wf3gbbz7ro4zedptk1o6.PNG)

5. 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.

6. Click the "Create function" button at the bottom right.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f09bcgiyu7zlfjeqkxx7.PNG)

### 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bviotz75jpfjyeo0u29d.PNG)

4. In the "Rule" dropdown, click "Create a new rule."
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bviotz75jpfjyeo0u29d.PNG)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvnnkjtyfldp40na9236.PNG)

### 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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrwj7o7bbpw5q5ntcqeh.PNG)
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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1oe1915ohoowvswg9h0.PNG)
2. In the left sidebar, under "Logs," click on "Log groups."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqzilthy7zbjzy7welhw.PNG)
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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tq44i4ova5lc6m77dra8.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41lc4fpyvhl5n2c6n6rb.PNG)
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](https://www.linkedin.com/in/abigealafolabi210990/)
[Reddit](https://www.reddit.com/user/arbythecoder/)
[Twitter](https://twitter.com/arbysbodyorg__)
[Medium](https://medium.com/@houseofarby)
[Hashnode](https://hashnode.com/@ArbytheCoder)