こんにちは!SRE2課 入倉です。
前回のブログではIncident ManagerとAutomationを使って、自動で電話通知、httpdプロセスの自動復旧を試みました。
しかし、前回の実装方法だとhttpdプロセスが無事に自動復旧した場合も電話通知がくることになります。
その為今回は、復旧した場合は電話通知不要、復旧しなかった場合は電話通知をするという実装を考えてみたいと思います。
構成
今回は以下の構成としました。
前回と違い、架電処理をAutomationの方に外出しする形としています。

Automationの設定
Runbookの作成
今回も前回と同様、カスタムランブックの作成を行い、独自の内容をAutomationで実行します。
AWSアカウントにログインを行い、
「Systems Manager」 > 「ドキュメント」 > 「Create document」 > 「Automation」をクリック

「名前」を入力 > 「エディタ」 > 「編集」をクリック

以下のyamlを「ドキュメントエディタ」内に貼り付けます。
※以下の箇所の"xxxxxxxxxxxx"はご自身のAWSアカウント番号に書き換える必要があります。
ActionPrefix: 'arn:aws:ssm-incidents::xxxxxxxxxxxx:response-plan/EC2Incident'
ContactId: 'arn:aws:ssm-contacts:ap-northeast-1:xxxxxxxxxxxx:contact/escalationplan'
description: ''
schemaVersion: '0.3'
assumeRole: '{{ AutomationAssumeRole }}'
parameters:
AutomationAssumeRole:
type: String
description: (Optional) The ARN of the role that allows Automation to perform the actions on your behalf.
mainSteps:
- name: GetInstanceId
action: 'aws:executeAwsApi'
inputs:
Service: cloudwatch
Api: describeAlarms
StateValue: ALARM
ActionPrefix: 'arn:aws:ssm-incidents::xxxxxxxxxxxx:response-plan/EC2Incident'
outputs:
- Selector: '$.MetricAlarms[0].Dimensions[1].Value'
Name: InstanceId
- Selector: '$.MetricAlarms[0].AlarmName'
Name: AlarmName
- name: ListIncidentRecords
action: 'aws:executeAwsApi'
inputs:
Service: ssm-incidents
Api: ListIncidentRecords
filters:
- condition:
equals:
stringValues:
- OPEN
key: status
outputs:
- Name: IncidentRecordsTitle
Selector: '$.incidentRecordSummaries[0].title'
Type: String
- Selector: '$.incidentRecordSummaries[0].arn'
Name: IncidentRecordsArn
Type: String
nextStep: BranchIncidentTitle
- name: BranchIncidentTitle
action: 'aws:branch'
inputs:
Choices:
- NextStep: HttpdRestartCommand
Variable: '{{ListIncidentRecords.IncidentRecordsTitle}}'
StringEquals: 'EC2Incident [{{GetInstanceId.AlarmName}}]'
isEnd: true
- name: HttpdRestartCommand
action: 'aws:runCommand'
inputs:
DocumentName: AWS-RunShellScript
InstanceIds:
- '{{GetInstanceId.InstanceId}}'
Parameters:
commands: systemctl restart httpd
nextStep: HttpdProccessCount
onFailure: 'step:StartEngagement'
- name: HttpdProccessCount
action: 'aws:runCommand'
inputs:
DocumentName: AWS-RunShellScript
InstanceIds:
- '{{GetInstanceId.InstanceId}}'
Parameters:
commands: ps aux | grep httpd | grep -v grep | wc -l
nextStep: BranchHttpdProccessCount
- name: BranchHttpdProccessCount
action: 'aws:branch'
inputs:
Choices:
- NextStep: IncidentResolved
Not:
Variable: '{{HttpdProccessCount.Output}}'
StringEquals: '0'
- NextStep: StartEngagement
Variable: '{{HttpdProccessCount.Output}}'
StringEquals: '0'
- name: IncidentResolved
action: 'aws:executeAwsApi'
inputs:
Service: ssm-incidents
Api: UpdateIncidentRecord
arn: '{{ListIncidentRecords.IncidentRecordsArn}}'
status: RESOLVED
isEnd: true
- name: StartEngagement
action: 'aws:executeAwsApi'
inputs:
Service: ssm-contacts
Api: StartEngagement
ContactId: 'arn:aws:ssm-contacts:ap-northeast-1:xxxxxxxxxxxx:contact/escalationplan'
Content: Testing engagements
Sender: SSM Automation
Subject: HttpdProccessError
isEnd: true
上記の内容をフローチャートの図にすると以下の流れとなります。

yamlの内容を入力したら「オートメーションを作成」をクリックします。
Incident Managerの設定
対応プランの修正
「Incident Manager」 > 「対応プラン」 > 前回作成した対応プランを選択 > 「編集」をクリック

「エンゲージメント」を空 > 「ランブック」を先ほど作成したドキュメントを指定 > 「対応プランを更新」をクリック

動作確認
httpdプロセスが自動復旧した場合
それではhttpdプロセスを落としてみます。

オープン状態のインシデントに追加され、一瞬で解決済みのインシデントに追加されました。 また、自動復旧に成功した為、電話通知がくることはありませんでした。

httpdのプロセスも無事「active」になっていました。
httpdプロセスが自動復旧しなかった場合
httpd.confをいじってプロセスを起動できなくしてみます。

Incident Managerにオープン状態でインシデントが残り、電話通知がきました。


プロセスの再起動に失敗し、「Start Engagement」による電話通知が成功していることがわかります。
その他
注意点
インシデントのエンゲージメントの表示について
電話通知についてはAutomationの方で実施とした為、Incident Managerの解決済みのインシデントの内容を見ると「エンゲージメント」タブに何も情報が記載されなくなります。
本来、誰に通知をしたのか、承認をしたのか等の情報を閲覧することが可能ですが、Automationの方で通知することによってその情報がインシデントに紐づかなくなりますのでご注意ください。

OpsItemについて
Incident ManagerのインシデントはOpsCenterのOpsItemと自動で関連付けられます。 Incident Managerのインシデントのみ自動クローズ処理を行っていますので、OpsItemが未解決状態で残存しますのでご注意ください。

料金
対応プラン一つにつき、 7 USD/月となります。
また、通知が日本宛ての場合SMS 0.1 USD/1件、電話 0.18 USD/1件かかります。
まとめ
2回に渡りIncident Manager、Automationについてご紹介しました。
何かアラームが発生した場合は基本メール通知、ランブックの内容が失敗した場合は電話通知にするなど実装方法は色々あるかと思いますのでご自身の環境に合わせて試してみてください。
また、今回ランブックの処理の所で動的にアラームが発生した特定のインスタンスIDを取得してくるのが難しいと感じました。何か処理について他に良いアイデアがあれば共有していただけると幸いです。
それではまたお会いしましょう!
入倉 翔吾(執筆記事の一覧)
クラウドインテグレーション部 SRE2課