# Serve HTTPS

Add an https service whose certificate {{product}} issues for names in your hosted zones and renews, optionally redirecting HTTP to HTTPS.

An `https` service ends TLS on the load balancer and forwards plain HTTP to the targets on the
destination port. This page covers a certificate Deutsche Cloud issues, on `lb11`, `lb21`, `lb31` and
`application`; for your own certificate (and on `dedicated`), see
[Use your own certificate](upload-a-certificate.md). Replace _www.example.com_ with your names.

## Prerequisites

- Permission for `lb:AddService`; `lb:UpdateService` to renew the certificate yourself.
- In the console, also `lb:DescribeLoadBalancers` on the load balancer; without it its page
  answers 404.
- Every name in one of your hosted zones under DNS, delegated to the Deutsche Cloud nameservers: the
  certificate is proven over DNS (DNS-01) through them, because port 80 goes to your targets.
  Wildcards such as `*.example.com` work. See [DNS](/docs/dns).
- For the HTTP-to-HTTPS redirect, no other service on port 80, and the https service itself not on
  port 80.
- On `application`: at most 10 names including the platform name, none of them served by another
  load balancer on the fleet; the https service listens on port 443.

## Add the https service

**Console**
1. Open **Load balancers**, open the load balancer, then the **Services** tab.
2. Choose **Add service**.
3. For **Protocol**, choose **HTTPS**. **Source port** moves to 443.
4. For **Certificate**, keep **Issued by Deutsche Cloud for my domains**.
5. For **Domains**, enter the names, one per line.
6. Keep **Redirect HTTP to HTTPS** selected to have port 80 answer with a redirect, or clear it.
7. For **Destination port**, enter the port your targets serve plain HTTP on.
8. Choose **Add**.

The **Certificate** column shows **Issuing**, then **Served** within about a minute, or
**Failed**. The console form selects the redirect; the API's default is no redirect.

**CLI**
```sh
dcloud lb services add lb-id --protocol https --source-port 443 --destination-port 8080 \
  --domains www.example.com --domains example.com --redirect-http
```

**API**
```http
POST /lb/api/load-balancers/lb-id/services
{ "protocol": "https", "sourcePort": 443, "destinationPort": 8080,
  "domains": ["www.example.com", "example.com"], "redirectHttp": true }
```

The answer is `201` with `certificateStatus: "pending"`. Read the load balancer until the service's
`certificateStatus` is `active`, or `failed` with the reason in `certificateError`.

**Terraform**
```hcl
resource "dcloud_lb_service" "https" {
  load_balancer_id = dcloud_lb.web.id
  protocol         = "https"
  source_port      = 443
  destination_port = 8080
  domains          = ["www.example.com"]
  redirect_http    = true
}
```

With the redirect, port 80 answers every request with a permanent redirect to the same URL over
`https` (`308` on `application`), and no other service can use port 80. On `lb11`–`lb31` the service exists at the provider from the
moment its certificate is issued. On `application`, an https service without `domains` or a
certificate (API, CLI or Terraform) serves only the platform name, with the fleet's own
certificate.

## Renewal

Deutsche Cloud checks its certificates daily and issues a new one when fewer than 30 days are left,
and again for a failed one. A failed issuance or renewal is also sent as an event
(`Certificate Issuance Failed` or `Certificate Renewal Failed`, source `ews.lb`).

To issue it again now, after fixing a zone or to retry:

**Console**
1. On the **Services** tab, choose the https service's row.
2. Choose **Renew now**, or **Try again** after a failure. Neither is offered while it is being
   issued.

**CLI**
```sh
dcloud lb services renew-certificate lb-id service-id
```

**API**
```http
POST /lb/api/load-balancers/lb-id/services/service-id/renew-certificate
```

`202`, and the certificate is issued in the background.

**Terraform:** not available. Renewing is an action, not a setting.

## When it is refused

- `400` for a name that is not in any of your hosted zones; the message names it.
- `400` `This load balancer already has a service on port 443` (with your port).
- `400` `The HTTP-to-HTTPS redirect needs port 80, and another service already listens there; remove it first`.
- `400` `An https service that redirects HTTP cannot itself listen on port 80`.
- `400` on `application` over 10 names, or for a name another load balancer already serves
  (`Already served by another load balancer on the fleet: …`).
- `400` for a renewal of an uploaded certificate.

## Related

- [Use your own certificate](upload-a-certificate.md)
- [Point a domain at a load balancer](point-a-domain.md)
- [Troubleshooting](troubleshooting.md)
