# S3 Intelligent Tiering
# Reduce S3 storage costs by routing data to different storage tiers based on access patterns and content
#
# Source: https://docs.expanso.io/examples/s3-intelligent-tiering
#
# Usage:
#   curl -o config.yaml https://docs.expanso.io/examples/s3-intelligent-tiering.yaml
#   expanso-edge run -f config.yaml

input:
  kafka:
    addresses:
      - ${KAFKA_BROKERS:localhost:9092}
    topics:
      - application-logs
    consumer_group: expanso-s3-tiering

pipeline:
  processors:
    # Parse the log event
    - mapping: |
        root = this.parse_json()

    # Classify severity for tiering
    - mapping: |
        let level = this.level.lowercase().or("info")
        root = this
        root.storage_tier = match $level {
          "error" => "hot",
          "fatal" => "hot",
          "warn"  => "warm",
          _       => "cold"
        }

output:
  switch:
    cases:
      # Errors and fatals → S3 Standard (instant access)
      - check: this.storage_tier == "hot"
        output:
          aws_s3:
            bucket: ${S3_BUCKET:my-logs}
            path: hot/${!now().ts_format("2006/01/02/15")}/${!uuid_v4()}.json
            storage_class: STANDARD
            region: ${AWS_REGION:us-east-1}
            batching:
              count: 50
              period: 10s

      # Warnings → S3 Standard-IA (infrequent access)
      - check: this.storage_tier == "warm"
        output:
          aws_s3:
            bucket: ${S3_BUCKET:my-logs}
            path: warm/${!now().ts_format("2006/01/02")}/${!uuid_v4()}.json
            storage_class: STANDARD_IA
            region: ${AWS_REGION:us-east-1}
            batching:
              count: 500
              period: 60s

      # Everything else → Glacier Deep Archive
      - check: this.storage_tier == "cold"
        output:
          aws_s3:
            bucket: ${S3_BUCKET:my-logs}
            path: archive/${!now().ts_format("2006/01/02")}/${!uuid_v4()}.json
            storage_class: DEEP_ARCHIVE
            region: ${AWS_REGION:us-east-1}
            batching:
              count: 2000
              period: 300s
