AWS

An Introduction to Available Triggers to Invoke a Lambda Function

AWS Lambda is an amazing cloud-based service that revolutionized the serverless world. It is actually a software as a service (SaaS) that can be easily and quickly set up and is helpful to reduce the overall budget of your cloud infrastructure. What you have to do is just design your code and run it using the lambda function.

Now, the point here is how you are supposed to execute your code in the function and the answer to this is that there is a long list of methods through which you can invoke or trigger your lambda functions. This includes many other AWS services which can be used to call the desired function when required.

In this article, you are going to see a brief explanation regarding the services and techniques which can be applied to invoke your lambda functions in Amazon.

Types of Invocations

Before we get further, let us discuss the following two major types of invocations that the lambda function can handle.

  • Synchronous Invocations
  • Asynchronous Invocations
  1. Synchronous Invocations
    In synchronous invocations, the service that invokes the lambda has to wait until the results are returned to it and then carry on the rest of the process. We can also say that the output of the lambda function is required by the function or service itself which invoked this lambda.
  2. Asynchronous Invocations

    Here, there is no wait for the lambda functions to provide the results back to the invoker. This is mainly used for notifications or to trigger some other independent events in AWS. The service that wants to invoke the lambda function just sends the trigger and that operation is queued in the lambda and will be executed on its turn.

Different Ways to Invoke Lambda

Here, you are going to see numerous ways to invoke the lambda functions. Knowing this is very helpful to you the next time you design your simple but cost-efficient AWS infrastructure.

Directly Invoke the Lambda Functions

In most cases, the lambda functions are designed to be triggered using other services, but you can invoke them directly using the AWS management console, AWS CLI and through the function URL.

Invoking Lambda from Management Console

When you create any lambda function in your AWS console, you can easily trigger it using the test run option in the console. The Test button is available under the code section of the lambda function.

You can create a custom event as well as use the console with your custom event pattern.

In this way, the lambda function can be triggered from the AWS console.

AWS CLI

AWS gives you the ability to use all its resources using the AWS command line interface. Any lambda function can also be invoked with this CLI. This can be very effective to test things during the development stages. Following the AWS CLI command can be used as a trigger to invoke the lambda function.

ubuntu@ubuntu:~$ aws lambda invoke \
  --function-name <Enter the Lambda Function Name> \
  --payload <Input value for Lambda Function> \
  --cli-binary-format < base64 | raw-in-base64-out > <Output File Name>

The function is successfully triggered and you can also observe this in the output.

Function URL

Function URL is actually an HTTP endpoint that you can configure for your lambda functions. This URL can be used to trigger the lambda functions and you can also share this URL with other users even outside your AWS account to invoke the lambda functions. Although you should be careful with function URLs, anyone with this link can trigger your lambda function countless times, and all the costs will be on your head.

A function URL can be configured while creating as well as after creating the lambda function. For this, simply go to the advanced settings in the configuration section and check the Enable function URL box.

In case you have not attached a function URL while creating the lambda function, you can do it later. For this, you have to go to the configuration tab, select the function URL, and click on Create function URL.

This way, the function URL is created and used to invoke the lambda function.

Invoke Lambda Functions Using AWS Services

Lots of AWS services can be configured as a trigger to invoke the lambda function. You just need to configure the AWS services as a trigger to invoke the lambda function. Here, we go through all these services with an explanation about how to use them as your lambda triggers.

API Gateway

This is an AWS service which is used widely to create and manage the APIs in your application model. APIs give a very flexible way of generating requests or calls from one software package to another package which we cannot expose directly and just want to keep behind the scenes.

To add any service as a trigger to your lambda function, simply go to the lambda function and click on add trigger.

Next, you can select the service that you want to attach as a trigger to your lambda function. For this section, we select the API gateway as trigger for the lambda function.

Next, configure the service as you want it to act in your application structure.

There are two types of APIs supported by the API gateway and these can be utilized to invoke a lambda function.

HTTP APIs: They are used to generate the HTTP endpoints which are routed to your lambda functions. HTTP APIs give less functionality and are less costly to use.

REST APIs: If you want more features in your API, you must go for a REST API. These APIs can invoke the lambda function and use the same HTTP methods, providing more flexibility and independence.

S3 Bucket

There are lots of use cases where you will see that S3 buckets are acting as a trigger to invoke the lambda function. You can configure an S3 bucket to trigger the lambda function for a specific S3 event.

For example, you want to collect the metadata of any file when it is uploaded to your bucket. For this, you develop a code and deploy it on the lambda function. For the Lambda trigger, select the S3 bucket. For the event type, select put object. So that, whenever a new file is added to the bucket, the lambda function is triggered and the metadata of the object is collected and stored in the destination location when you specify.

