일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- 애플리케이션 기본 사용자 인증 정보
- GCP 모니터링
- prometheus dockerfile
- permission denied 에러 발생
- 포트 9255 설정
- prometheus 연동
- 리소스전체삭제
- stackdriver exporter
- 정적 웹 사이트 호스팅
- github 사용법
- git
- stackdriver exporter 설치
- dockerfile로 stackdriver-export이미지 생성
- gke 클러스터 액세스 설정
- 방화벽 오픈
- compose.yml로 stackdriver-export 서비스 설정
- Prometheus 서버 설정
- prometheus 서버 서비스등록
- vs code로 ssh 접속
- prometheus.yml 파일 설정
- minikube 설치 및 실행
- 리소스삭제
- GCP VM
- aws-nuke
- Prometheus 서버 설치
- 스위치 4대 기능
- 서비스 계정 키 설정
- ADC 사용자 인증
- kubectl 설치
- github
- Today
- Total
My __ 노트
AWS PHD 알람 설정 (3) - CloudFormation으로 Lambda를 이용하여 알림 전송 본문
해당 글은 CloudFormation으로 Lambda를 이용하여 Slack에 알람을 보내는 방법에 대해 기재하였으며 아키텍처는 아래와 같습니다.
목차
▶ 스택 배포
▶ Slack 템플릿 변경
▶ 로그 그룹 보존 기간 설정
▶ 스택 배포
먼저 aws에서 제공하는 해당 코드를 이용하여 스택을 배포해 보았습니다.
참고 링크 : https://github.com/aws/aws-health-tools/tree/master/slack-notifier
해당 링크 중 slack-notifier.yml 파일을 보면 슬랙 web hook url와 channel name을 지정하는 파라미터 코드가 있으므로 해당 정보들을 미리 메모장에 복사해 두어야 합니다.
해당 파일로 스택을 생성하면 EventBridge와 Lambda가 배포되며 해당 설정이 정상적으로 적용되었는지 검토하기 위해 Lambda에서 test event에 아래 예시 코드를 넣어 슬랙에 알림이 오는지 확인해 봅니다.
# 예시
{
"version": "0",
"id": "",
"detail-type": "AWS Health Event",
"source": "aws.health",
"account": "123456789012",
"time": "2023-01-26T01:43:21Z",
"region": "ap-southeast-2",
"resources": [],
"detail": {
"eventArn": "arn:aws:health:ap-southeast-2::event/AWS_ELASTICLOADBALANCING_API_ISSUE_90353408594353980",
"service": "ELASTICLOADBALANCING",
"eventTypeCode": "AWS_ELASTICLOADBALANCING_OPERATIONAL_ISSUE",
"eventTypeCategory": "issue",
"eventScopeCode": "PUBLIC",
"communicationId": "4826e1b01e4eed2b0f117c543",
"startTime": "Thu, 26 Jan 2023 13:19:03 GMT",
"endTime": "Thu, 26 Jan 2023 13:44:13 GMT",
"lastUpdatedTime": "Thu, 26 Jan 2023 13:44:13 GMT",
"statusCode": "open",
"eventRegion": "ap-southeast-2",
"eventDescription": [{
"language": "en_US",
"latestDescription": "A description of the event will be provided here"
}]
}
}
아래와 같은 형식으로 알람이 전송되는 것을 확인할 수 있습니다.
▶ Slack 템플릿 변경
AWS Chatbot을 이용하여 슬랙 알림을 받을 경우 아래와 같은 형식으로 메시지가 출력되므로 해당 형식과 유사하게 lambda 코드를 변경해 보겠습니다.
[ 긴 문장 "더 보기" 탭으로 숨기기 ]
eventDescription 부분이 길 경우 문장이 짤 릴 수 있지만 실제로 AWS Chatbot에서 보내는 알림 메시지는 [ 더 보기 ] 탭으로 긴 문장을 짤리지 않게 출력할 수 있는 것을 확인할 수 있습니다. 해당 템플릿 설정을 위해서 알림 메시지를 Section 별로 나눠야 합니다.
AS - IS
slack_message = {
"channel": "${SlackChannel}",
"text": message,
"username": "AWS - Personal Health Updates"
}
TO - BE
slack_message = {
"channel": "aws",
"attachments": [
{
"color": blockcolor,
"blocks" : [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message1
},
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message2
},
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message3
},
}
]
}
],
"username": "AWS - Personal Health Updates"
}
[ 이벤트 유형별 아이콘 설정 ]
PHD 이벤트 유형에 따라 슬랙 메시지 아이콘과 blockcolor가 변경되므로 해당 부분에 대한 코드도 추가해 봅니다.
if event["detail"]["eventTypeCategory"] == str("issue"):
emoji = str(" :wrench: ")
blockcolor = str("FF9900")
elif event["detail"]["eventTypeCategory"] == str("accountNotification"):
emoji = str(" :mega: ")
blockcolor = str("#2B538D")
elif event["detail"]["eventTypeCategory"] == str("scheduledChange"):
emoji = str(" :spiral_calendar_pad: ")
blockcolor = str("#2B538D")
[ 메시지 정보 요약 ]
해당 알람의 event type, region, account 정보, status 정보 등을 한눈에 알아보기 쉽게 메시지 상단에 출력해 주는 코드도 추가해 봅니다.
message1 = str(
emoji + "*<https://phd.aws.amazon.com/phd/home?region=us-east-1#/event-log?eventID=" +
event['detail']['eventArn'] + "|" +
event['detail-type'] + " | " +
event['region'] + " | " +
"Account :" + event['account'] + " | " +
event['detail']['statusCode'] + ">*" +
"\n\n" +
" *Event type code: * " + "*" + event['detail']['eventTypeCode']+ "*" +
"\n\n" +
event['detail']['eventDescription'][0]['latestDescription']+
"\n\n" )
[ 영향받는 리소스 정보 표시 ]
영향받는 리소스의 정보들을 출력해 주는 코드도 추가해 봅니다.
message2 = str("*Affected resources : * " + "\n")
for i in range(len(event['detail']['affectedEntities'])):
resource = event['detail']['affectedEntities'][i]['entityValue'] + "\n"
message2 += resource
위 코드를 lambda 함수에 적용하면 아래와 같은 형식으로 PHD 알림 메시지를 Slack에서 전송받을 수 있습니다.
▶ 로그 그룹 보존 기간 설정
링크에서 공유한 코드에는 로그 보존 기간이 설정되어 있지 않으므로 만기 없음으로 설정됩니다. 비용 관리를 위해 적절한 보존 기간을 설정하려면 스택에 아래 코드를 추가해주어야 합니다.
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${SlackNotifierLambdaFn}"
RetentionInDays: 7
● RetentionInDays 값을 설정 시 아래 기준일에 맞게 설정하지 않으면 만기 없음으로 설정될 수 있습니다.
'클라우드 > AWS(클라우드)' 카테고리의 다른 글
[ Systems Manager ] Private EC2의 사용자 정보를 Fleet Manager로 확인/생성/삭제해보기 (0) | 2023.11.03 |
---|---|
AWS RDS 이벤트 로그 조회 및 수집 (0) | 2023.10.10 |
AWS PHD 알람 설정 (2) - CloudFormation으로 SNS와 ChatBot을 이용하여 알림 전송 (0) | 2023.10.06 |
AWS PHD 알람 설정 (1) - 콘솔에서 SNS와 Chatbot을 이용하여 알람 전송 (0) | 2023.10.05 |
AWS 클라우드 VPC 리소스들에 대한 개념 (4) | 2020.12.09 |