How to use Keda in EKS with SQS as Trigger
Tags: keda, aws, kubernetes, eks
Published: 2024-01-21 19:05:39

What problem Keda will solve in this case cenario ? 

- Event Driven requests to start new pods in eks
- Manage to save costs in stand by pods

##How to install keda in eks: 

We can download it from [here](https://github.com/kedacore/keda/releases/download/v2.13.0/keda-2.13.0-core.yaml) and apply it with: 

```
kubectl apply -f keda-2.13.0-core.yaml
```
or just execute it like this


```
kubectl apply --server-side -f https://github.com/kedacore/keda/releases/download/v2.13.0/keda-2.13.0-core.yaml
```

##Making it work in EKS:

Then when it is installed we can go and make an sample Python App

```python
import json
from fastapi import FastAPI
import boto3

#Sample Python App for getting queue messages

app = FastAPI()

def getQueue(queue: str):
    sqs = boto3.resource('sqs', region_name='')
    # Get the queue
    print(queue)
    queue = sqs.get_queue_by_name(QueueName='')
    QMessage = []
    for message in queue.receive_messages():
        QMessage.append(message.body)
        message.delete()
    return QMessage

@app.get("/getQueue")
def root(queue: str):
    returnList = getQueue(queue)
    return {"queue Message list": returnList}

@app.get("/ping")
def ping():
    return {"status": "ok"}
```
Now lets make an Deployment file for it:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-app
  namespace: keda
spec:
  selector:
    matchLabels:
      app: python-app
  replicas: 1
  template:
    metadata:
      labels:
        app: python-app
    spec:
      serviceAccount: keda-operator
      containers:
        - name: python-app
          image: ecr_url
          command: ["uvicorn"]
          args :  ["app.main:app", "--host", "0.0.0.0", "--port", "80"]
          resources:
            limits:
              cpu: "1"
              memory: "2Gi"
            requests:
              cpu: "1"
              memory: "2Gi"
          ports:
            - containerPort: 80
          

```
Then add the Service Account for permissions:

```yaml
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
  labels:
    app.kubernetes.io/name: keda-operator
    app.kubernetes.io/part-of: keda-operator
    app.kubernetes.io/version: main
  name: keda-operator
  namespace: keda
  annotations:
   eks.amazonaws.com/role-arn:  arn:aws:iam::accountId:role/KedaSqsExecutionRole
```
And then add the scaled object for the SQS Queue and the Auth Trigger:

```yaml
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: keda-trigger-auth-aws-credentials
  namespace: keda
spec:
  podIdentity:
    provider: aws-eks
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: aws-sqs-queue-scaledobject
  namespace: keda
spec:
  minReplicaCount: 0  #-> min pods that will be when there is smt in queue
  maxReplicaCount: 5 #-> max pods
  pollingInterval: 10 
  cooldownPeriod:  25 
  scaleTargetRef:
    name: python-app
  triggers:
  - type: aws-sqs-queue
    authenticationRef:
      name: keda-trigger-auth-aws-credentials
    metadata:
      queueURL: sqs_queue_url
      queueLength: "1"
      awsRegion: "aws_regiion"
      identityOwner: operator
```