cloudwatch-alarm-to-slack-pythonを使って、EC2のメトリクス情報をSlackへ通知させる

EC2のCPU使用率が50%を超えたら、Slackへ通知が飛ぶようにする
EC2→Cloudwatch→Lambda→Slackという感じですね

# Cloudwatch設定
**メトリクス-EC2から対象のリソースを選ぶ**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/fe9a4e3c-1c5b-dd4f-2a16-b570ad8a33c9.png)

**CPU使用率を対象にしてみる**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/ae9ba352-d1b6-7275-4c0a-d1982f03b514.png)

**`グラフ化したメトリクス` を選び、右にあるベルマークを選択**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/c3749ba5-a812-c6ef-a6f4-3e4b053829fc.png)

**アラームを作成**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/0c7cb37c-95d6-78f3-1cf7-4a1ee0aa7a45.png)

**`50%` 以上のCPU使用率でアラートを上がるようにする**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/c2afd263-48a4-73ab-5b14-52926c7953ba.png)

**`新しいトピックを作成` を選びメアドを入力し、`トピックの作成` をしたら、 `次へ` **
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/bfe045a4-8804-04ce-fe85-4efa7a68f285.png)

**`CloudWatch_test` と名前を付け`次へ`**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/b4960abc-0b1a-2964-ab82-12e02bcd8c2d.png)

**プレビュー画面でOKなら、`アラームの作成`**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/13645b56-531c-fca9-e176-7144e69094ea.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/d6214662-f76a-aa50-fd0d-abede6e3d8f5.png)

**アラームが作成される**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/9a6db2f4-d271-71bf-a56f-eda34e5036ee.png)

**サブスクリプションを登録する**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/54fd73c5-2f4c-7529-638d-95e8514c152f.png)

**メールが届くので `Confilm Subscription`**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/9f2da85a-ef49-8fb0-bdbc-4b6745f0b664.png)

`Confilm Subscription` をクリックするとリダイレクトされる
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/1e0ecb73-15b4-e577-0a74-48a49b6e623a.png)

Confilmすると、アラーム状態が`OK`になる
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/b9083dfa-ca95-3876-a76b-d0024cb572c9.png)

# Slack
https://slack.com/apps

**`Inconming Webhook`をインストール**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/9e4c0151-4ab4-2940-5239-dee3b63c83c1.png)

**設定の追加**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/7512bb11-72d8-fa3d-90f4-9d67ccd1449c.png)

**インテグレーションの追加**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/d6cb1097-16e7-b4f7-8958-ca5bea37e348.png)

**WebhookURLを控える**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/52f3b5d0-8806-9bd5-d84b-549aa4c0dfbe.png)

**設定を保存する**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/725df821-911d-1127-44d6-b926d9fff8d4.png)

**OK**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/55b3d9b8-fd26-5c1a-e2a2-9f883d00d483.png)

# KMSの作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/07fd178b-8cce-1946-8598-495d306ac956.png)

**カスタマー管理型のキー**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/323a5c1a-f80e-ca18-ccbc-cdfcb57a838a.png)

**キーを作成**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/9439f809-995c-1fa3-62e8-a105073d7d55.png)

**タグを適当に付ける**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/4703ecad-219b-2e39-a142-294cabdbdb29.png)

**キー管理者を選ぶ**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/40207466-6669-598d-e78d-bab18fe7295e.png)

**アクセス許可も同様に選ぶ**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/32df0af0-78ed-37b7-4745-68bf36717485.png)

**確認画面で完了**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/b84cb63c-f890-09de-5479-338ba0f54aa1.png)

# Lambda

**次はLambda**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/3be15986-15a0-9ad7-c621-410832401108.png)

**関数の作成**
- 設計図の使用
- Cloudwatch-alarm-to-stack-python
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/41644557-37ff-e264-be0c-c63f441797e4.png)

**関数の設定**
基本的な情報
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/2040447a-c263-1689-6aa0-7754e81b03b5.png)


SNSトリガー
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/1801799c-40b6-61c7-d2ca-b4c3d02beb35.png)

環境変数
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/8d258b3c-1127-a8af-6eac-d8cab78d2b6f.png)

**関数が作成された**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/d6342da7-ab20-ee6c-1953-edaa72592910.png)

# 動作テスト
ec2インスタンスへログインしてCPU使用率を100%に持っていく

```
yes > /dev/null
```

**Slackへ通知が来た！**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/293481/88731e9a-4926-013c-172d-f6a29acc860e.png)