Amazon ECS 태스크 중지 이벤트용 Amazon Simple Notification Service 알림 보내기
Amazon ECS 태스크 중지 이벤트용 Amazon Simple Notification Service 알림 보내기
중요 컨테이너(essential container) 중 하나가 종료되어 태스크가 중지된 경우의 태스크 이벤트만 캡처하는 Amazon EventBridge 이벤트 규칙을 구성해요. 이벤트는 특정 stoppedReason 속성이 있는 태스크 이벤트만 지정된 Amazon SNS 토픽으로 보냅니다.
출처: 문서
본문
중요 컨테이너 중 하나가 종료되어 태스크가 중지된 경우의 태스크 이벤트만 캡처하는 Amazon EventBridge 이벤트 규칙을 구성합니다. 이벤트는 특정 stoppedReason 속성이 있는 태스크 이벤트만 지정된 Amazon SNS 토픽으로 보냅니다.
사전 요구 사항: 테스트 클러스터 설정
이벤트를 캡처할 실행 중인 클러스터가 없다면, "Getting started with the console using Linux containers on AWS Fargate"의 단계를 따라 클러스터를 만드세요. 이 튜토리얼이 끝나면 이 클러스터에서 태스크를 실행해 Amazon SNS 토픽과 EventBridge 규칙이 올바르게 구성되었는지 테스트합니다.
사전 요구 사항: Amazon SNS 권한 구성
EventBridge가 Amazon SNS 토픽에 게시하도록 허용하려면 aws sns get-topic-attributes와 aws sns set-topic-attributes 명령을 사용하세요. 권한 추가 방법은 Amazon Simple Notification Service Developer Guide의 "Amazon SNS permissions"를 참고하세요.
다음 권한을 추가하세요.
{
"Sid": "PublishEventsToMyTopic",
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sns: Publish",
"Resource": "arn:aws:sns:<region>:<account-id>:TaskStoppedAlert",
}
1단계: Amazon SNS 토픽 생성 및 구독
이 튜토리얼에서는 Amazon SNS 토픽을 새 이벤트 규칙의 이벤트 대상으로 구성합니다. Amazon SNS 토픽 생성·구독 방법은 Amazon Simple Notification Service Developer Guide의 "Getting started with Amazon SNS"를 참고하고, 다음 표로 선택할 옵션을 정합니다.
| 옵션 | 값 |
|---|---|
| Type | Standard |
| Name | TaskStoppedAlert |
| Protocol | |
| Endpoint | 현재 접근할 수 있는 이메일 주소 |
2단계: 이벤트 규칙 등록
다음으로, 중지된 컨테이너가 있는 태스크의 task-stopped 이벤트만 캡처하는 이벤트 규칙을 등록합니다. 규칙 생성 방법은 Amazon EventBridge User Guide의 "Create a rule in Amazon EventBridge"를 참고하고, 다음 표로 선택할 옵션을 정합니다.
| 옵션 | 값 |
|---|---|
| Rule type | Rule with an event pattern |
| Event source | AWS events or EventBridge partner events |
| Event pattern | Custom pattern (JSON editor) |
| Event pattern | 아래 JSON |
| Target type | AWS service |
| Target | SNS topic |
| Topic | TaskStoppedAlert (1단계에서 생성한 토픽) |
{
"source":[
"aws.ecs"
],
"detail-type":[
"ECS Task State Change"
],
"detail":{
"lastStatus":[
"STOPPED"
],
"stoppedReason":[
"Essential container in task exited"
]
}
}
3단계: 규칙 테스트하기
시작한 직후에 종료되는 태스크를 실행해 규칙이 작동하는지 확인합니다. 이벤트 규칙이 올바르게 구성되었다면 몇 분 안에 이벤트 텍스트가 담긴 이메일을 받게 됩니다. 규칙 요구 사항을 충족하는 기존 태스크 정의가 있다면 그걸로 태스크를 실행합니다. 없다면 다음 단계로 Fargate 태스크 정의를 등록하고 실행해 봅니다.
- 콘솔을 https://console.aws.amazon.com/ecs/v2 에서 엽니다.
- 탐색 창에서 Task definitions를 선택합니다.
- Create new task definition, Create new task definition with JSON을 선택합니다.
- JSON 편집기 상자에서 JSON 파일을 편집하고 다음을 편집기에 복사합니다.
{
"containerDefinitions":[
{
"command":[
"sh",
"-c",
"sleep 5"
],
"essential":true,
"image":"public.ecr.aws/amazonlinux/amazonlinux:latest",
"name":"test-sleep"
}
],
"cpu":"256",
"executionRoleArn":"arn:aws:iam::012345678910:role/ecsTaskExecutionRole",
"family":"fargate-task-definition",
"memory":"512",
"networkMode":"awsvpc",
"requiresCompatibilities":[
"FARGATE"
]
}
- Create를 선택합니다.
- 콘솔에서 태스크를 실행합니다.
- 콘솔을 https://console.aws.amazon.com/ecs/v2 에서 엽니다.
- Clusters 페이지에서 사전 요구 사항에서 만든 클러스터를 선택합니다.
- Tasks 탭에서 Run new task를 선택합니다.
- Application type에서 Task를 선택합니다.
- Task definition에서
fargate-task-definition을 선택합니다. - Desired tasks에 시작할 태스크 수를 입력합니다.
- Create를 선택합니다.
더 알아보기 (Learn more)
- Amazon EventBridge 규칙과 Amazon SNS 알림에 대한 자세한 내용은 AWS 공식 문서를 참고해 주세요.