官术网_书友最值得收藏!

Creating Lambda triggers

The Lambda function can be configured in response to events. AWS provides a list of triggers that support lots of events. These triggers belong to their associated AWS services.

You can add a trigger to your Lambda function from the triggers section.

I am going to slightly modify the hello world Lambda function. Here, we are printing the request ID, which is received in the context object as an aws_request_id attribute. It also prints the timestamp:

Now, we are going to add a trigger to our Lambda function that will execute our Lambda function every minute.

The following screenshot shows the Add trigger flow, where you can easily configure any trigger from the left-hand panel with your Lambda function:

We are going to configure the CloudWatch Events trigger. CloudWatch Events deliver near real-time system events that describe the changes in AWS resources.

You can set up simple event rules with operational events in AWS resources as they occur, and you can also schedule automated events that self-trigger based on cron or the rate expression.

The cron and rate expression are two different methods to define a schedule expression. The cron expressions have six required fields, such as cron (fields), and the rate expressions have two required fields, such as rate (value unit). These methods help us to define a schedule expression. You can find detailed information at http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html.

Here, we are going to schedule a rate expression to execute our hello world Lambda function every minute. We need to select CloudWatch Events from the triggers dropdown.

To create the CloudWatch event rule, we are going to create a new rule. We need to set up the rule with some required information, such as the rule name, which is a unique identifier. So, we are going to name the rule as hello-world-every-minute and the rule type as either the event pattern or schedule expression. In our case, it would be a schedule expression as the rate (1 minute), as shown in the preceding screenshot.

Once we set the trigger and enable it, the scheduled event would get triggered as per the schedule expression. Let's see our hello world Lambda logs after five minutes.

To view the logs related to any services, you need to do the following:

  1.  Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/
  2. In the navigation pane, choose Logs
  3. Select the log group related to the HelloWorld Lambda function

The following screenshot describes the CloudWatch log access:

By selecting the HelloWorld Lambda function log groups, you see the logging activity related to our HelloWorld Lambda function. The following screenshot shows the logs of the HelloWorld function:

Here, you can see that our hello world Lambda function is executed exactly every minute since the time we have enabled the trigger.  

Now, let's move ahead to create a serverless RESTful API.

主站蜘蛛池模板: 桃江县| 北海市| 清水县| 松潘县| 旬阳县| 民勤县| 阳山县| 望谟县| 新竹县| 视频| 牟定县| 富川| 西充县| 左权县| 教育| 泸水县| 湖南省| 万山特区| 华阴市| 潢川县| 穆棱市| 鹤庆县| 晋江市| 锦州市| 贡觉县| 河北省| 麻城市| 清徐县| 来宾市| 西乌珠穆沁旗| 浙江省| 炉霍县| 富蕴县| 保靖县| 吴旗县| 大安市| 谷城县| 日喀则市| 江都市| 文登市| 五大连池市|