Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions infra/terraform/cloudflare-r2/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: sourceplane.io/v1
kind: Component

metadata:
name: cloudflare-r2
description: Provisions the Cloudflare R2 bucket the app binds (stage and prod)
labels:
domain: infra
scope: app-object-store

spec:
type: terraform
lifecycle: production
owner: platform
domain: infra

parameters:
orgName: sourceplane
stackName: cloudflare-r2
terraformDir: terraform
terraformVersion: "1.15.3"
# SEC-JOB: the wiring document (R2 bucket name) is lease-published
# post-apply; app deploys read it back as WIRING_CLOUDFLARE_R2_<ENV>.
secretOutputs: "WIRING_CLOUDFLARE_R2=wiring"

# The Cloudflare Terraform provider authenticates via the CLOUDFLARE_API_TOKEN
# env var, resolved by orun from the project-rung brokered secret (fresh per
# run, Workers R2 Storage Write scope). Declared at component level because plan-only
# lanes refresh against the live Cloudflare API too, not just apply.
secretEnv:
CLOUDFLARE_API_TOKEN: "secret://commerce/commerce/{{ .environment }}/CLOUDFLARE_API_TOKEN"
# Non-secret connection fact (account-id template) — replaces the ambient
# CLOUDFLARE_ACCOUNT_ID / TF_VAR_cloudflare_account_id CI values.
CLOUDFLARE_ACCOUNT_ID: "secret://commerce/commerce/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID"
TF_VAR_cloudflare_account_id: "secret://commerce/commerce/{{ .environment }}/CLOUDFLARE_ACCOUNT_ID"

subscribe:
environments:
- name: stage
profile: plan-only
profileRules:
- profile: apply
when:
triggerRef: github-push-main

- name: prod
profile: plan-only
profileRules:
- profile: apply
when:
triggerRef: github-push-main
38 changes: 38 additions & 0 deletions infra/terraform/cloudflare-r2/terraform/adopt.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# --- Self-healing adoption ---
#
# When the platform state does not yet track the bucket but Cloudflare already
# has one under this name (a re-bootstrap over a torn-down workspace), import
# it at plan time instead of colliding with the duplicate-name guard. Fresh
# products resolve to id = "" and create normally; a resource already in state
# short-circuits the lookup.

data "external" "adopt_app_bucket" {
program = ["bash", "-c", <<-EOT
set -euo pipefail
name="$(jq -r .name)"
state_file="$(mktemp)"
trap 'rm -f "$state_file"' EXIT
code="$(curl -s -o "$state_file" -w '%%{http_code}' \
-u "$TF_HTTP_USERNAME:$TF_HTTP_PASSWORD" "$TF_HTTP_ADDRESS" || echo 000)"
if [ "$code" = "200" ] && jq -e \
'[.resources[]? | select(.type == "cloudflare_r2_bucket" and .name == "app")] | length > 0' \
"$state_file" >/dev/null 2>&1; then
jq -n '{id: ""}'
exit 0
fi
resp="$(curl -fsS --retry 3 -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
"https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/r2/buckets?per_page=1000")"
printf '%s' "$resp" | jq -c --arg n "$name" \
'{id: ([.result.buckets[]? | select(.name == $n) | .name] | first // "")}'
EOT
]
query = {
name = local.bucket_name
}
}

import {
for_each = toset(compact([data.external.adopt_app_bucket.result.id]))
to = cloudflare_r2_bucket.app
id = "${var.cloudflare_account_id}/${each.value}"
}
5 changes: 5 additions & 0 deletions infra/terraform/cloudflare-r2/terraform/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terraform backend configuration is provided by Orun at runtime
# through environment variables and automatic backend initialization.
#
# S3 backend details are set via CLI args in the Orun runner.
# Local placeholder file for clarity.
117 changes: 117 additions & 0 deletions infra/terraform/cloudflare-r2/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
terraform {
required_version = ">= 1.15.0"

# State lives on the platform: the runner exports TF_HTTP_* per job, so this
# block stays empty — no S3 bucket, no AWS role, no workspaces.
backend "http" {}

required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.30"
}
external = {
source = "hashicorp/external"
version = "~> 2.3"
}
}
}

# Authenticates via the CLOUDFLARE_API_TOKEN env var (provider-native): the
# token is an orun-managed secret resolved into the job env at run time.
provider "cloudflare" {}

variable "cloudflare_account_id" {
type = string
sensitive = true
default = ""
description = "Cloudflare account ID (from CLOUDFLARE_ACCOUNT_ID env var)"
}

variable "orgName" {
type = string
default = "sourceplane"
}

variable "owner" {
type = string
default = "sourceplane"
}

variable "repo" {
type = string
default = "commerce"
}

variable "namespace" {
type = string
default = "sourceplane"
}

variable "namespacePrefix" {
type = string
default = ""
}

variable "lane" {
type = string
default = "verify"
}

variable "environment" {
type = string
default = "stage"
}

variable "component" {
type = string
default = "cloudflare-r2"
}

variable "stackName" {
type = string
default = "cloudflare-r2"
}

variable "terraformDir" {
type = string
default = "terraform"
}

variable "terraformVersion" {
type = string
default = "1.15.3"
}

# --- The app's object store ---
#
# One R2 bucket per environment. It stands in for Vercel Blob: uploads the app
# accepts (attachments, images) and, where the app's OpenNext config asks for
# it, the Next.js incremental cache. Bucket names are unique per account, so
# the name carries the product and the environment.

locals {
bucket_name = "${var.namespacePrefix}${var.repo}-${var.environment}"
}

resource "cloudflare_r2_bucket" "app" {
account_id = var.cloudflare_account_id
name = local.bucket_name
}

# --- Wiring manifest (via orun secrets) ---
# Published post-apply as the WIRING_CLOUDFLARE_R2 secret on the project/{{env}}
# rung; the app's deploy reads it back as WIRING_CLOUDFLARE_R2_<ENV> and
# renders its wrangler template from it.

output "wiring" {
description = "Wiring document for downstream deploy-time binding resolution (pushed to orun secrets)"
value = jsonencode({
r2_bucket_name = cloudflare_r2_bucket.app.name
})
}

output "r2_bucket_name" {
description = "Cloudflare R2 bucket name (the Workers binding's bucket_name)"
value = cloudflare_r2_bucket.app.name
}
2 changes: 2 additions & 0 deletions infra/terraform/cloudflare-r2/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Output definitions are in main.tf for simplicity.
# This file is a placeholder for consistency with the Orun Terraform pattern.
2 changes: 2 additions & 0 deletions infra/terraform/cloudflare-r2/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Variable definitions are in main.tf for simplicity.
# This file is a placeholder for consistency with the Orun Terraform pattern.
Loading