CLUSTER_NAME=<CLUSTER_NAME>
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
aws sqs create-queue --queue-name wsi-demo-queue
aws ecr create-repository \\
    --repository-name sqsconsumer
cat << EOF > iam_policy.json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "GetQueueAttributes",
            "Effect": "Allow",
            "Action": [
                "sqs:GetQueueAttributes",
                "sqs:ReceiveMessage",
                "sqs:GetQueueUrl",
                "sqs:ListQueues",
                "sqs:deletemessage"
            ],
            "Resource": "*"
        }
    ]
}
EOF
aws iam create-policy \\
    --policy-name SqsPolicy \\
    --policy-document file://iam_policy.json
eksctl create iamserviceaccount \\
  --cluster=$CLUSTER_NAME \\
  --namespace=keda-sqs-guidance \\
  --name=keda-operator \\
  --role-name=keda-operator-role \\
  --attach-policy-arn=arn:aws:iam::$AWS_ACCOUNT_ID:policy/SqsPolicy \\
  --approve
helm repo add kedacore <https://kedacore.github.io/charts>
helm repo update
helm install keda kedacore/keda \\
  -n keda-sqs-guidance \\
  --set serviceAccount.operator.create=false \\
  --set serviceAccount.operator.name=keda-operator
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: sqsconsumer-hpa
  namespace: keda-sqs-guidance
spec:
  scaleTargetRef:
    name: sqs-consumer-backend
  minReplicaCount: 2
  maxReplicaCount: 10
  pollingInterval: 10
  cooldownPeriod:  60
  advanced:
    horizontalPodAutoscalerConfig:
      behavior:
        scaleDown:
          stabilizationWindowSeconds: 30    # 최근 30초간 메트릭 참고
          policies:
            - type: Percent
              value: 100                    # 최대 100%까지 줄일 수 있음
              periodSeconds: 15            # 15초마다 스케일 인 평가
  triggers:
  - type: aws-sqs-queue
    metadata:
      queueURL: <https://sqs.ap-northeast-2.amazonaws.com/362708816803/wsi-demo-queue>
      activationQueueLength: "0"
      queueLength: "5"
      awsRegion: ap-northeast-2
      identityOwner: operator
kubectl apply -f scaledobject.yaml
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: sqs-consumer-backend
  namespace: keda-sqs-guidance
spec:
  selector:
    matchLabels:
      app: sqs-consumer-backend
  template: 
    metadata:
      labels:
        app: sqs-consumer-backend
    spec:
      serviceAccountName: keda-operator
      containers:
      - name: sqs-consumer
        image: 362708816803.dkr.ecr.ap-northeast-2.amazonaws.com/sqsconsumer:latest
        env:
        - name: RELIABLE_QUEUE_NAME
          value: wsi-demo-queue
        - name: AWS_REGION
          value: ap-northeast-2
        - name: MAX_MSGS_PER_BATCH
          value: "5"
        - name: MSG_POLL_BACKOFF
          value: "2"
        - name: MSG_PROCESS_DELAY
          value: "10"
        - name: TOT_MSGS_TO_PROCESS
          value: "10000"
        - name: LOG_LEVEL
          value: INFO