> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ale.run/llms.txt
> Use this file to discover all available pages before exploring further.

# AWS ALBの作成

## ターゲットグループの作成

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_01.png" />
</Frame>

> `EC2 > Load Balancing > Target groups`で`Create target group`をクリックします。

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_02.png" />
</Frame>

> Specify group detailsメニューでターゲットグループ情報を次のように入力または選択し、`Next`をクリックします。

* Basic configuration
  * Choose a target type: Instances
  * Target group name: ターゲットグループ名
  * Protocol-Port: HTTP, 9001
  * IP address type: IPv4
  * VPC: インスタンスが属するVPC
  * Protocol version: HTTP1

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_03.png" />
</Frame>

> エールをインストールしたインスタンスを選択し、Ports for the selected instancesに9001を入力して`Include as pending below`をクリックします。その後`Create target group`をクリックします。

## ALBの作成

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_04.png" />
</Frame>

> `EC2 > Load Balancing > Load balancers`で`Create Load Balancer`をクリックし、**Application Load Balancer**を選択します。

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_05.png" />
</Frame>

> Load balancer情報を入力または選択し、`Create load balancer`をクリックします。

* Basic configuration
  * Name: ALB名
  * Scheme: internet-facing
  * IP address type: ipv4
* Network mapping
  * VPC: インスタンスが属するVPC
  * Availability Zones: すべての使用可能なサブネットを選択
* Security groups
  * Security groups: EC2インスタンスに適用されたセキュリティグループを選択
* Listeners and routing
  * Listener port: 80
  * Target group: 前に作成したターゲットグループを選択

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_06.png" />
</Frame>

> 生成されたLoad balancerページで`Add rule`をクリックし、以下の5ステップでルールを作成します。

<Steps>
  <Step title="Add rule">
    <Frame>
      <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_07.png" />
    </Frame>

    > Nameフィールドにルール名を入力してください。
  </Step>

  <Step title="Define rule conditions">
    <Frame>
      <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_08.png" />
    </Frame>

    > `Add condition`をクリックして生成されるモーダルでHost headerを選択し、`app.[ドメイン]`を入力して`confirm`をクリックするとConditionカードが生成されます。その後`Next`をクリックしてください。
  </Step>

  <Step title="Define rule Actions">
    <Frame>
      <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_09.png" />
    </Frame>

    > Action typesでForward to target groupsを選択し、前に作成したターゲットグループを選択して`Next`をクリックしてください。
  </Step>

  <Step title="Set rule priority">
    <Frame>
      <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_10.png" />
    </Frame>

    > Priorityを1に設定して`Next`をクリックしてください。
  </Step>

  <Step title="Review and create">
    <Frame>
      <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_11.png" />
    </Frame>

    > 最終的に`Create`をクリックするとルールが生成されます。
  </Step>
</Steps>

<Note>
  ルール生成後少し待機すると、エールがインスタンスで正常に実行されている場合、ALBでルーティングするターゲットグループの状態が以下の画像のようにHealthyと表示されます。

  <Frame>
    <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_12.png" />
  </Frame>
</Note>

## DNSレコードの登録とALB適用

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_13.png" />
</Frame>

> `Route53 > Hosted zones > 適用対象のドメイン`ページで`Create record`をクリックします。

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_14.png" />
</Frame>

> レコード生成ページで以下のように値を入力し、`Create record`をクリックします。

* **Record name**: app
* **Record type**: A
* **Alias**: 有効化
* **Route traffic to**
  * **Alias to Application and Classic Load Balancer**
  * **Region**: VPCのリージョン
  * **ALB**: 前に作成したALBを選択

<Info>
  設定したレコード名のドメイン(ex. app.aletest.run)はエールの接続ドメインとして使用されます。
</Info>

## ACM証明書の生成及び適用

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_15.png" />
</Frame>

> AWS Certificate Managerダッシュボードで`Request`をクリックし、続く画面でRequest a public certificateを選択し`Next`をクリックしてください。

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_16.png" />
</Frame>

> 証明書情報を次のように入力し、`Request`をクリックしてください。

* Domain names
  * Fully qualified domain name: app.\[ドメイン]
* Validation method
  * DNS validation
* Key configuration
  * RSA 2048

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_17.png" />
</Frame>

> DNS validationのため生成された証明書ページで`Create record in Route 53`をクリックし、続いて`Create records`をクリックしてください。

## ALB HTTPSリスナー追加

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_18.png" />
</Frame>

> `EC2 > Load Balancing > Load Balancers`の生成されたLoad balancerページで`Add listener`をクリックしてください。

<Frame>
  <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/04_19.png" />
</Frame>

> 続く画面で次のように値を入力し、`Add`をクリックしてください。

* Listener configuration
  * Protocol: HTTPS
  * Port: 443
* Default actions
  * Routing actions: Forward to target groups
    * Target group: 前に作成したターゲットグループを選択
* Security Policy
  * Security category: All security policies
  * Policy name: `recommended`が表示されたポリシーを適用
* Default SSL/TLS server certificate
  * Certificate source: From ACM
  * Certificate: 前に生成したACM証明書を選択

<Tip>
  `https://app.[ドメイン]`にアクセスしてエールに正常に接続できることを確認してください。

  <Frame>
    <img className="block rounded-md" src="https://files.cloudtype.io/ale-docs/setup/ec2/images/en/04_20.png" />
  </Frame>
</Tip>

<Note>
  正常な使用のため、続くイングレス構成プロセスが必要です。
</Note>
