0

我正在尝试使用 Slack 集成设置 AWS Chatbot,以显示有关 AWS Glue 状态更改(错误)的错误消息。我已设置 AWS EventBridge 事件模式来捕获 Glue 作业状态更改,如下所示:

{
  "source": ["aws.glue"],
  "detail-type": ["Glue Job State Change"],
  "detail": {
    "state": [
      "FAILED"
    ]
  }
}

这成功捕获了所有失败的 Glue 作业,并且我使用输入转换器将 AWS SNS 主题设置为目标。

输入变压器输入路径

{"jobname":"$.detail.jobName","jobrunid":"$.detail.jobRunId","jobstate":"$.detail.state"}

输入变压器输入模板

"{\"detail-type\": \"Glue Job <job-name> has entered the state <job-state> with the message <message>.\"}"

AWS SNS 有一个 AWS Chatbot 订阅端点,它无法将通知发送到 Slack。

AWS Chatbot CloudWatch 使用 Input Transformer 记录事件后的日志

Event received is not supported (see https://docs.aws.amazon.com/chatbot/latest/adminguide/related-services.html ): 
{
    "subscribeUrl": null,
    "type": "Notification",
    "signatureVersion": "1",
    "signature": <signature>,
    "topicArn": <topic-arn>,
    "signingCertUrl": <signing-cert-url>,
    "messageId": <message-id>,
    "message": "{\"detail-type\": \"Glue Job MyJob has entered the state FAILED with the message SystemExit: None.\"}",
    "subject": null,
    "unsubscribeUrl": <unsubscribe-url>,
    "timestamp": "2022-03-02T12:17:16.879Z",
    "token": null
}

当 AWS EventBridge 选择目标中的输入设置为“匹配事件”时,Slack 通知将发送,但它缺少任何详细信息。

松弛通知

Glue Job State Change | eu-west-1 | Account: <account>
Glue Job State Change

AWS EventBridge 匹配事件 JSON 输出

{
    "Type" : "Notification",
    "MessageId" : <message-id>,
    "TopicArn" : <topic-arn>,
    "Message" : "{\"detail-type\": [\"Glue Job State Change\"]}",
    "Timestamp" : "2022-03-02T11:17:52.443Z",
    "SignatureVersion" : "1",
    "Signature" : <signature>,
    "SigningCertURL" : <signing-cert-url>,
    "UnsubscribeURL" : <unsubscribe-url>
  }

两个 JSON 输出之间几乎没有区别,但是输入转换器被认为是不受支持的事件。使用 AWS Chatbot 处理错误时是否可以生成自定义消息?

4

1 回答 1

0

最好的解决方案是创建一个 Lambda 函数作为 AWS EventBridge 的目标,该函数对Slack Webhook执行 POST 。

# Import modules
import logging
import json
import urllib3

# Set up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)


# Define Lambda function
def lambda_handler(event, context):
    
    http = urllib3.PoolManager()
    
    url = <url>
    link = <glue-studio-monitoring-link>
    message = f"A Glue Job {event['detail']['jobName']} with Job Run ID {event['detail']['jobRunId']} has entered the state {event['detail']['state']} with error message: {event['detail']['message']}. Visit the link for job monitoring {link}"
    logger.info(message)
    headers = {"Content-type": "application/json"}
    data = {'text': message}
    response = http.request('POST',
                        url,
                        body = json.dumps(data),
                        headers = headers,
                        retries = False)
    logger.info(response.status)
于 2022-03-02T14:39:55.173 回答