skip to Main Content

I’m deploying some lambda functions and at the end, I need to print the complete URL of the latest CloudWatch log stream. So that other users can view the log directly by clicking the URL.

I’ve tried the below, but I need to print the complete URL.

     print("CloudWatch log stream name:", context.log_stream_name)

I believe this can be done by using describe_log_streams along with some tweaks. But I’m helpless where to start.

2

Answers


  1. Chosen as BEST ANSWER

    I tried the below code and its working as expected, please comment for any improvement.

    def multiple_replace(text, adict):
        rx = re.compile('|'.join(map(re.escape, adict)))
        def one_xlat(match):
            return adict[match.group(0)]
        return rx.sub(one_xlat, text)
    
    def main(event, context):
        region = context.invoked_function_arn.split(':')[3]
        replace_map = {}
        replace_map['/']='$252F'
        replace_map['[']='$255B'
        replace_map['$']='$2524'
        replace_map[']']='$255D'
    
        cw_logs = 'https://' + region + '.console.aws.amazon.com/cloudwatch/home?region=' + region + '#logsV2:log-groups/log-group/' + context.log_group_name.replace('/', '%252F') + '/log-events/' + multiple_replace(context.log_stream_name,replace_map)
    

  2. It can be done much easier (just replace "$" before replacing other special characters, then "$" doesn’t require any special treating is as "escape character"):

        region = context.invoked_function_arn.split(':')[3]
        log_stream_name = context.log_stream_name.replace('$','$2524').replace('/','$252F').replace('[','$255B').replace(']','$255D')
        log_group_name = context.log_group_name.replace('/', '$252F')
        cw_logs_url = 'https://' + region + '.console.aws.amazon.com/cloudwatch/home?region=' + region + '#logsV2:log-groups/log-group/'+ log_group_name  + '/log-events/' + log_stream_name
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search