Understanding AWS Schedule Lambda: 2 Critical Aspects

on AWS, AWS Lambda, Lambda, Lambda Function • April 14th, 2022 • Write for Hevo

aws schedule lambda: FI

A Serverless Architecture is a method of developing and deploying applications and services without the need to maintain infrastructure. One of the essential solutions for executing application code while creating Serverless Applications is AWS Lambda.

AWS Lambda is a Serverless, Event-Driven Compute Service offered by Amazon as part of Amazon Web Services. It is a computing service that runs code in response to events and maintains the computing resources required by that code automatically.

This article talks about AWS Schedule Lambda in-depth. In addition to that, it describes AWS Lambda and its key Features. 

Table Of Contents

What is AWS Lambda?

AWS Lambda is a serverless compute service that is triggered by events that allow you to run code for virtually any application or backend service without having to provision or manage servers. You can call Lambda from more than 200 AWS services and SaaS apps, and you only pay for what you use.

The term “serverless” computing refers to the fact that you don’t need to run these functions on your servers. AWS Lambda is a fully managed service that handles all of your infrastructure requirements.

AWS Lambda users write functions, which are self-contained applications written in one of the supported languages and runtimes, and upload them to AWS Lambda, which then executes them in a fast and flexible manner. 

Lambda functions can be used to do anything from serving web pages to processing data streams to calling APIs and integrating with other AWS services.

It also integrates with a variety of other AWS services, including API Gateway, DynamoDB, and RDS, and forms the foundation for AWS Serverless solutions. Lambda is compatible with a wide range of popular languages and runtimes, making it a good fit for Serverless developers.

Key Features of AWS Lambda

  • Concurrency and Scaling Controls: Concurrency and scaling controls, such as concurrency limits and provisioned concurrency, allow you to fine-tune the scalability and reactivity of your production applications.
  • Functions Defined as Container Images: To develop, test, and deploy your Lambda functions, it lets you use your favorite container image tooling, processes, and dependencies.
  • Code Signing: Code signing for Lambda offers trust and integrity controls, allowing you to ensure that only unmodified code published by authorized developers is deployed in your Lambda services.
  • Lambda Extensions: Lambda extensions may be used to enhance your Lambda functions. You can use extensions, for example, to make it easier to combine Lambda with your chosen tools for monitoring, observability, security, and governance.
  • Blueprints of Function: A function blueprint contains sample code that demonstrates how to utilize Lambda in conjunction with other AWS services or third-party applications. Sample code and function setup settings for the Node.js and Python run-times are included in the blueprints.
  • Database Accessibility: A database proxy controls a pool of database connections and transmits function queries. This allows a function to attain large levels of concurrency without draining database connections.
  • Access to File Systems: A function can be configured to mount an Amazon Elastic File System (Amazon EFS) file system to a local directory. With Amazon EFS, your function code may securely and concurrently access and alter shared resources

Aggregate Data in Minutes Using Hevo’s No-Code Data Pipeline

Hevo Data, a Fully-managed Data Pipeline solution, can help you automate, simplify & enrich your aggregation process in a few clicks. With Hevo’s out-of-the-box connectors and blazing-fast Data Pipelines, you can extract & aggregate data from 100+ Data Sources(including 40+ Free Sources) like Amazon S3 straight into your Data Warehouse, Database, or any destination. To further streamline and prepare your data for analysis, you can process and enrich Raw Granular Data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!”

GET STARTED WITH HEVO FOR FREE[/hevoButton]

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

Understanding AWS Schedule Lambda

An AWS Lambda function can be scheduled using a rule. AWS Schedule Lambda can be created by 2 methods:

AWS Schedule Lambda: Using EventBridge

AWS Schedule Lamba can be implemented using EventBridge. Second-level precision in schedule expressions is not available in EventBridge for schedules. One minute is the highest resolution achievable with a CRON expression. Because EventBridge and the target services are distributed, there may be a few seconds between when the scheduled rule is triggered and when the target service runs the target resource. 

Step 1: Create a Lambda Function

To keep track of the AWS Schedule lambda events, create a Lambda function. 

