0

使用此tutorial我創建了一個lambda函數,當它失敗時,雲計時警報會導致SNS使用lambda錯誤度量標準發送電子郵件。我正在使用它來檢查是否有任何ec2實例正在upcomming scheduled events。眼下,這是信息的CloudWatch和SNS在發送電子郵件的:如何更改cloudwatch SNS電子郵件?

Alarm Details: 
- Name:      ec2-scheduled-events-alarm 
- Description:    an ec2 instance has an upcomming scheduled event 
- State Change:    OK -> ALARM 
- Reason for State Change: Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0). 
- Timestamp:     Wednesday 12 September, 2016 00:16:54 UTC 
- AWS Account:    .......... 

Threshold: 
- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for 300 seconds. 

Monitored Metric: 
- MetricNamespace:   AWS/Lambda 
- MetricName:     Errors 
- Dimensions:     
- Period:      300 seconds 
- Statistic:     Sum 
- Unit:      not specified 

State Change Actions: 
- OK: 
- ALARM: [arn:aws:sns:us-west-2:..........:ec2-scheduled-events] 
- INSUFFICIENT_DATA: 

我想改變這條消息也包含來自我的拉姆達腳本信息(如列明EC2實例我定義爲不及格)。我怎樣才能做到這一點?我猜測它涉及以某種方式改變Monitored Metric:- Dimensions:的輸出。

或更好的但我怎麼才能讓我的電子郵件包含我的lambda函數的日誌輸出?

回答

0

是的,你可能會讓你的lambda函數發佈自己的消息,這可能是日誌信息。你沒有提到你使用的是什麼語言,所以我只是用python來展示它。

from __future__ import print_function 
import json 
import boto3 
import logging 
import time 
import datetime 

logger = logging.getLogger() 
logger.setLevel(logging.INFO) 

def Publish(): 
     """ 
     Send a message to the topic 
     """ 

     sns = boto3.client('sns') 
     subject = '' 
     message = 'Here is the message' 
     # You can likely insert the debugging prints from logger into the 
     # message 

     # This will have TargetArn - the arn of the topic 
     # Subject and message of the email. There are other parameters as 
     # well. Just check out boto3 sns api 

     sns.publish(
      TargetArn='arn:', 
      Subject= subject, 
      Message= message)#of email 
    return 

我不確定您是否能夠從您顯示的默認SNS電子郵件中訪問您的lambda日誌中的信息。我列出的選項可能是您的解決方案。您需要製作一個單獨的SNS主題並訂閱它,因此這可能會對您所尋找的內容造成太大的開銷。

我希望這有助於!

(編輯:我現在意識到你在9個月前問過這個問題,所以你可能已經想通了,哎呀。)

相關問題