List icon
Script icon
If icon
DeleteList icon
SlackIncomingWebhook icon
Log icon
Schedule icon

Cloudflare R2 Lifecycle Compaction and Cost Guard

Automate Cloudflare R2 storage compaction, prune stale multipart and transient partitions, and alert via Slack on FinOps anomalies with Kestra.

Categories
CloudInfrastructure

Automated lifecycle auditing, transient object compaction, and cost guard alerting for Cloudflare R2 object storage buckets using S3-compatible APIs.

How it works

  1. A daily schedule trigger initiates the storage audit at 02:00 UTC.
  2. The S3 listing task queries the Cloudflare R2 endpoint using account credentials and S3-compatible APIs to discover transient partitions.
  3. An inline Python task parses partition timestamps, evaluates object age against a 7-day retention threshold, and fails loudly with explicit exceptions if the endpoint returns invalid data.
  4. A conditional flow gate checks if stale objects or threshold breaches require remediation.
  5. When compaction is required, the S3 DeleteList task prunes stale objects directly from the target partition prefix.
  6. An actionable notification is dispatched to Slack reporting reclaimed storage capacity and object counts.
  7. A compliance log task records the operational audit summary for observability.

What you get

  • Automated pruning of abandoned multipart uploads and orphaned transient partitions.
  • Active lifecycle maintenance ensuring Cloudflare R2 storage stays strictly within budget.
  • Proactive Slack alerts detailing exact reclaimed gigabytes upon compaction.

Who it's for

  • FinOps Practitioners and Cloud Cost Managers tracking multi-cloud storage spend.
  • Data Platform Engineers operating analytical data lakes on Cloudflare R2.
  • Site Reliability Engineers ensuring object store hygiene across environments.

Why orchestrate this with Kestra

  • Declarative YAML definition version-controlled alongside data pipelines.
  • Secure credential management keeping Cloudflare API keys masked in execution logs.
  • Native support for S3-compatible endpoints with custom endpoint routing and path styles.

Prerequisites

  • A Cloudflare account with an active R2 bucket.
  • R2 API token with read and write permissions (Access Key ID and Secret Access Key).
  • A Slack Incoming Webhook URL for alerting notifications.

Secrets

  • R2_ACCOUNT_ID: Cloudflare alphanumeric Account ID used for the R2 S3 endpoint URL.
  • R2_ACCESS_KEY_ID: S3-compatible access key credential for Cloudflare R2.
  • R2_SECRET_ACCESS_KEY: S3-compatible secret key credential for Cloudflare R2.
  • R2_BUCKET_NAME: Target R2 bucket name to monitor and compact.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL for operational alerts.

Quick start

  1. Store your Cloudflare R2 credentials and Slack webhook URL in your Kestra namespace secrets.
  2. Adjust the prefix in list_transient_partitions to match your target partition folder (e.g. transient-logs/).
  3. Run a manual test execution to verify connectivity and validate bucket metrics.
  4. Enable the schedule trigger for continuous daily automated audits.

How to extend

  • Forward storage volume metrics to Datadog or Prometheus using Kestra metrics plugins.
  • Add multi-bucket parallel iteration with io.kestra.plugin.core.flow.Loop across all organizational buckets.
  • Incorporate multi-stage tiering by transferring older partitions to cold archive storage.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.