【AWS】LaravelアプリをEC2デプロイ⑤【SNS / Chatbot編】

# 0. はじめに
大阪のLaravel初学者サウナーこと、kazumakishimoto([@kazuma_dev](https://twitter.com/kazuma_dev))です！
自動デプロイ(CodeDeploy)の開始と終了を`SNS / Chatbot`で`Slack通知`させる方法です。

## 0-1. 前回記事
- 【AWS】LaravelアプリをEC2デプロイ【まとめ編】

https://qiita.com/kazumakishimoto/items/e0e109fca443a4acd2a1

- 【AWS】LaravelアプリをEC2デプロイ①【CloudFormation / EC2 / RDS編】

https://qiita.com/kazumakishimoto/items/0859a5a365b226c1b43a

- 【AWS】LaravelアプリをEC2デプロイ②【Route53編】

https://qiita.com/kazumakishimoto/items/825f8c5f94d0160b2364

- 【AWS】LaravelアプリをEC2デプロイ③【ACM / ELB編】

https://qiita.com/kazumakishimoto/items/bdb07c6d4881384f7063

- 【AWS】LaravelアプリをEC2デプロイ④【CircleCI / CodeDeploy編】

https://qiita.com/kazumakishimoto/items/09b84473c44c5899ee57

## 0-2. 全体の流れ
> [1.Slack](#1-slack)
> [2.SNS](#2-sns)
> [3.Chatbot](#3-chatbot)
> [補足](#補足)
> [Reference](#reference)
> [次回記事](#次回記事)

## 0-3. 本記事の対象者
- CodeDeployの自動デプロイ開始と終了をSlack通知させたい

## 0-4. 事前準備
- AWSアカウント作成済み
- Slackアカウント作成済み
- リージョンはアジアパシフィック(東京)ap-northeast-1
- `grfl`や`hoge`はサンプル名なので適宜変更して下さい

https://aws.amazon.com/jp/register-flow/

## 0-5. 要件
- Slackワークペース作成
- CodeDeployで通知ルールの作成
- Chatbotでチャネルの作成

## 0-6. 本番環境
|ツール|バージョン|
|:--|:--|
|OS|Amazon Linux 2|
|nginx|1.12|
|PHP|7.4.28|
|Laravel|6.20.44|
|MySQL|5.7.37|
|Composer|1.10.26|
|Node.js|13.14.0|

## 0-7. AWS構成図
![aws](https://user-images.githubusercontent.com/68370181/178103075-eec5508a-4d29-409c-84f4-3f687fa9cd5d.png)


# 1. Slack
https://slack.com/intl/ja-jp/help/articles/206845317

- Slackの手順に従って進んでください
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/8dc96c8a-aa55-6623-7b5f-6d92ded59b76.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/03985267-fd15-4e73-89c3-16900c8a280d.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/2aa8b15e-b03a-e821-9726-5c0426336e20.png)


# 2. SNS
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/fe0159be-d5c5-dc4e-9b87-0aa5f0aa4238.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/ceebd5fd-89ee-d471-5fa4-4d351e50509b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/7d975c1c-c8e1-43ff-7b80-59639f6d9cae.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/89b010fb-598e-48c9-09a2-d7382879768d.png)

# 3. Chatbot
## 3-1. チャネル作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/46d57df7-5a58-ad75-bfa4-df061f4e1067.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/922c6923-2717-3d08-f0fa-10105834466e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/663378e2-2424-0bae-722f-532c503e8c5f.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/5052515b-a10b-a47b-ffd2-8c46ff3aa25d.png)

## 3-2. service-linked role作成
- 『AWS ChatbotはSlackのチャネル設定を作成できません。』のエラーが発生するため、Chatbotに`iam:CreateServiceLinkedRole`のインラインポリシーを作成
```
AWS Chatbot was unable to create the service-linked role for account xxxxxxxxxxxx. Rate exceeded (Service: AmazonIdentityManagement; Status Code: 400; Error Code: Throttling; Request ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx; Proxy: null). Learn more https://docs.aws.amazon.com/chatbot/latest/adminguide/using-service-linked-roles.html.
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/0178a279-e0a5-c7f3-bf95-f5b7fdb1bdcf.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/7ef85967-45ff-f0b3-a3ef-72f08065b148.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/cf610d59-a009-1e04-0145-bdd523c5dd90.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/e578dfab-de9a-f6ed-16f9-5bc53c9187ec.png)

```json:JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:CreateServiceLinkedRole",
                "iam:UpdateRoleDescription",
                "iam:DeleteServiceLinkedRole",
                "iam:GetServiceLinkedRoleDeletionStatus"
            ],
            "Resource": "*"
        }
    ]
}
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/6372a3af-0c44-e1cb-1a31-7b46b07d8449.png)
- 『Slackチャネルを設定』に戻って再び『設定』を押下してみると完了

https://docs.aws.amazon.com/chatbot/latest/adminguide/using-service-linked-roles.html

https://docs.aws.amazon.com/IAM/latest/UserGuide/using-service-linked-roles.html

## 3-3. 自動デプロイ実行
- 開発ブランチでcommit→GitHubにpush
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/aef21141-824d-35bb-3190-722ccc5e5566.png)


# 補足
## 開発環境（FW/ツールのバージョンなど）
|ツール|バージョン|
|:--|:--|
|Vue.js|2.6.14|
|jQuery|3.4.1|
|PHP|7.4.1|
|Laravel|6.20.43|
|MySQL|5.7.36|
|Nginx|1.18.0|
|Composer|2.0.14|
|npm|6.14.6|
|Git|2.33.1|
|Docker|20.10.11|
|docker-compose|v2.2.1|
|PHPUnit|8.0|
|CircleCI|2.1|
|heroku|7.59.4|
|MacBook Air|M1,2020|
|macOS|Monterey 12.3|
|Homebrew|3.3.8|

## ディレクトリ構造
```
【ルートディレクトリ】
├─ .circleci
│   └─ config.yml
├─ aws / CloudFormation
│   └─ ec2.yml
├─ docker
│   └─ mysql
│   └─ nginx
│   └─ php
│   └─ phpmyadmin
├─ src
│   └─ 【Laravelのパッケージ】
└─ docker-compose.yml
```

# Reference
https://www.techpit.jp/courses/11

https://www.techpit.jp/courses/78

https://qiita.com/yuuichimizuta/items/899251a9cb2dd61dabb0

https://qiita.com/yyy752/items/b9d7674fc349f669e801

https://qiita.com/hiroaki-u/items/d275d406f6405c0a5208

https://slack.com/intl/ja-jp/help/articles/206845317

https://docs.aws.amazon.com/chatbot/latest/adminguide/using-service-linked-roles.html

https://docs.aws.amazon.com/IAM/latest/UserGuide/using-service-linked-roles.html

# 次回記事
- 【AWS】LaravelアプリをEC2デプロイ⑥【S3編】

https://qiita.com/kazumakishimoto/items/d08894711bf59a4e21df

- 【AWS】LaravelアプリをEC2デプロイ⑦【API編】

https://qiita.com/kazumakishimoto/items/fd4afc02f9490ddb89e5

- 【AWS】お役立ちリンク集【随時更新】

https://qiita.com/kazumakishimoto/items/02e53cdf7b9887252040