# Use your own certificate

Add an https service with a certificate and private key you upload, and replace it before it expires.

An uploaded certificate is attached when the service is added and is never renewed by
Deutsche Cloud: you replace it before it expires.

## Prerequisites

- Permission for `lb:AddService`; to replace the certificate later, also `lb:DeleteService`.
- In the console, also `lb:DescribeLoadBalancers` on the load balancer; without it its page
  answers 404.
- The certificate in PEM, leaf first, then its chain (at most 64 KiB), and its private key in PEM,
  not encrypted (at most 16 KiB).
- On `lb11`, `lb21` and `lb31`, a load balancer that is no longer `provisioning`.
- On `application` and `dedicated`, every name on the certificate in one of your hosted zones, at
  most 10 names including the load balancer's own name, and none served by another load balancer
  on the region's fleet.

Where the key is kept depends on the type: on `lb11`–`lb31` it is handed to the provider and not
stored by Deutsche Cloud; on `application` and `dedicated` it is stored sealed under the KMS master key
and opened only to configure the fleet.

## Add the service

**Console**
1. Open **Load balancers**, open the load balancer, then the **Services** tab.
2. Choose **Add service**.
3. For **Protocol**, choose **HTTPS**.
4. For **Certificate**, choose **Upload my own**.
5. For **Certificate (PEM)**, paste the certificate, then its chain.
6. For **Private key (PEM)**, paste the key.
7. Select or clear **Redirect HTTP to HTTPS**.
8. Enter the **Source port** and the **Destination port**, then choose **Add**.

The key is not kept in the form's saved draft.

**CLI**
```sh
dcloud lb services add lb-id --protocol https --source-port 443 --destination-port 8080 \
  --certificate-pem "$(cat fullchain.pem)" --private-key-pem "$(cat key.pem)"
```

**API**
```http
POST /lb/api/load-balancers/lb-id/services
{ "protocol": "https", "sourcePort": 443, "destinationPort": 8080,
  "certificatePem": "-----BEGIN CERTIFICATE-----\n…", "privateKeyPem": "-----BEGIN PRIVATE KEY-----\n…" }
```

**Terraform**
```hcl
resource "dcloud_lb_service" "https" {
  load_balancer_id = dcloud_lb.web.id
  protocol         = "https"
  source_port      = 443
  destination_port = 8080
  certificate_pem  = file("fullchain.pem")
  private_key_pem  = file("key.pem")
}
```

Terraform does not read the certificate or the key back, so they are empty after an import.

## Result

`201` with the service, `certificateSource: "uploaded"`, `certificateStatus: "active"`, the names
read from the certificate in `domains` and its expiry in `certificateNotAfter`.

## Replace the certificate

A certificate cannot be replaced in place: remove the service (see
[Add or remove a service](add-or-remove-a-service.md)) and add it again with the new pair. The port
is not served between the two steps.

## When it is refused

- `400` `certificatePem is not a PEM-encoded X.509 certificate`.
- `400` `privateKeyPem is not a PEM-encoded private key (it must not be encrypted)`.
- `400` `privateKeyPem is not the key of this certificate`.
- `400` when the certificate has expired.
- `400` `An uploaded certificate needs both certificatePem and privateKeyPem`.
- `400` `Give either domains (the platform issues the certificate) or an uploaded certificate, not both`.
- `400` on `application` and `dedicated` for a name outside your hosted zones, over 10 names, or
  for a name another load balancer serves (`Already served by another load balancer on the fleet: …`).
- `409` `The load balancer is still provisioning; add the https service once it is active`
  (`lb11`–`lb31`).
- `502` when the provider refuses the certificate (`lb11`–`lb31`); the service is not added.

## Related

- [Serve HTTPS](serve-https.md)
- [Security](security.md)