To create a Lambda function, follow these steps:

  • Step 1: Go to https://console.aws.amazon.com/lambda/ to access the AWS Lambda console.
  • Step 2: Select the Create option.
  • Step 3: Choose an author from the ground up.
  • Step 4: Give the Lambda Function a name and a description. Name the function LogScheduledEvent, for example.
  • Step 5: Choose the Create Function and leave the rest of the options at their defaults.
  • Step 6: Double-click index.js on the function page’s Code tab.
  • Step 7: Substitute the following code for the existing code.
'use strict';

exports.handler = (event, context, callback) => {
    console.log('LogScheduledEvent');
    console.log('Received event:', JSON.stringify(event, null, 2));
    callback(null, 'Finished');
};
  • Step 8: Select Deploy.

Step 2: Create a Rule

Make a rule to run the Lambda function you made in step 1 regularly.

To create the rule, you can use either the console or the AWS CLI. To use the AWS CLI, you must first grant permission to the rule to call your Lambda function. Then, as a target, you can add the Lambda function to the rule.

To create a rule (console) follow the steps:

  • Step 1: Go to https://console.aws.amazon.com/events/ to access the Amazon EventBridge console.
  • Step 2: Choose Rules from the Navigation pane.
  • Step 3: Make a rule by selecting Create Rule.
  • Step 4: Give the Rule a name and a description. A rule in the same Region and on the same event bus cannot have the same name as another rule.
  • Step 5: Select the event bus that you want to link to this rule under Event Bus. Select AWS Default Event Bus if you want this rule to match events from your account. When an AWS service in your account emits an event, it is always sent to the default event bus in your account.
  • Step 6: Choose Schedule as the rule type.
  • Step 7: Select Next.
  • Step 8: Choose an AWS Schedule Lambda pattern that runs at a regular interval, such as every 10 minutes. and select Minutes from the drop-down list, then enter 5.
  • Step 9: Select Next.
  • Step 10: Choose AWS Service for target types.
  • Step 11: Choose Lambda Function from the drop-down list for Select a Target.
  • Step 12: Select the Lambda Function you created in step 1: Create a Lambda Function section for Function. Select LogScheduledEvent in this case.
  • Step 13: Select Next.
  • Step 14: Select Next.
  • Step 15: Review the rule’s details before selecting Create Rule.

To create a rule (AWS CLI) follow the steps given below:

  • Step 1: Use the put-rule command to create a rule that runs on a set.
aws events put-rule 
--name my-scheduled-rule 
--schedule-expression 'rate(5 minutes)'

When this rule is activated, it generates an event and sends it to the specified targets. An example of an event is shown below.

{
    "version": "0",
    "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
    "detail-type": "Scheduled Event",
    "source": "aws.events",
    "account": "123456789012",
    "time": "2015-10-08T16:53:06Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
    ],
    "detail": {}
}
  • Step 2: Use the add-permission command to grant the rule’s execution permission to the EventBridge Service Principal (events.amazonaws.com).
aws lambda add-permission 
--function-name LogScheduledEvent 
--statement-id my-scheduled-event 
--action 'lambda:InvokeFunction' 
--principal events.amazonaws.com 
--source-arn arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule
  • Step 3: Use the put-targetscommand to add the Lambda function that you created in step 1 to the rule.
aws events put-targets --rule my-scheduled-rule --targets file://targets.json
  • Step 4: Make a file named targets.json with the contents listed below.
[
  {
    "Id": "1", 
    "Arn": "arn:aws:lambda:us-east-1:123456789012:function:LogScheduledEvent"
  }
]

Step 3: Verify the Rule

After waiting at least five minutes after completing step 2, you can check to see if your Lambda function was called.

Follow the steps below to see the Output of your Lambda Function:

  • Step 1: At https://console.aws.amazon.com/cloudwatch/, access the CloudWatch console.
  • Step 2: Select Logs from the Navigation bar.
  • Step 3: (/aws/lambda/function-name) Choose the name of the Log Group for your Lambda function.
  • Step 4: To view the data returned by the function for the instance you launched, select the Name of the Log Stream.

Step 4: Confirm Success

