Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create ECS as a trigger to trigger function calculation

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to create ECS as a trigger to trigger function calculation, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Problem description

Although function computation does not support direct integration into ECS management events, function computation itself supports log service as a trigger. That is, you can configure the enhanced log in the logstore in the log service as a trigger to trigger the functions in the function computing service. At the same time, you can transfer related log information such as name and beginCursor/endCursor of project and logstore as event to the function computing service for secondary processing and processing.

This is equivalent to providing an idea, that is, we can send the creation of ECS or other related operations to the log service as a log, so that the relevant function calculation service can be triggered.

So what is this method? One feasible way is to operate the audit service. Operation audits can record user records at all API levels, including operations such as CreateInstance.

So the whole process becomes:

Activate operation audit service-> configure operation audit trail, deliver event to log service-> configure log service as a function to calculate trigger and pass log-> trigger function

Take a chestnut.

After activating the operation audit service, create a log trace

Then create an instance, and you can see that the operation audit records this behavior

At the same time, this behavior record was also found in the log service.

Next, we can configure a function calculation service. For the specific process, please refer to the last document in this article. Here we emphasize the configuration of triggers. What we should pay attention to here is the configuration of logstore in the figure. The above is the logstore that triggers the log, and the following is the lostore that writes the log, which should not be confused.

Then copy in a piece of code, the core of which is to get the specific log information that triggered the event, and then write it to the log library where the function is calculated.

#-*-coding: utf-8-*-import loggingimport jsonfrom aliyun.log import LogClientfrom time import timedef logClient (endpoint, creds): logger = logging.getLogger () logger.info ('creds info') logger.info (creds.access_key_id) logger.info (creds.access_key_secret) logger.info (creds.security_token) accessKeyId =' XXX' accessKey = 'XXX' client = LogClient (endpoint, accessKeyId, accessKey) return clientdef handler Context): logger = logging.getLogger () logger.info ('start deal SLS data') logger.info (event.decode (). Encode ()) info_arr = json.loads (event.decode ()) fetchdata (info_arr [' source'], context) return 'hello world'def fetchdata (event,context): logger = logging.getLogger () endpoint = event [' endpoint'] creds = context.credentials client = logClient (endpoint Creds) if client = = None: logger.info ("client creat failed") return False project = event ['projectName'] logstore = event [' logstoreName'] start_cursor = event ['beginCursor'] end_cursor = event [' endCursor'] loggroup_count = 10 shard_id = event ['shardId'] while True: res = client.pull_logs (project, logstore, shard_id, start_cursor, loggroup_count End_cursor) res.log_print () next_cursor = res.get_next_cursor () if next_cursor = = start_cursor: break start_cursor = next_cursor # log_data = res.get_loggroup_json_list () return True

After the above configuration is completed, the behavior of a console to create an ECS (and, of course, other behaviors that can be audited) can be used to trigger the function evaluated by the function.

Result

We release the instance we just created and see the log of the operation audit

Then we also see the corresponding log in the log library of the function calculation, which is passed to the function calculation and recorded by the log of the audit record just now. In a real application scenario, customers can get the relevant information in this log to do more operations.

On how to create ECS as a trigger to trigger function calculation is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report