- Ec2 5분마다 healthcheck 하고 실패하면 종료하기2024년 12월 27일
- wngnl05
- 작성자
- 2024.12.27.:56
while true; do # health check response=$(curl --write-out "%{http_code}" --silent --output /dev/null "http://localhost:8080/health") if [ "$response" -ne 200 ]; then # 인스턴스 ID 가져오기 token=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") Instance_ID=$(curl -s -H "X-aws-ec2-metadata-token: $token" "http://169.254.169.254/latest/meta-data/instance-id") Instance_Name=$(aws ec2 describe-instances --instance-ids $Instance_ID --query 'Reservations[0].Instances[0].Tags[?Key==`Name`].Value' --output text) # 도커 컨테이너 로그 가져오기 Container_ID=$(docker ps -a --format "{{.ID}}") Error_Log=$(docker logs "$container_id") sns_topic_arn='<SNS ARN>' message="$Instance_Name (ID: $Instance_ID)를 종료했습니다.\n\n$Error_Log" aws sns publish --topic-arn "$sns_topic_arn" --message "$message" --subject "$Instance_Name 인스턴스 종료" aws ec2 terminate-instances --instance-ids $Instance_ID fi sleep 300 # 5분 대기 done
'Aws' 카테고리의 다른 글
CloudWatch 로그 그룹에 지표 필터 사용하기 (0) 2024.12.27 IAM 역활 특정 정책을 제외하고 모두 REVOKE (0) 2024.12.27 CloudFront Function 다른 경로로 접근 시 redirect (0) 2024.12.27 ApiGateway 통합응답 문법 (0) 2024.12.27 DynamoDB + ApiGateway를 이용해서 Serverless API 만들어보기 (0) 2024.12.27 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)