There can be many other scenarios where S3 can be used as a trigger to invoke the lambda function.

Load Balancer

Suppose your application is designed to run on lambda functions as lambda function is the most cost-effective solution for a simple cloud-based application. Now, to expose your application to the end users, you might want to attach a load balancer in front of it. For this section, select the load balancer which acts as trigger to invoke the lambda function. Remember that you can only set up the application load balancer for this task as other load balancers are not supported by lambda functions.

In order to add the application load balancer to the lambda function, you first need to create a target group and the lambda function is added to that target group. Now, the newly created target group can be added to the listeners of the application load balancer.

CloudFront

Amazon CloudFront is actually a CDN (Content Delivery Network) and is used to cache the application data at edge locations that are much nearer to the end users as compared to the actual application servers. Using CloudFront, you can really improve the response time to serve the static content to the end users across the globe.

The lambda functions can be triggered using the CloudFront service. For this, you need to deploy your lambda function on edge locations across the globe known as lambda@edge.

You can set the CloudFront as trigger to send the requests to the lambda@egde through CloudFront to improve the response time. As the lambda@edge is deployed on all the edge locations across the globe, the end users face minimum response time by accessing the lambda deployed nearest edge location.

To configure this, simply go to add trigger and choose the CloudFront service. There, you will see the deploy to lambda@edge option.

Now, you just need to complete the configuration steps and get it started.

CloudWatch Logs

Whenever you think about monitoring in the AWS cloud, the first thing that comes to mind is CLoudWatch as this is a very vast monitoring service that can be configured for different services in a very helpful way.

CloudWatch logs, as the name defines, is a logging service that can be used to store all kinds of logs. You can create different log groups for different services to keep the logs separate. These logs can be used to trigger your lambda function based on the events they are receiving, irrespective of the service or procedure that produces these events.

You can either configure the trigger from the lambda function console or directly from the CloudWatch logs. To do this from the CloudWatch console, simply go to the CloudWatch service and open the log groups. Here, you have to create a lambda subscription filter.

Next, choose the lambda function that you want and you are good to go.

Now, whenever CloudWatch gets that log stream, it acts as a trigger to invoke the lambda function.

EventBridge

Amazon EventBridge (previously known as CloudWatch Events) is an AWS service that allows you to create the event rules to trigger a specific AWS service on a specific event that happens in an AWS account.

There is a wide variety of rules that you can set for AWS services (like EC2 instance creation or RDS database events) as well as third-party services (such as GitHub push event). These rules can be further linked with other services such as lambda functions in such a way that whenever this rule is satisfied, it invokes the lambda function.

If you have an EventBridge rule already set, you can easily add this rule as a trigger to your lambda function. Select the EventBridge as your trigger and simply provide the name of the rule.

An existing rule is added here as a trigger, but you could also create a rule at this point.

DynamoDB

You might have known that DynamoDB is just a NoSQL database and it appears as a fully separate service in AWS. This is a fully configured serverless database and you can just directly start creating tables in it. These DynamoDB tables can be configured to act as triggers to invoke the lambda functions.

The data from the DynamoDB can be loaded to lambda as input in the form of batches and it gets processed using the code which is deployed in lambda.

Kinesis

If you want to collect and analyze the real-time data at a high rate, you can benefit from AWS Kinesis. Suppose you want to process the data collected by the Kinesis data streams using the lambda functions. You simply need to trigger your lambda function each time the data is recorded by the Kinesis.

You are just done configuring your Kinesis data stream to invoke the lambda function.

SNS

It is simply a notification service that is commonly used to send notifications from one AWS service to the other because sometimes there is no way to configure the notifications directly from one service to the other. The lambda functions can be triggered using this service.

Create an SNS topic first, then use it to invoke your lambda function.

You have to select the name of your SNS topic. There are no other configurations or settings.

Conclusion

Amazon Lambda is really a breakthrough in cloud architecture. Application development and deployment have never been so easy and straightforward before it. It allows you to simply create your code in any common framework and upload your code to lambda and it gets executed. There is a long list of other services which can be coupled with AWS lambda and act as a trigger to invoke your lambda function only when it is required. There is no continuous running cost of the server, but you will be charged depending on the number of triggers and time of code execution.

About the author

Zain Abideen

A DevOps Engineer with expertise in provisioning and managing servers on AWS and Software delivery lifecycle (SDLC) automation. I'm from Gujranwala, Pakistan and currently working as a DevOps engineer.