jdecool/scaleway-runtimepublic Fork
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
docs: verify Command Invocations end to end on Scaleway b64f0a3Unverified · on main · Jérémy DECOOL · 10d ago
main.tf · 102 lines · 2.9 KBTerraform Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
# Deploys the example application.
# Credentials come from the usual SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID variables.
# Build the zip first: (cd example && vendor/bin/scaleway-package).

terraform {
  required_version = ">= 1.5"

  required_providers {
    scaleway = {
      source  = "scaleway/scaleway"
      version = "~> 2.92"
    }
  }
}

variable "region" {
  type    = string
  default = "fr-par"
}

variable "zip_file" {
  type    = string
  default = "../function.zip"
}

variable "app_secret" {
  type      = string
  sensitive = true
}

# The public host of the function, for absolute URLs: its own domain_name cannot be referenced here (a cycle),
# so set it to a custom domain, or to the endpoint output after a first apply. Empty keeps the runtime's localhost.
variable "host" {
  type    = string
  default = ""
}

# Lets CRON triggers run console commands; see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md.
variable "cron_secret" {
  type      = string
  sensitive = true
}

provider "scaleway" {
  region = var.region
}

resource "scaleway_function_namespace" "example" {
  name        = "scaleway-runtime-example"
  description = "Example Symfony application of jdecool/scaleway-runtime"
}

resource "scaleway_function" "example" {
  namespace_id = scaleway_function_namespace.example.id
  name         = "example"
  runtime      = "php85"
  handler      = "handler.handle"
  privacy      = "public"
  min_scale    = 0
  # A single instance lets the e2e scenarios observe Warm Process reuse.
  max_scale    = 1
  memory_limit = 256
  timeout      = 60
  zip_file     = var.zip_file
  zip_hash     = filesha256(var.zip_file)
  deploy       = true

  environment_variables = merge({ APP_ENV = "prod" }, var.host == "" ? {} : { SCALEWAY_HOST = var.host })
  secret_environment_variables = {
    APP_SECRET           = var.app_secret
    SCALEWAY_CRON_SECRET = var.cron_secret
  }
}

# Calls POST / every hour with the arguments as JSON body.
resource "scaleway_function_cron" "example" {
  function_id = scaleway_function.example.id
  name        = "example-cron"
  schedule    = "0 * * * *"
  args        = jsonencode({ job = "hourly" })

  # Replacing the function deletes its CRON triggers server side: recreate them in the same apply.
  lifecycle {
    replace_triggered_by = [scaleway_function.example.id]
  }
}

# Runs the app:greet console command every hour: the matching secret turns the arguments into a Command Invocation.
resource "scaleway_function_cron" "example_command" {
  function_id = scaleway_function.example.id
  name        = "example-command"
  schedule    = "30 * * * *"
  args        = jsonencode({ command = "app:greet", args = { name = "CRON" }, secret = var.cron_secret })

  lifecycle {
    replace_triggered_by = [scaleway_function.example.id]
  }
}

output "endpoint" {
  value = "https://${scaleway_function.example.domain_name}"
}