If the AWS Schedule Lambda event appears in the CloudWatch logs, the AWS Schedule Lambda was successfully implemented. If the event isn’t in your CloudWatch logs, start troubleshooting by checking that the rule was created successfully. If the rule appears to be correct, check the code of your Lambda function.

Step 5: Clean up your Resources

Unless you want to keep the resources you created while scheduling, you can now delete them. You can avoid unnecessary charges to your AWS account by deleting AWS resources that you are no longer using.

To delete the EventBridge rule(s) follow the steps below:

  • Step 1: Open the EventBridge console’s Rules page.
  • Step 2: Choose the Rule(s) you created.
  • Step 3: Select Delete.
  • Step 4: Select Delete.

To get rid of the Lambda function, follow these steps:

  • Step 1: Open the Lambda console’s Functions page.
  • Step 2: Choose the function(s) you’ve written.
  • Step 3: Select Actions, then Delete.
  • Step 4: Select Delete

AWS Schedule Lambda: Using CloudWatch

AWS Schedule Lambda can be configured using CloudWatch. Second-level precision in AWS Schedule Lambda expressions is not available in CloudWatch Events. A minute is the finest resolution possible with a CRON expression. Due to the distributed nature of CloudWatch Events and target services, there may be a delay of several seconds between the time the scheduled rule is triggered and the time the target service honors the target resource execution. Within that minute, but not on the exact 0th second, your scheduled rule is triggered.

There are 3 steps involved in creating an AWS Schedule Lambda using CloudWatch:

Step 1: Create an AWS Lambda Function

To keep track of the AWS Schedule Lambda events, create a Lambda function. When you’re creating your rule, be sure to include this function.

To create a Lambda function follow the steps below:

  • Step 1: Go to https://console.aws.amazon.com/lambda/ to access the AWS Lambda console.
  • Step 2: You’ll see a welcome page if you’re new to Lambda. Select Get Started Now from the drop-down menu. Choose to Create a Lambda Function if you don’t already have one.
  • Step 3: Type hello for the filter on the Select Blueprint Page and select the hello-world blueprint.
  • Step 4: Choose Next on the Configure Triggers page.
  • Step 5: Do the following on the Configure Function page:
    • Give the Lambda function a name and a brief description. Name the function “LogScheduledEvent” for example.
    • Edit the Lambda function sample code. Consider the following example: 
'use strict';

exports.handler = (event, context, callback) => {
    console.log('LogScheduledEvent');
    console.log('Received event:', JSON.stringify(event, null, 2));
    callback(null, 'Finished');
};
  • Choose a role for yourself. Select a pre-existing role. Select your basic execution role for the Existing role. Create a new basic execution role if necessary.
  • Select Next.
  • Step 6: Select Create function from the Review page.

Step 2: Create a Rule

Make a rule to AWS Schedule Lambda for the execution of your Lambda function.

To create a rule using the console follow these steps:

  • Step 1: Go to https://console.aws.amazon.com/cloudwatch/ to access the CloudWatch console.
  • Step 2: Choose Events, and Create a Rule from the Navigation pane.
  • Step 3: Do the following for the Event Source:
    • Select a Schedule.
    • Set the Schedule Interval and choose a Fixed-Rate (for example, 5 minutes).
  • Step 4: Select Add target, Lambda function from the Targets menu.
  • Step 5: Select the Lambda Function you created for Function.
  • Step 6: Select Details to configure.
  • Step 7: Type a name and description for the rule in the Rule Definition box.
  • Step 8: Make a rule by selecting Create rule.

You can also use the AWS CLI to create the rule. To begin, you must allow the rule to call your Lambda function. The Lambda function can then be added as a Target to the rule.

To create a rule using the AWS CLI follow the steps given below:

  • Step 1: To create a rule that executes on an AWS Schedule Lambda, 
use the put-rule command:aws events put-rule 
--name my-scheduled-rule 
--schedule-expression 'rate(5 minutes)'
  • Step 2: When this rule is triggered, it creates an event that is used as input by the rule’s targets. An example of an event is as follows:
{
    "version": "0",
    "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
    "detail-type": "Scheduled Event",
    "source": "aws.events",
    "account": "123456789012",
    "time": "2015-10-08T16:53:06Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
    ],
    "detail": {}
}
  • Step 3: To make your Lambda Function run every five minutes, use the put-targets command to add it to this rule:
