AWS Certified Developer - Associate: Running Scheduled Lambda Functions

Ideal Solution for Running Scheduled Lambda Functions

Prev Question Next Question

Question

You have developed a Lambda function.

This function needs to run on a scheduled basis.

Which of the following can be done to accomplish this requirement in an ideal manner?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer - C.

The AWS Documentation mentions the following.

Option A is incorrect since there is no inbuilt scheduler in AWS Lambda.

Option B is incorrect since this would add more maintenance overhead.

Option D is incorrect since this service is an API monitoring service.

For more information on running Lambda functions on schedules, please refer to the below URL-

https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/RunLambdaSchedule.html
Tutorial: Schedule AWS Lambda Functions Using CloudWatch
Events

You can set up a rule to run an AWS Lambda function on a schedule. This tutorial shows how to use the
AWS Management Console or the AWS CLI to create the rule. If you would like to use the AWS CLI but
have not installed it, see the AWS Command Line Interface User Guide.

CloudWatch Events does not provide second-level precision in schedule expressions. The finest resolution
using a cron expression is a minute. Due to the distributed nature of the CloudWatch Events and the
target services, the delay between the time the scheduled rule is triggered and the time the target service
honors the execution of the target resource might be several seconds. Your scheduled rule is triggered
within that minute but not on the precise Oth second.

The correct answer is C. Use Cloudwatch events to schedule the Lambda function.

Explanation: AWS Lambda is a serverless compute service that allows developers to run their code without managing servers. Developers can create Lambda functions and trigger them using various events such as API Gateway, S3, and Cloudwatch events.

To run a Lambda function on a scheduled basis, we need to configure it to trigger at specific times. AWS provides various ways to schedule a Lambda function, such as:

A. Use the schedule service in AWS Lambda: AWS Lambda provides a built-in scheduler that allows developers to schedule a function to run at specific times. This option is ideal if the Lambda function needs to run at a fixed interval, for example, every 5 minutes, every hour, or every day. However, this option is limited to fixed schedules and cannot trigger a Lambda function based on more complex events.

B. Use an EC2 Instance to schedule the Lambda invocation: This option involves creating an EC2 instance and configuring it to run a script that invokes the Lambda function. This option is not ideal because it involves managing an EC2 instance and increases the infrastructure overhead.

C. Use Cloudwatch events to schedule the Lambda function: Cloudwatch events is a service that allows developers to create rules that match events and route them to targets such as Lambda functions. This option is ideal because Cloudwatch events can trigger Lambda functions based on complex event patterns, such as an S3 bucket upload or a Cloudtrail event.

D. Use Cloudtrail to schedule the Lambda function: Cloudtrail is a service that records API calls and events in AWS. This option is not ideal because Cloudtrail does not provide a mechanism to trigger Lambda functions directly. However, it can be used in conjunction with Cloudwatch events to trigger a Lambda function based on Cloudtrail events.

In summary, the ideal option to schedule a Lambda function is to use Cloudwatch events.