# デプロイメント前提条件

EMQX Platform Bring Your Own Cloud（BYOC）をデプロイする前に、以下の準備を完了してください。

- VPC、サブネット、ECSなどのパブリッククラウドサービスおよびネットワーク構成の基本概念を理解する。
- パブリッククラウドアカウントとEMQX Platformアカウントを用意する。
- 関連するクラウドリソースとクラウドアカウントの権限を準備する。
- デプロイメントのブートストラップ環境を構築する。
- EMQXサービス用のドメイン名と対応するTLS/SSL証明書を準備する。
- EMQX BYOCライセンスを準備する。

## アカウント準備

EMQX BYOCのデプロイメントはお客様のクラウドアカウント上に作成されます。対応するパブリッククラウドアカウントをお持ちでない場合は、ご希望のクラウドプラットフォームの公式ドキュメントに従ってアカウントを作成してください。例：

- [AWS組織内でのアカウント作成](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_accounts_create.html)
- [Google Cloud Platformアカウントの作成](https://cloud.google.com/docs/get-started)

現在、BYOCは以下のパブリッククラウドおよびリージョンをサポートしています。その他のクラウドサービスプロバイダーやリージョンのサポートが必要な場合は、チケットを提出いただくか、[メール](<mailto:cloud-support@emqx.io>)でお問い合わせください。

| クラウドプロバイダー | リージョン                                                                                                                                                                                                                                            |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| AWS                  | **米国:** US East (N. Virginia), US East (Ohio), US West (N. California), US West (Oregon)<br />**欧州:** EU (Ireland), EU (Frankfurt)<br />**アジア:** Asia Pacific (Singapore), Asia Pacific (Mumbai), Asia Pacific (Hong Kong), Asia Pacific (Tokyo) |
| Google Cloud         | **米国:** us-east1 (South Carolina), us-west1 (Oregon), us-central1 (Iowa)<br />**欧州:** europe-west3 (Frankfurt), europe-north1 (Finland)<br />**アジア:** asia-south1 (Mumbai), asia-southeast1 (Singapore), asia-east1 (Taiwan)                     |

また、デプロイメントを完了するにはEMQX Platformアカウントも必要です。未登録の場合は、[EMQX Platformアカウント登録ページ](https://accounts.emqx.com/signup)から登録してください。

## ドメインと証明書

EMQXサービス用のドメインおよびTLS/SSL証明書を事前に準備してください。EMQX Platformはカスタムの片方向TLS/SSL認証をサポートしています。

::: warning 注意事項

1. ポリシー要件により、ドメインはICP登録を完了している必要があり、ドメインが登録されているクラウドサービスプロバイダーはBYOCをデプロイするプラットフォームと一致している必要があります。
2. BYOCデプロイメントはCA署名済み証明書のみ受け付けます。TLS/SSL証明書のフォーマット要件については、[BYOCプランのTLS/SSL設定](../deployments/byoc_ssl.md)を参照してください。

:::

## リソースと権限

EMQX BYOCのデプロイメントには、お客様のクラウドアカウント内でさまざまなクラウドリソースとサービスを作成する必要があります。関連するクラウドアカウントに十分なリソースクォータが確保され、必要な権限設定が完了していることを確認してください。

### リソースクォータ

以下の表はEMQX BYOCデプロイメントに必要なクラウドリソースとサービスを示しています。現在のアカウントのリソースが不足している場合は、クラウドアカウント管理者に連絡して該当サービスのクォータ増加を依頼してください。

::: tip

クラウド管理者はクラウドクォータでリソースとサービスの使用状況およびクォータを迅速に確認できます。
- [AWSサービスクォータ](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html)
- [Google Cloudのクォータ](https://cloud.google.com/docs/quota_detail/view_manage)

:::

:::: tabs
::: tab "AWS"

| リソースとサービス           | デプロイメントに必要な数量 |
|------------------------------|----------------------------|
| VPC                          | 1                          |
| サブネット                   | 3                          |
| セキュリティグループ         | 1                          |
| ルートテーブル*              | 3                          |
| ロードバランサー             | 1                          |
| LBリスナー                   | 6                          |
| ターゲットグループ           | 6                          |
| インターネットゲートウェイ   | 1                          |
| キーペア                     | 1                          |
| EC2インスタンス              | N*+1                       |
| Amazon Machine Images (AMIs) | 1                          |
| IAMポリシー                  | 1                          |

*ルートテーブル: 使用されていないデフォルトのルーティングテーブルを含みます。

*N: EMQXノード数を指します。
:::
::: tab "Google Cloud"

| リソースとサービス        | デプロイメントに必要な数量 |
|---------------------------|----------------------------|
| VPCネットワーク           | 1                          |
| サブネット                | 4                          |
| ファイアウォールルール    | 3                          |
| ネットワークロードバランサー | 2                          |
| IPアドレス                | 2                          |
| VMインスタンス            | N*+1                       |
| VMイメージ                | 1                          |
| IAMロール                 | 2                          |
| IAMサービスアカウント     | 1                          |

*N: EMQXノード数を指します。
:::

::::

::: warning 注意
Google Cloudを使用する場合、GCPアカウントにデフォルトネットワークが存在するかを必ず確認してください。存在しない場合は作成してください。デプロイメントプロセスではpackerがシステムイメージのパッケージングツールとして使用されます。これにはVPCのデフォルトネットワーク内でパッケージングに必要なリソースを作成する必要があります。

セキュリティポリシーによりデフォルトネットワークが作成できない場合は、チケットを提出いただくか、[メール](mailto:cloud-support@emqx.io)でお問い合わせください。
:::

### IAM権限

クラウドアカウント内でクラウドリソースを作成するために必要なIdentity and Access Management（IAM）権限をロールに付与する必要があります。クラウド管理者にBYOCデプロイメント作成に十分な権限を割り当ててもらい、対応する認証情報を生成してください。

:::: tabs
::: tab "AWS"

カスタムポリシーを作成するには、[JSONエディターを使用したポリシー作成](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html#access_policies_create-json-editor)の手順に従い、提供されたポリシー定義を利用してください。カスタムポリシー作成後、IAMユーザーまたはユーザーグループにアタッチします。最後に、[IAMユーザーのアクセスキー管理](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html)の手順に従い、IAMユーザーのアクセスキーを生成してください。

以下はデプロイメント作成用のJSONポリシー定義です：
```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": [
        "ec2:AssociateRouteTable",
        "ec2:AttachInternetGateway",
        "ec2:AuthorizeSecurityGroupIngress",
        "ec2:CreateInternetGateway",
        "ec2:CreateImage",
        "ec2:CreateKeyPair",
        "ec2:CreateRoute",
        "ec2:CreateRouteTable",
        "ec2:CreateSecurityGroup",
        "ec2:CreateSubnet",
        "ec2:CreateTags",
        "ec2:CreateVpc",
        "ec2:DescribeInstances",
        "ec2:DescribeInstanceAttribute",
        "ec2:DescribeInstanceCreditSpecifications",
        "ec2:DescribeInstanceTypes",
        "ec2:DescribeInternetGateways",
        "ec2:DescribeImages",
        "ec2:DescribeRegions",
        "ec2:DescribeRouteTables",
        "ec2:DescribeSecurityGroups",
        "ec2:DescribeSecurityGroupRules",
        "ec2:DescribeKeyPairs",
        "ec2:DescribeSubnets",
        "ec2:DescribeTags",
        "ec2:DescribeVolumes",
        "ec2:DescribeVpcs",
        "ec2:DescribeVpcAttribute",
        "ec2:DeleteSecurityGroup",
        "ec2:DeleteKeyPair",
        "ec2:ImportKeyPair",
        "ec2:RunInstances",
        "ec2:ModifyImageAttribute",
        "ec2:ModifySubnetAttribute",
        "ec2:ModifyVpcAttribute",
        "elasticloadbalancing:AddTags",
        "elasticloadbalancing:CreateListener",
        "elasticloadbalancing:CreateLoadBalancer",
        "elasticloadbalancing:CreateTargetGroup",
        "elasticloadbalancing:DescribeListeners",
        "elasticloadbalancing:DescribeLoadBalancers",
        "elasticloadbalancing:DescribeLoadBalancerAttributes",
        "elasticloadbalancing:DescribeTags",
        "elasticloadbalancing:DescribeTargetGroups",
        "elasticloadbalancing:DescribeTargetGroupAttributes",
        "elasticloadbalancing:DescribeTargetHealth",
        "elasticloadbalancing:ModifyLoadBalancerAttributes",
        "elasticloadbalancing:ModifyTargetGroupAttributes",
        "elasticloadbalancing:RegisterTargets"
      ],
      "Resource": "*"
    },
    {
      "Sid": "VisualEditor1",
      "Effect": "Allow",
      "Action": [
        "ec2:StopInstances",
        "ec2:TerminateInstances"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/used-by": "emqx-cloud"
        }
      }
    }
  ]
}
```
以下はデプロイメント削除用のJSONポリシー定義です：
```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeInstances",
        "ec2:DescribeInstanceAttribute",
        "ec2:DescribeInstanceTypes",
        "ec2:DescribeInstanceCreditSpecifications",
        "ec2:DescribeInternetGateways",
        "ec2:DescribeImages",
        "ec2:DescribeKeyPairs",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DescribeRouteTables",
        "ec2:DescribeSecurityGroups",
        "ec2:DescribeSubnets",
        "ec2:DescribeTags",
        "ec2:DescribeVolumes",
        "ec2:DescribeVpcs",
        "ec2:DescribeVpcAttribute",
        "ec2:DetachInternetGateway",
        "ec2:DisassociateRouteTable",
        "ec2:ModifyInstanceAttribute",
        "elasticloadbalancing:DescribeLoadBalancers",
        "elasticloadbalancing:DescribeLoadBalancerAttributes",
        "elasticloadbalancing:DescribeTags",
        "elasticloadbalancing:DescribeTargetGroups",
        "elasticloadbalancing:DescribeTargetGroupAttributes"
      ],
      "Resource": "*"
    },
    {
      "Sid": "VisualEditor1",
      "Effect": "Allow",
      "Action": [
        "ec2:DeleteInternetGateway",
        "ec2:DeleteKeyPair",
        "ec2:DeleteVpc",
        "ec2:DeleteRouteTable",
        "ec2:DeleteSubnet",
        "ec2:DeregisterImage",
        "ec2:TerminateInstances",
        "elasticloadbalancing:DeleteLoadBalancer",
        "elasticloadbalancing:DeleteListener",
        "elasticloadbalancing:DeleteTargetGroup"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/used-by": "emqx-cloud"
        }
      }
    }
  ]
}
```
以下はデプロイメントの停止および起動用のJSONポリシー定義です：
```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeInstances"
      ],
      "Resource": "*"
    },
    {
      "Sid": "VisualEditor1",
      "Effect": "Allow",
      "Action": [
        "ec2:StartInstances",
        "ec2:StopInstances"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/used-by": "emqx-cloud"
        }
      }
    }
  ]
}
```
:::
::: tab "Google Cloud"

gcloud CLIを使用してカスタムロールを作成するには、[カスタムロールの作成](https://cloud.google.com/iam/docs/creating-custom-roles)の手順に従い、以下のロール定義を利用してください。カスタムロール作成後、サービスアカウントにバインドし、サービスアカウントキーを生成します。

1. カスタムロール用のYAMLファイルを作成します。BYOCデプロイメント作成用と削除用の2つのロールが必要です。

```yaml
title: "EMQX BYOC creation"
description: "EMQX BYOCデプロイメント作成に必要な最小限のロール定義"
stage: "GA"
includedPermissions:
- compute.firewalls.get
- compute.targetPools.get
- compute.images.get
- compute.disks.create
- compute.disks.delete
- compute.disks.get
- compute.instances.setMetadata
- compute.instances.setLabels
- compute.subnetworks.useExternalIp
- compute.subnetworks.get
- compute.instances.setServiceAccount
- compute.addresses.create
- compute.instances.create
- compute.addresses.get
- compute.networks.create
- compute.httpHealthChecks.get
- compute.firewalls.create
- compute.networks.updatePolicy
- compute.images.create
- compute.images.setLabels
- compute.addresses.use
- compute.subnetworks.create
- compute.instances.getSerialPortOutput
- compute.httpHealthChecks.create
- compute.disks.useReadOnly
- compute.forwardingRules.get
- compute.machineTypes.get
- compute.targetPools.create
- compute.zones.get
- compute.regionOperations.get
- compute.images.useReadOnly
- compute.instances.use
- compute.instances.get
- compute.targetPools.use
- compute.forwardingRules.create
- compute.httpHealthChecks.useReadOnly
- compute.instances.delete
- compute.globalOperations.get
- compute.subnetworks.use
- compute.networks.get
- compute.zoneOperations.get
- compute.forwardingRules.setLabels
```

```yaml
title: "EMQX BYOC deleting"
description: "EMQX BYOCデプロイメント削除に必要な最小限のロール定義"
stage: "GA"
includedPermissions:
- compute.firewalls.get
- compute.zoneOperations.get
- compute.instances.delete
- compute.targetPools.delete
- compute.networks.delete
- compute.regionOperations.get
- compute.firewalls.delete
- compute.networks.updatePolicy
- compute.forwardingRules.get
- compute.globalOperations.get
- compute.networks.get
- compute.httpHealthChecks.get
- compute.subnetworks.get
- compute.httpHealthChecks.delete
- compute.targetPools.get
- compute.addresses.delete
- compute.subnetworks.delete
- compute.disks.get
- compute.addresses.get
- compute.instances.get
- compute.forwardingRules.delete
- compute.images.delete
- compute.images.get
```

2. 以下のコマンドでYAMLファイルを使ってロールを作成します。

```bash
gcloud iam roles create CreateEmqxCloudByocRole --project=<project-id> --file=<yaml-file-path>
```

3. （任意）サービスアカウントがない場合は、以下のコマンドで作成します。

```bash
gcloud iam service-accounts create <service-account-name> --project=<project-id>
```

4. 以下のコマンドでロールをサービスアカウントにバインドします。

```bash
gcloud projects add-iam-policy-binding <project-id> --member=serviceAccount:<service-account-name>@<project-id>.iam.gserviceaccount.com --role=projects/<project-id>/roles/CreateEmqxCloudByocRole
```

5. 以下のコマンドでサービスアカウントキーをJSON形式で生成します。

```bash
gcloud iam service-accounts keys create <json-file> --iam-account=<service-account-name>@<project-id>.iam.gserviceaccount.com
```

`<project-id>`, `<yaml-file-path>`, `<service-account-name>`, `<json-file>`は実際の値またはプレースホルダーに置き換えてください。

デプロイメントの**停止および起動**権限を別途設定したい場合は、以下の定義を使用してください。

```yaml
title: "EMQX BYOC operation"
description: "EMQX BYOCデプロイメントの停止または起動に必要な最小限のロール定義"
stage: "GA"
includedPermissions:
- compute.instances.get
- compute.instances.start
- compute.instances.stop
```
:::

::::

## 起動環境の準備

EMQX BYOCをデプロイするには、パブリックネットワークに接続可能なUbuntu 20.04 LTS（AMD64）環境が必要です。以下のいずれかの方法で準備してください。

- （推奨）クラウドアカウント内でUbuntu 22.04 LTSイメージを使い仮想マシンインスタンスを作成する。インスタンスのメモリは1GiB以上必要です。
- ローカルのUbuntu 20.04 LTS環境を使用する。

## BYOCライセンス

EMQX Platformの[BYOCライセンス](./byoc_license.md)を準備してください。BYOCライセンスの申請は弊社営業チームにお問い合わせください。接続デバイス数やメッセージスループットのTPSに応じて最適なライセンス仕様をご案内いたします。
