Event sources

The following are the ways in which you can invoke a Lambda function:

  • Via the AWS Lambda invoke API
  • By using an SDK
  • Event sources

We've already touched on the fact that various services in the AWS ecosystem can natively trigger a Lambda function to be invoked. A full list of services that support this can be found in the public developer guide for AWS Lambda, which can be found in the Further reading section. Event sources can also make use of the various invocation types; for example, Amazon SNS will invoke a function asynchronously, as well as Amazon Lex synchronously. Amazon SQS is message-based, so Lambda can read events from the queue.

A useful example to mention is the ability to trigger a Lambda function from a CloudWatch Event. One feature of CloudWatch Events is that you can create a rule to self-trigger on an automated schedule using cron expressions. This means we can run Lambda functions on a regularly scheduled basis – useful! Here's what it looks like:

Time-based CloudWatch Event triggering a Lambda function

And here's the format for cron expressions:

cron(Minutes Hours Day-of-month Month Day-of-week Year)

We can translate this into a CLI command to create a CloudWatch Event rule. By doing this, we get the following response:

aws events put-rule --schedule-expression "cron(0 12 * * ? *)" --name MyLambdaSchedule

Each service has a particular message event structure, and I recommend that you keep the documentation at hand when developing with an event source.

In the next section, we will describe the components that make up the execution of a function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79