2022-03-03
cloud-watch-url

在 AWS 中,所產生的 log 都是會記錄到 cloud-watch 中,所以在產生 mail 通知時,如果能一並建立連結到 cloud-watch 的log,會是滿方便的
產生 url 的過程中,找到 AWS 是將 log_group, log_stream 進行了兩次 encodeURI,然後產生 url 的

可以參考的 python code

`
def get_cloud_watch_search_url(search, log_group, log_stream, region=None,):
“””Return a properly formatted url string for search cloud watch logs

search = "{$.message: "You are amazing"}
log_group = Is the group of message you want to search
log_stream = The stream of logs to search
"""

url = f'https://{region}.console.aws.amazon.com/cloudwatch/home?region={region}'

def aws_encode(value):
    """The heart of this is that AWS likes to quote things twice with some substitution"""
    value = urllib.parse.quote_plus(value)
    value = re.sub(r"\+", " ", value)
    return re.sub(r"%", "$", urllib.parse.quote_plus(value))

bookmark = '#logsV2:log-groups'
bookmark += '/log-group/' + aws_encode(log_group)
bookmark += "/log-events/" + log_stream
bookmark += re.sub(r"%", "$", urllib.parse.quote("?filterPattern="))
bookmark += aws_encode(search)

return url + bookmark

`

Reference

Read More