阿里雲函數計算是一個事件驅動的全託管計算服務。經過函數計算,您無需管理服務器等基礎設施,只需編寫代碼並上傳。函數計算會爲您準備好計算資源,以彈性、可靠的方式運行您的代碼,並提供日誌查詢,性能監控,報警等功能。藉助於函數計算,您能夠快速構建任何類型的應用和服務,無需管理和運維。更棒的是,您只須要爲代碼實際運行消耗的資源付費,而代碼未運行則不產生費用。html
阿里雲雲監控爲雲上用戶提供開箱即用的企業級開放型一站式監控解決方案。涵蓋 IT 設施基礎監控,外網網絡質量撥測監控,基於事件、自定義指標、日誌的業務監控。爲您全方位提供更高效、更全面、更省錢的監控服務。json
雲監控提供了豐富事件,而且事件還在不斷豐富中(雲產品系統事件監控), 豐富的事件觸發自定義處理的函數,能夠實現更加完美的的自動化運維。api
在本文中,重點關注函數計算對 ecs 的重啓事件處理,由於這些 ecs 重啓事件是目前用戶須要很高優先級用戶優先級去響應的;假設以前一臺 ecs 發生因系統錯誤而重啓,用戶可能會緊急起來作一些驗證或者建立快照的處理, 在本示例中,咱們對一臺由於系統錯誤實例重啓或者因實例錯誤而重啓的機器進行自動化處理,好比成功重啓後建立快照處理。服務器
ecs 系統事件 微信
雲產品系統事件監控 網絡
注:記得給函數的service的role設置操做ecs的權限less
mock調試 運維
模擬真實的ecs事件<br />請參考演練系統事件處理程序? So Easy~dom
# -*- coding: utf-8 -*- import logging import json, random, string, time from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.DeleteSnapshotRequest import DeleteSnapshotRequest from aliyunsdkecs.request.v20140526.CreateSnapshotRequest import CreateSnapshotRequest from aliyunsdkecs.request.v20140526.DescribeDisksRequest import DescribeDisksRequest from aliyunsdkcore.auth.credentials import StsTokenCredential LOGGER = logging.getLogger() clt = None def handler(event, context): creds = context.credentials sts_token_credential = StsTokenCredential(creds.access_key_id, creds.access_key_secret, creds.security_token) ''' { "product": "ECS", "content": { "executeFinishTime": "2018-06-08T01:25:37Z", "executeStartTime": "2018-06-08T01:23:37Z", "ecsInstanceName": "timewarp", "eventId": "e-t4nhcpqcu8fqushpn3mm", "eventType": "InstanceFailure.Reboot", "ecsInstanceId": "i-bp18l0uopocfc98xxxx" }, "resourceId": "acs:ecs:cn-hangzhou:123456789:instance/i-bp18l0uopocfc98xxxx", "level": "CRITICAL", "instanceName": "instanceName", "status": "Executing", "name": "Instance:SystemFailure.Reboot:Executing", "regionId": "cn-hangzhou" } ''' evt = json.loads(event) content = evt.get("content"); ecsInstanceId = content.get("ecsInstanceId"); regionId = evt.get("regionId"); global clt clt = client.AcsClient(region_id=regionId, credential=sts_token_credential) name = evt.get("name"); name = name.lower() if name in ['Instance:SystemFailure.Reboot:Executing'.lower(), "Instance:InstanceFailure.Reboot:Executing".lower()]: pass # do other things if name in ['Instance:SystemFailure.Reboot:Executed'.lower(), "Instance:InstanceFailure.Reboot:Executed".lower()]: request = DescribeDisksRequest() request.add_query_param("RegionId", "cn-shenzhen") request.set_InstanceId(ecsInstanceId) response = _send_request(request) disks = response.get('Disks').get('Disk', []) for disk in disks: diskId = disk["DiskId"] SnapshotId = create_ecs_snap_by_id(diskId) LOGGER.info("Create ecs snap sucess, ecs id = %s , disk id = %s ", ecsInstanceId, diskId) def create_ecs_snap_by_id(disk_id): LOGGER.info("Create ecs snap, disk id is %s ", disk_id) request = CreateSnapshotRequest() request.set_DiskId(disk_id) request.set_SnapshotName("reboot_" + ''.join(random.choice(string.ascii_lowercase) for _ in range(6))) response = _send_request(request) return response.get("SnapshotId") # send open api request def _send_request(request): request.set_accept_format('json') try: response_str = clt.do_action_with_exception(request) LOGGER.info(response_str) response_detail = json.loads(response_str) return response_detail except Exception as e: LOGGER.error(e)
「阿里巴巴雲原生微信公衆號(ID:Alicloudnative)關注微服務、Serverless、容器、Service Mesh 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,作最懂雲原生開發者的技術公衆號。」函數