# Scale on queue length with KEDA

The keda add-on scales workloads on events, and on the length of your {{product}} queues with no key in the cluster.

Replace _my-cluster_ with your cluster's name or id and _orders_ with one of your queues.

The `keda` add-on runs KEDA: a ScaledObject scales a Deployment (to zero, too) on an event source,
a ScaledJob starts Jobs. For your Deutsche Cloud queues, list them in the add-on's `queues`: KEDA then
reads their length as the IAM service account `eks-keda-<cluster id>`, whose only permission is
`queue:GetQueueAttributes` on those queues. It gets temporary credentials for its own
service-account token, so no key is stored in the cluster. Every other KEDA scaler works with your
own TriggerAuthentication.

The listed queues' lengths (never their messages) are readable by every ScaledObject in the
cluster.

## Prerequisites

- Permission for `eks:CreateAddon` on the cluster and `queue:GetQueueAttributes` on each queue.
  In the console, also `eks:DescribeCluster` to open the cluster.

## Install the add-on

**Console**
1. Open **Kubernetes**, open the cluster, then choose **Add-ons**.
2. Choose **Add an add-on**, then **Event autoscaling (KEDA)**.
3. Add the queues whose length should drive scaling.
4. Choose **Install**.

**CLI**
```sh
dcloud eks addons create my-cluster --name keda --config 'queues=["orders"]'
```

**API**
```http
POST /eks/api/clusters/my-cluster/addons
{ "name": "keda", "config": { "queues": ["orders"] } }
```

**Terraform**
```hcl
resource "dcloud_eks_addon" "keda" {
  cluster_id = dcloud_eks_cluster.main.id
  name       = "keda"
  config     = jsonencode({ queues = ["orders"] })
}
```

## Scale a worker on its queue

The queue's URL is on the queue's page and in `dcloud queue queues get`.

```yaml
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata: { name: ews-queues, namespace: shop }
spec:
  podIdentity: { provider: aws }
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata: { name: worker, namespace: shop }
spec:
  scaleTargetRef: { name: worker }
  minReplicaCount: 0
  maxReplicaCount: 20
  triggers:
    - type: aws-sqs-queue
      authenticationRef: { name: ews-queues }
      metadata:
        queueURL: https://sqs.example/123/orders
        queueLength: '10'
        awsRegion: de1
```

The worker runs one replica per ten waiting messages, and none while the queue is empty.
