Untitled

오늘 일정은 어제 구현 못한 람다 함수를 구현하고 일일 성능 지표에 들어갈 그라파나 대시보드를 만드는 것이다.

import zlib
import gzip
import json
import base64
import urllib.request
from base64 import b64decode

def post_slack(argStr):
    message = argStr
    send_data = {
        "text": message,
    }
    send_text = json.dumps(send_data)
    request = urllib.request.Request(
        "<https://hooks.slack.com/services/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>", 
        data=send_text.encode('utf-8'), 
    )

    with urllib.request.urlopen(request) as response:
        slack_message = response.read()        
        

def lambda_handler(event, context):

    if 'awslogs' in event:
        cw_data = event['awslogs']['data']

        compressed_payload = base64.b64decode(cw_data)
        uncompressed_payload = gzip.decompress(compressed_payload).decode('utf-8')
        payload = json.loads(uncompressed_payload)
    
        log_events = payload['logEvents']
        for log_event in log_events:
            print(f'LogEvent: {log_event}')    
        
            messages = log_event['message']
            if messages is not None:
                print(messages)
                messages = json.loads(messages)
            
            detail = messages['detail']
            if detail is not None:
                print(detail)
            
            findings = detail['findings']
            if findings is not None:
                print(findings)
            
            
            for finding in findings:
                ProductName = finding['ProductName']
                Severity = finding['FindingProviderFields']['Severity']['Label']
                description = finding['Description']
                time = messages['time']
                        
                slack_message = "Time: {}\\nProductName: {}\\nLabel: {}\\nDescription: {}\\n".format(time, ProductName, Severity, description)
                        
                print(slack_message)        
                        
                post_slack(slack_message)
                    
    else:
        print("'awslogs' key not found in event data")

lambda 함수의 수정이 있었다. print를 찍으면서 지금 데이터가 어떻게 전달되고 있는지 하나하나 확인 했고, python 문법에 대해서 공부하고 진행 하였다.

완성한 Lambda 함수는 CloudWatch에서 인코딩 되어 들어오는 로그를 디코딩하여 원하는 데이터인 시간, 정보, 레벨, 내용을 추출하여 Slack으로 보내준다.

Untitled

Lambda 함수에 전달되는 인코딩 된 로그를 뽑아서 그 로그로 테스트하는 환경을 구축했다.

Untitled

테스트 데이터가 들어간 후 SecurityHub에 Confog의 점검 상황이 들어가 로그가 제대로 나왔다.

Untitled

아키텍처 미팅이 있었다. 지금까지의 구현 상황을 브리핑 후, 이후에 구현할 방향에 대해서 이야기하는 시간을 가졌다.

Untitled