apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: order-cluster
  version: "1.31"
  region: ap-northeast-2

cloudWatch:
  clusterLogging:
    enableTypes: ["*"]

iam:
  withOIDC: true
  serviceAccounts:
    - metadata:
        name: aws-load-balancer-controller
        namespace: kube-system
      wellKnownPolicies:
        awsLoadBalancerController: true
    - metadata:
        name: cert-manager
        namespace: cert-manager
      wellKnownPolicies:
        certManager: true

vpc:
  subnets:
    public:
      ap-northeast-2a: { id: public_a }
      ap-northeast-2b: { id: public_b }
    private:
      ap-northeast-2a: { id: private_a }
      ap-northeast-2b: { id: private_b }

managedNodeGroups:
  - name: order-app-nodegroup
    instanceName: order-app-node
    instanceType: c5.large
    desiredCapacity: 2
    minSize: 2
    maxSize: 4
    privateNetworking: true
public_a=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=keda-public-subnet-a" --query "Subnets[].SubnetId[]" --region ap-northeast-2 --output text)
public_b=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=keda-public-subnet-b" --query "Subnets[].SubnetId[]" --region ap-northeast-2 --output text)
private_a=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=keda-private-subnet-a" --query "Subnets[].SubnetId[]" --region ap-northeast-2 --output text)
private_b=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=keda-private-subnet-b" --query "Subnets[].SubnetId[]" --region ap-northeast-2 --output text)

sed -i "s|public_a|$public_a|g" cluster.yaml
sed -i "s|public_b|$public_b|g" cluster.yaml
sed -i "s|private_a|$private_a|g" cluster.yaml
sed -i "s|private_b|$private_b|g" cluster.yaml

eksctl create cluster -f cluster.yaml
CLUSTER_NAME=order-cluster
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
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=order \\
  --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 order \\
  --set serviceAccount.operator.create=false \\
  --set serviceAccount.operator.name=keda-operator
apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-processor
  namespace: order
  labels:
    app: order-processor
spec:
  replicas: 1
  selector:
    matchLabels:
      app: order-processor
  template:
    metadata:
      labels:
        app: order-processor
    spec:
      serviceAccountName: keda-operator
      containers:
        - name: order-processor
          image: 362708816803.dkr.ecr.ap-northeast-2.amazonaws.com/order-app:v1
          env:
            - name: QUEUE_URL
              value: "<https://sqs.ap-northeast-2.amazonaws.com/362708816803/order-queue>"
            - name: REGION_NAME
              value: ap-northeast-2
          ports:
            - containerPort: 8080
kubectl apply -f deployment.yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: order-processor-scaler
  namespace: order
spec:
  scaleTargetRef:
    name: order-processor
  minReplicaCount: 1
  maxReplicaCount: 10
  pollingInterval: 10
  cooldownPeriod:  60
  advanced:
    horizontalPodAutoscalerConfig:
      behavior:
        scaleDown:
          stabilizationWindowSeconds: 30   
          policies:
            - type: Percent
              value: 100                    
              periodSeconds: 15          
  triggers:
  - type: aws-sqs-queue
    metadata:
      queueURL: <https://sqs.ap-northeast-2.amazonaws.com/362708816803/order-queue>
      activationQueueLength: "0"
      queueLength: "5"
      awsRegion: ap-northeast-2
      identityOwner: operator