aws events put-targets --rule my-scheduled-rule --targets file://targets.json

Create a file called targets.json and fill it with the following information:

[
  {
    "Id": "1", 
    "Arn": "arn:aws:lambda:us-east-1:123456789012:function:LogScheduledEvent"
  }
]

Step 3: Verify the Rule

You can check that your AWS Schedule Lambda Function was invoked at least five minutes after completing Step 2.

Follow these steps to test your rule:

  • Step 1: Go to https://console.aws.amazon.com/cloudwatch/ to access the CloudWatch console.
  • Step 2: Choose Events, and Rules, select the name of the rule you created and then Show Metrics for the rule in the Navigation pane.
  • Step 3: Follow these steps to see the Output of your Lambda Function:
    • Select Logs from the Navigation pane.
    • Select the name of your Lambda function’s Log Group (/aws/lambda/function-name).
    • To view the data provided by the function for the instance that you launched, select the name of the Log Stream.
  • Step 4: (Optional) Once you’re done, you can turn off the rule.
    • Go to https://console.aws.amazon.com/cloudwatch/ to access the CloudWatch console.
    • Select Events, and Rules from the Navigation pane.
    • Choose Actions, Disable for the rule you want to disable.
    • Choose Disable when prompted for confirmation.

What Makes Hevo’s ETL Process Best-In-Class

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
SIGN UP HERE FOR A 14-DAY FREE TRIAL

Limitations of AWS Lambda

  • Cold Start Time: There may be some latency between the event and when the function runs when it is started in response to an event. Latency can be as high as 5-10 seconds if your function hasn’t been used in the last 15 minutes, making Lambda unsuitable for latency-sensitive applications.
  • Function Limits: There are a few constraints on Lambda functions:
    • Execution Time/Run Time: After 15 minutes of running time, a Lambda function will time out. There is no way to get around this restriction. If your function takes more than 15 minutes to run, AWS Lambda might not be the best option for you.
    • Memory Available to Function: The amount of RAM available to Lambda functions ranges from 128MB to 3,008MB, with a 64MB step between them.
    • Code Package Size: The size of the zipped Lambda code package should not exceed 50MB, and the unzipped version should not exceed 250MB.
    • Concurrency:  All AWS Lambda functions in a single AWS account are limited to 1,000 concurrent executions by default. Any Lambda executions that exceed your concurrency limit will be throttled and forced to wait until other functions have completed their execution.
    • Payload Size: The maximum payload size that API Gateway can handle when triggering Lambda functions in response to HTTP requests (i.e. when building a web application) is 10MB.
  • Not Always Cost-Effective: You only pay for the function runtime you use on AWS Lambda (plus any associated charges like network traffic). For certain usage patterns, such as cron jobs or other on-demand tasks, this can result in significant cost savings. However, as your application’s load grows, the cost of AWS Lambda rises in lockstep, and it may end up being higher than the cost of similar infrastructure on AWS EC2 or other cloud providers.
  • Limited Number of Supported Runtimes: While AWS Lambda allows for the creation of custom runtimes, it can be time-consuming. If your programming language isn’t supported by Lambda, you might be better off using AWS EC2 or another cloud provider.

Conclusion

This article explains how to create an AWS Schedule Lambda using EventBridge and CloudWatch. It also gives a brief introduction to AWS Lambda before diving into the key aspects of AWS Schedule Lambda.

visit our website to explore hevo

Hevo Data, a No-code Data Pipeline provides you with a consistent and reliable solution to manage data transfer between a variety of sources and a wide variety of Desired Destinations, with a few clicks. Hevo Data with its strong integration with 100+ sources (including 40+ free sources) allows you to not only export data from your desired data sources & load it to the destination of your choice, but also transform & enrich your data to make it analysis-ready so that you can focus on your key business needs and perform insightful analysis.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

No-code Data Pipeline For Your Data Warehouse