# Get started with an application load balancer

Put an application load balancer in front of an instance that serves HTTP, reach it at its own name, and delete it again.

This tutorial uses the `application` type, because it answers at a name of its own as soon as it
is active, with no DNS record of yours. Replace _web-lb_ with the name you want and _de1_ with your
region.

## Prerequisites

- An instance in the same region with a public IPv4 address, serving HTTP on port 80. An
  application load balancer reaches an instance over its public address. (In an organization
  whose load balancers run on its own hosts, it reaches the instance at its site address
  instead, and no public address is needed.)
- Permission for `lb:CreateLoadBalancer`, `lb:AddService`, `lb:AddTarget`,
  `lb:DescribeLoadBalancers` and, for the clean-up, `lb:DeleteLoadBalancer`.
- In the console, `lb:DescribeLoadBalancers` to open **Load balancers** (without it the page
  says you do not have permission to view load balancers), and `ec2:DescribeInstances` to pick the
  instance from a list.
- Room in the `lb.load-balancers` quota of the region.

## Step 1: Create the load balancer

**Important:**
You pay for an application load balancer from the hour it is active until you delete it, at most
€4.99 a month.

**Console**
1. Open **Load balancers**, then choose **Create load balancer**.
2. For **Name**, enter web-lb.
3. For **Region**, choose your region.
4. For **Type**, choose the `application` entry.
5. Choose **Create**.

The load balancer appears in the list with the status `provisioning`, and turns `active` once the
fleet carries it.

**CLI**
```sh
dcloud lb load-balancers create --name web-lb --type application --region-id de1
```

**API**
```http
POST /lb/api/load-balancers
{ "name": "web-lb", "type": "application", "regionId": "de1" }
```

The answer is `201` with the new load balancer, its `status` `provisioning` and its `id`.

## Step 2: Add an HTTP service

The service listens on port 80 and forwards to port 80 on the target.

**Console**
1. Open the load balancer, then the **Services** tab.
2. Choose **Add service**.
3. Keep **Protocol** on HTTP, **Source port** on 80 and **Destination port** on 80.
4. Choose **Add**.

**CLI**
```sh
dcloud lb services add lb-id --protocol http --source-port 80 --destination-port 80
```

**API**
```http
POST /lb/api/load-balancers/lb-id/services
{ "protocol": "http", "sourcePort": 80, "destinationPort": 80 }
```

## Step 3: Add your instance as a target

**Console**
1. Open the **Targets** tab, then choose **Add target**.
2. Keep **Target type** on Instance, and for **Instance**, pick your instance.
3. Choose **Add**.

The target shows the status `unknown` until the fleet has checked it, then `healthy`.

**CLI**
```sh
dcloud lb targets add lb-id --type server --instance-id i-0123456789abcdef
```

**API**
```http
POST /lb/api/load-balancers/lb-id/targets
{ "type": "server", "instanceId": "i-0123456789abcdef" }
```

## Step 4: Send a request

On the **Overview** tab, under **Names**, copy the platform name, then request it:

```sh
curl -i http://web-lb-1a2b3c4d.lb.apps.example.com/
```

The answer comes from your instance. If the health check (a `GET /` on port 80) has taken the target
out of rotation, the load balancer answers `503` instead. With the CLI, the name is the `hostname` field of
`dcloud lb load-balancers get lb-id`.

## Clean up

Deleting the load balancer removes its services and targets; the instance keeps running.

**Console**
1. Open the load balancer, then the **Danger zone** tab.
2. Choose **Delete load balancer**.
3. Type the load balancer's name, then choose **Delete load balancer**.

**CLI**
```sh
dcloud lb load-balancers delete lb-id
```

**API**
```http
DELETE /lb/api/load-balancers/lb-id
```

## Next steps

- [Serve HTTPS](serve-https.md)
- [Route requests by host and path](route-by-host-and-path.md)
- [Load balancer types](load-balancer-types.md)
