docs: fold the probe results inUnverified
The README now states the measured platform facts instead of the unverified ones: Composer at deploy time, the lazily built cache and the auto-scripts pitfall, the host option, the trusted headers, the CRON Event and marker, the CORS headers added to every response, the 6 MiB limit and stdout. The plan records Phase 9 and the remaining work, and the probe recreates its CRON trigger when its function is replaced.
7b3548a parent: c20f007 modified
README.md +113 -75 | @@ -9,8 +9,8 @@ It also ships the tools to package the function and to run it locally. | ||
| 9 | 9 | Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOSSARY.md), design decisions in [docs/adr](docs/adr). |
| 10 | 10 | |
| 11 | 11 | > [!IMPORTANT] |
| 12 | -> The runtime has only been run locally, through the Emulator. | |
| 13 | -> Several platform facts it relies on are still assumptions, see [Unverified platform facts](#unverified-platform-facts). | |
| 12 | +> The platform facts the runtime relies on were measured on Scaleway with the [probe](probe), see [docs/probe-results.md](docs/probe-results.md). | |
| 13 | +> The runtime itself has only run through the Emulator so far, and the package is not on Packagist yet. | |
| 14 | 14 | |
| 15 | 15 | ## Contents |
| 16 | 16 | |
| @@ -30,7 +30,7 @@ Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOS | ||
| 30 | 30 | |
| 31 | 31 | - The Scaleway PHP runtimes `php84` or `php85` (PHP 8.4 or later). |
| 32 | 32 | - Symfony 7.4 or 8.x components (`symfony/runtime`, `symfony/http-kernel`, `symfony/http-foundation`). |
| 33 | -- To build the deployment zip: the `zip` PHP extension and `composer` on the `PATH`. | |
| 33 | +- To build the deployment zip: the `zip` PHP extension. | |
| 34 | 34 | - macOS or Linux for the packaging and Emulator tools (see [Windows](#windows)). |
| 35 | 35 | |
| 36 | 36 | ## Installation |
| @@ -97,6 +97,7 @@ Set these on the function, in production: | ||
| 97 | 97 | | Variable | Kind | Why | |
| 98 | 98 | | --- | --- | --- | |
| 99 | 99 | | `APP_SECRET` | Secret | Symfony's `kernel.secret`, used to sign URLs, cookies and CSRF tokens. | |
| 100 | +| `SCALEWAY_HOST` | Plain | The public host of the function, for absolute URLs, see [Host](#host). | | |
| 100 | 101 | | `SCALEWAY_CRON_SECRET` | Secret | Only to run console commands from CRON triggers, see [Command Invocations](#command-invocations). | |
| 101 | 102 | | `APP_ENV` | Plain | Only to run another env than `prod`, see below. | |
| 102 | 103 | |
| @@ -109,13 +110,43 @@ The Emulator runs `dev` instead, unless it is started with an `APP_ENV`. | ||
| 109 | 110 | For every other variable, real environment variables win over `.env` files, as usual with Symfony's Dotenv. |
| 110 | 111 | `.env.local`, `.env.local.php` and `.env.*.local` are never shipped. |
| 111 | 112 | |
| 112 | -### Writable directories | |
| 113 | +### Host | |
| 114 | + | |
| 115 | +No `Host` header reaches the function, so the runtime cannot know the domain it is called on. | |
| 116 | +Every Request gets the host of the `host` runtime option, which defaults to the `SCALEWAY_HOST` environment variable, then to `localhost`: | |
| 117 | + | |
| 118 | +```sh | |
| 119 | +SCALEWAY_HOST=api.example.com | |
| 120 | +``` | |
| 121 | + | |
| 122 | +The scheme comes from the platform's `X-Forwarded-Proto` (`https` for HTTP calls), the port is the scheme's default. | |
| 123 | +Absolute URLs (`UrlGeneratorInterface::ABSOLUTE_URL`, signed URLs, redirects built from the Request) are only right once the host is set. | |
| 124 | +Set it to the function's domain (its `domain_name` on Scaleway) or to its custom domain. | |
| 125 | +With Terraform, a function cannot reference its own `domain_name` in its variables (the reference would be a cycle): use a custom domain, or set the variable after a first apply. | |
| 126 | + | |
| 127 | +### Cache and writable directories | |
| 128 | + | |
| 129 | +The code directory is writable on Scaleway, so the Kernel builds its cache in `var/cache` at the first Invocation of each Warm Process, which pays the container compilation once. | |
| 130 | +To pay it at deploy time instead, warm the cache from a Composer script, which Scaleway runs after installing the dependencies: | |
| 131 | + | |
| 132 | +```json | |
| 133 | +{ | |
| 134 | + "scripts": { | |
| 135 | + "post-install-cmd": ["@php bin/console cache:warmup --env=prod"] | |
| 136 | + } | |
| 137 | +} | |
| 138 | +``` | |
| 139 | + | |
| 140 | +Only do it when the warmup needs no secret: the function's environment variables are not available at deploy time, and a failing script does not fail the deployment, the first Invocation then building the cache as usual. | |
| 141 | + | |
| 142 | +> [!WARNING] | |
| 143 | +> Symfony Flex's `auto-scripts` also run at deploy time, with the `APP_ENV` of the shipped `.env`, usually `dev`: `cache:clear` then fails silently, its dev bundles not being installed with `--no-dev`. | |
| 144 | +> Make them env-explicit (`"cache:clear --env=prod": "symfony-cmd"`), or remove `cache:clear` from them and warm the cache as above. | |
| 113 | 145 | |
| 114 | -The deployed code may be read-only. | |
| 115 | 146 | When the project's `var/` directory (or the project dir, without `var/`) is not writable, the runtime sets, unless your application already did: |
| 116 | 147 | |
| 117 | -- `APP_BUILD_DIR` to `var/cache`, so the container warmed in the zip is used as is; | |
| 118 | -- `APP_CACHE_DIR` to `<temp dir>/cache` and `APP_LOG_DIR` to `<temp dir>/log`. | |
| 148 | +- `APP_CACHE_DIR` to `<temp dir>/cache` and `APP_LOG_DIR` to `<temp dir>/log`; | |
| 149 | +- `APP_BUILD_DIR` to `var/cache`, only when `var/cache/<env>` holds a container warmed before, so that it is used as is. | |
| 119 | 150 | |
| 120 | 151 | Kernels using `MicroKernelTrait` read these variables. |
| 121 | 152 | Other Kernels must override `getCacheDir()`, `getBuildDir()` and `getLogDir()` themselves. |
| @@ -195,9 +226,9 @@ require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | ||
| 195 | 226 | return static fn (): RequestHandlerInterface => new ApiHandler(); |
| 196 | 227 | ``` |
| 197 | 228 | |
| 198 | -The PSR-7 request carries the client address, scheme, host and port resolved from the platform's `X-Forwarded-*` headers, in its URI and in the `REMOTE_ADDR`, `HTTPS`, `HTTP_HOST` and `SERVER_PORT` server params. | |
| 229 | +The PSR-7 request carries the client address, scheme, host and port the runtime resolved (see [Client IP and trusted proxies](#client-ip-and-trusted-proxies)), in its URI and in the `REMOTE_ADDR`, `HTTPS`, `HTTP_HOST` and `SERVER_PORT` server params. | |
| 199 | 230 | A handler implementing `Symfony\Contracts\Service\ResetInterface` is reset before every Invocation but the first. |
| 200 | -A malformed `Host` or `X-Forwarded-Host` is answered with a 400 before reaching the handler. | |
| 231 | +A malformed host is answered with a 400 before reaching the handler. | |
| 201 | 232 | |
| 202 | 233 | ### Console Application or Command |
| 203 | 234 | |
| @@ -253,7 +284,7 @@ An invokable object is not taken for a raw callable; return `$object(...)` to ma | ||
| 253 | 284 | |
| 254 | 285 | ### CRON triggers |
| 255 | 286 | |
| 256 | -A CRON trigger is assumed to call the Application with a `POST /` whose JSON body holds the trigger's arguments: Scaleway does not document the CRON Event, and the [probe](probe) checks it. | |
| 287 | +A CRON trigger calls the Application with a `POST /` whose JSON body holds the trigger's arguments, over plain `http` and without client address (`getClientIp()` is `127.0.0.1`). | |
| 257 | 288 | With a Kernel, handle it like any route: |
| 258 | 289 | |
| 259 | 290 | ```php |
| @@ -265,6 +296,8 @@ public function cron(Request $request): Response | ||
| 265 | 296 | } |
| 266 | 297 | ``` |
| 267 | 298 | |
| 299 | +Any HTTP client can call `POST /` too: tell CRON Invocations apart with a secret of your own in the arguments, or run a command instead. | |
| 300 | + | |
| 268 | 301 | ### Command Invocations |
| 269 | 302 | |
| 270 | 303 | A CRON trigger may run a console command instead, through the Console Application of the same Kernel. |
| @@ -290,10 +323,14 @@ Services are reset around each command, so it shares no state with the Invocatio | ||
| 290 | 323 | |
| 291 | 324 | ### Security model |
| 292 | 325 | |
| 293 | -Nothing known in an Event tells a CRON Invocation from an HTTP one, so any internet client can send a body naming a command to a public function. | |
| 294 | -A body only becomes a Command Invocation when its `secret` matches `SCALEWAY_CRON_SECRET`, compared in constant time. | |
| 295 | -Otherwise the body reaches the Application as a plain HTTP body on `POST /`, and a body carrying a wrong `secret` is logged (without its value). | |
| 296 | -Without `SCALEWAY_CRON_SECRET`, or with an empty one, Command Invocations are off. | |
| 326 | +A body only becomes a Command Invocation when both hold: | |
| 327 | + | |
| 328 | +- the Event carries neither `X-Real-Ip` nor `X-Envoy-External-Address`, which the platform sets on every HTTP call, overwriting what the client sends, and which CRON Events lack; | |
| 329 | +- its `secret` matches `SCALEWAY_CRON_SECRET`, compared in constant time. | |
| 330 | + | |
| 331 | +Otherwise the body reaches the Application as a plain HTTP body on `POST /`, and a rejected body carrying a `secret` is logged (without its value). | |
| 332 | +So no HTTP call can run a command, even with the secret, and without `SCALEWAY_CRON_SECRET`, or with an empty one, Command Invocations are off. | |
| 333 | +The first check relies on undocumented platform behaviour, which is why the secret stays required. | |
| 297 | 334 | |
| 298 | 335 | Keep in mind that: |
| 299 | 336 | |
| @@ -308,38 +345,36 @@ See [ADR 0005](docs/adr/0005-authenticate-command-invocations-with-a-shared-secr | ||
| 308 | 345 | vendor/bin/scaleway-package [function.zip] |
| 309 | 346 | ``` |
| 310 | 347 | |
| 311 | -It builds the deployment zip in a private temporary directory, removed afterwards whatever happens: | |
| 348 | +It zips your application as it is, `composer.json` and `composer.lock` included, but without `vendor/` nor `var/`: Scaleway runs `composer install --no-dev` from the shipped lock at deploy time, Composer scripts included, and replaces any shipped `vendor/` ([ADR 0008](docs/adr/0008-let-scaleway-install-the-dependencies.md)). | |
| 349 | +Executable bits are kept, and symbolic links are followed: their target is zipped. | |
| 312 | 350 | |
| 313 | -1. It copies the application, without the excluded files below and without the zip being written. | |
| 314 | -2. It runs `composer install --no-dev --classmap-authoritative` from the committed `composer.lock`, with `APP_ENV=prod` and `APP_DEBUG=0`. | |
| 315 | - Relative `path` repositories are resolved against your application, and mirrored instead of symlinked. | |
| 316 | -3. It runs `bin/console cache:warmup`, when `bin/console` exists, to ship a warmed prod cache. | |
| 317 | -4. It zips the result, keeping the executable bits, and the `composer.lock` exactly as committed. | |
| 351 | +It refuses to build the zip, as Scaleway would install something else or fail without failing the deployment, when: | |
| 318 | 352 | |
| 319 | -It requires a committed `composer.lock`, for reproducible builds, and `handler.php`. | |
| 320 | -Symbolic links are followed: their target is copied. | |
| 353 | +- `handler.php` or `composer.lock` is missing; | |
| 354 | +- `composer.lock` is not up to date with `composer.json`; | |
| 355 | +- `composer.json` declares a `path` repository, or `composer.lock` installs a package from a local path: Scaleway cannot resolve them. | |
| 321 | 356 | |
| 322 | 357 | | Shipped | Not shipped | |
| 323 | 358 | | --- | --- | |
| 324 | -| Your code, `composer.json`, `composer.lock`, `handler.php` | `vendor/` and `var/` from your machine (rebuilt) | | |
| 325 | -| `vendor/` without dev dependencies | `tests/`, `phpunit.xml(.dist)`, `.phpunit.cache`, `.phpunit.result.cache`, `.php-cs-fixer.cache` | | |
| 326 | -| `var/cache/prod`, warmed | `.env.local`, `.env.local.php`, `.env.*.local` | | |
| 327 | -| `.env`, `.env.prod`... as they are | `.git`, `.idea`, `.vscode`, `.DS_Store`, `node_modules` anywhere | | |
| 359 | +| Your code, `composer.json`, `composer.lock`, `handler.php` | `vendor/` and `var/` (installed and built on the platform) | | |
| 360 | +| `.env`, `.env.prod`... as they are | `tests/`, `phpunit.xml(.dist)`, `.phpunit.cache`, `.phpunit.result.cache`, `.php-cs-fixer.cache` | | |
| 361 | +| | `.env.local`, `.env.local.php`, `.env.*.local` | | |
| 362 | +| | `.git`, `.idea`, `.vscode`, `.DS_Store`, `node_modules` anywhere | | |
| 328 | 363 | | | `.terraform`, `*.tfstate`, `*.tfstate.*` anywhere (Terraform state holds secrets in clear) | |
| 329 | 364 | |
| 330 | 365 | The full list is `Packager::EXCLUDES` in [src/Packager.php](src/Packager.php). |
| 331 | -`--classmap-authoritative` means only classes known at build time can be autoloaded. | |
| 332 | 366 | |
| 333 | 367 | Scaleway accepts zips up to 100 MiB, and up to 500 MiB of unzipped code ([limits](https://www.scaleway.com/en/docs/serverless-functions/reference-content/functions-limitations/)). |
| 334 | -`scaleway-package` warns when the zip exceeds either, but still writes it. | |
| 368 | +`scaleway-package` warns when the zip or its content exceeds either, but still writes it. | |
| 335 | 369 | |
| 336 | 370 | ## Deployment |
| 337 | 371 | |
| 338 | -[example/terraform](example/terraform/main.tf) deploys the [example application](example) with its `php85` runtime, `handler.handle`, `APP_ENV=prod`, the `APP_SECRET` and `SCALEWAY_CRON_SECRET` secrets, a CRON trigger calling `POST /` and one running a command. | |
| 372 | +[example/terraform](example/terraform/main.tf) deploys the [example application](example) with its `php85` runtime, `handler.handle`, `APP_ENV=prod`, the `APP_SECRET` and `SCALEWAY_CRON_SECRET` secrets, an optional `SCALEWAY_HOST`, a CRON trigger calling `POST /` and one running a command. | |
| 339 | 373 | Its [README](example/README.md#deploy-it) gives the commands. |
| 340 | -That Terraform code was written against the `scaleway/scaleway` provider v2.92 documentation and has never been applied. | |
| 374 | +That Terraform code was written against the `scaleway/scaleway` provider v2.92 documentation and has never been applied; the [probe](probe/terraform/main.tf)'s, written the same way, has. | |
| 341 | 375 | |
| 342 | 376 | Redeploy by running `scaleway-package` again, then `terraform apply`. |
| 377 | +Replacing a function, for instance to change its `runtime`, deletes its CRON triggers on Scaleway's side: give them a `lifecycle { replace_triggered_by = [scaleway_function.<name>.id] }`, as the example does, so the same apply recreates them. | |
| 343 | 378 | |
| 344 | 379 | ## Local development with the Emulator |
| 345 | 380 | |
| @@ -353,40 +388,44 @@ It sets `APP_ENV=dev` unless started with an `APP_ENV`, as the deployed function | ||
| 353 | 388 | A crashed process is replaced by a fresh one, as Scaleway starts a new instance. |
| 354 | 389 | |
| 355 | 390 | PHP's built-in server receives the requests, and forwards the Events to that process over a Unix socket in a private temporary directory. |
| 356 | -The context passed to `handle()` is an empty array. | |
| 357 | 391 | |
| 358 | -The Event is built the way the platform is assumed to build it: | |
| 392 | +The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)): | |
| 359 | 393 | |
| 360 | -- header names in `Title-Case`, repeated headers joined with `, `; | |
| 361 | -- the client address appended to `X-Forwarded-For`, and `X-Forwarded-Proto: http`; | |
| 362 | -- `queryStringParameters` as a map of strings, the last value of a repeated key winning; | |
| 363 | -- bodies that are not valid UTF-8 base64-encoded, with `isBase64Encoded: true`. | |
| 394 | +- the `httpMethod`, `queryStringParameters` (the last value of a repeated key winning, `[]` without query string) and other keys of the platform; | |
| 395 | +- header names in `Title-Case`, repeated headers joined with `, `, no `Host`; | |
| 396 | +- `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through; | |
| 397 | +- empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag. | |
| 364 | 398 | |
| 365 | -Simulate a CRON trigger with a `POST /` carrying its arguments as JSON body: | |
| 399 | +So the Requests are `https`, and their host is `localhost` unless you set `SCALEWAY_HOST`, as on the platform. | |
| 400 | + | |
| 401 | +No HTTP call can be a CRON Invocation, so the Emulator turns a `POST` on its reserved `/_scaleway/cron` path into a CRON Event whose arguments are the request body: | |
| 366 | 402 | |
| 367 | 403 | ```sh |
| 368 | -curl -X POST http://127.0.0.1:8000/ -H 'Content-Type: application/json' --data '{"job": "cleanup"}' | |
| 404 | +curl -X POST http://127.0.0.1:8000/_scaleway/cron --data '{"job": "cleanup"}' | |
| 369 | 405 | ``` |
| 370 | 406 | |
| 371 | 407 | Simulate a Command Invocation by starting the Emulator with `SCALEWAY_CRON_SECRET` set, and sending the [Command Invocation body](#command-invocations) the same way: |
| 372 | 408 | |
| 373 | 409 | ```sh |
| 374 | 410 | SCALEWAY_CRON_SECRET=local-secret vendor/bin/scaleway-serve |
| 375 | -curl -X POST http://127.0.0.1:8000/ --data '{"command": "app:greet", "args": {"name": "Scaleway"}, "secret": "local-secret"}' | |
| 411 | +curl -X POST http://127.0.0.1:8000/_scaleway/cron --data '{"command": "app:greet", "args": {"name": "Scaleway"}, "secret": "local-secret"}' | |
| 376 | 412 | ``` |
| 377 | 413 | |
| 378 | -To run exactly what gets deployed, serve the packaged zip from a read-only copy, in `prod`: | |
| 414 | +To run what gets deployed, install the zip the way Scaleway does, without the function's variables, then serve it in `prod`: | |
| 379 | 415 | |
| 380 | 416 | ```sh |
| 381 | 417 | vendor/bin/scaleway-package |
| 382 | -unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function | |
| 418 | +unzip -q function.zip -d /tmp/function | |
| 419 | +composer install --working-dir=/tmp/function --no-dev | |
| 383 | 420 | APP_ENV=prod APP_SECRET=local php /tmp/function/vendor/bin/scaleway-serve |
| 384 | 421 | ``` |
| 385 | 422 | |
| 423 | +The [example](example/README.md#run-it-locally)'s `e2e/deploy-locally.sh` does the same with the runtime of this repository. | |
| 424 | + | |
| 386 | 425 | ## Warm Processes |
| 387 | 426 | |
| 388 | 427 | The Application is built once per Warm Process and reused by the following Invocations, like with FrankenPHP's worker mode or RoadRunner ([ADR 0002](docs/adr/0002-reuse-application-across-invocations.md)). |
| 389 | -If Scaleway does not keep processes warm, this degrades to one boot per Invocation. | |
| 428 | +Scaleway keeps processes warm: consecutive Invocations of an instance run in the same PHP process. | |
| 390 | 429 | |
| 391 | 430 | Between Invocations, for a Kernel: |
| 392 | 431 | |
| @@ -408,56 +447,54 @@ Everything else survives, so your application must be safe for long-running exec | ||
| 408 | 447 | - **Superglobals**: `$_GET`, `$_POST`, `$_COOKIE`, `$_FILES` and the request entries of `$_SERVER` (`REQUEST_URI`, `HTTP_*`...) are not populated per Invocation. |
| 409 | 448 | Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation. |
| 410 | 449 | - **Output**: only the returned `Response` reaches the client. |
| 411 | - `echo` outside the Response, `header()` and `http_response_code()` do not. | |
| 412 | -- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, likely reaches the Application with one value only. | |
| 450 | + `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body. | |
| 451 | +- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only. | |
| 413 | 452 | Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests. |
| 414 | 453 | - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does. |
| 415 | 454 | Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not. |
| 416 | 455 | - **Cookies**: request cookie names are taken verbatim: unlike PHP's `$_COOKIE`, dots and spaces are not turned into underscores and `a[b]` does not nest. |
| 417 | 456 | - **Streaming**: `StreamedResponse` and `BinaryFileResponse` are buffered into one Function Response; Server-Sent Events and long downloads are not possible. |
| 418 | - Bodies that are not valid UTF-8 are sent base64-encoded. | |
| 457 | + Bodies that are not valid UTF-8 are sent base64-encoded, and decoded by the platform. | |
| 419 | 458 | - **PSR-15 responses** go through Symfony's `Response`, which adds `Cache-Control: no-cache, private` and `Date` when they are missing. |
| 420 | -- **HEAD requests**: only FrameworkBundle's `ResponseListener` strips the body of a `HEAD` response. | |
| 421 | - A bare `HttpKernelInterface`, a Response Front Controller or a PSR-15 handler returns the full body, unless it handles `HEAD` itself. | |
| 422 | -- **Request size**: Scaleway documents a 6 MiB limit for request bodies. | |
| 459 | +- **HEAD requests**: only FrameworkBundle's `ResponseListener` strips the body of a `HEAD` response, but the platform sends none anyway. | |
| 460 | +- **Request size**: Scaleway rejects request bodies over 6 MiB with a `413`, before they reach the function. | |
| 461 | +- **CORS**: Scaleway adds `Access-Control-Allow-Origin: *` and `Access-Control-Allow-Headers: Content-Type` to every response, whatever the Application sends. | |
| 462 | + Any website can therefore read the responses of a public function from a browser, though without the user's cookies: do not rely on the same-origin policy to protect them. | |
| 463 | +- **Uncaught failures**: the runtime turns exceptions into a 500 itself, but a fatal error or `exit()` ends the Warm Process, and Scaleway answers a failed Invocation with a `500` whose body is `Error while executing function`. | |
| 423 | 464 | |
| 424 | 465 | ### Client IP and trusted proxies |
| 425 | 466 | |
| 426 | -The `Request` always sees `127.0.0.1` as its peer, standing for the platform proxy, which the runtime trusts for `X-Forwarded-For`, `-Host`, `-Port` and `-Proto`. | |
| 427 | -If your application configures its own trusted proxies (`framework.trusted_proxies`), the runtime leaves them alone: include `127.0.0.1`, or `getClientIp()`, `getHost()` and `isSecure()` will ignore the platform's headers. | |
| 467 | +The runtime trusts no proxy, as clients can forge most forwarding headers on Scaleway ([ADR 0007](docs/adr/0007-trust-only-headers-the-platform-overwrites.md)): | |
| 468 | + | |
| 469 | +| Request | From | Why | | |
| 470 | +| --- | --- | --- | | |
| 471 | +| `getClientIp()` | `X-Real-Ip`, `127.0.0.1` without it (CRON Invocations) | Set by the platform, whatever the client sends | | |
| 472 | +| `isSecure()`, `getScheme()`, port | `X-Forwarded-Proto`, the scheme's default port | Set by the platform (`https`, `http` for CRON Invocations) | | |
| 473 | +| `getHost()` | The [`host` option](#host) | No `Host` header reaches the function | | |
| 474 | + | |
| 475 | +`X-Forwarded-For` (the client's value is kept in front), `X-Forwarded-Host`, `X-Forwarded-Port` and `Forwarded` are passed through from the client: they reach the Application as plain headers, and nothing trusts them. | |
| 428 | 476 | |
| 429 | 477 | > [!CAUTION] |
| 430 | -> Whether Scaleway overwrites, appends to or passes through the `X-Forwarded-*` headers a client sends is not verified yet. | |
| 431 | -> If it passes them through, a client can choose the IP returned by `getClientIp()` and the host used to generate absolute URLs. | |
| 432 | -> Do not rely on them for access control or rate limiting until the probe settles it. | |
| 478 | +> Do not configure `framework.trusted_proxies`: the client is already resolved, and trusting `REMOTE_ADDR` or private ranges would make Symfony trust those forgeable headers again. | |
| 479 | +> Trusted proxies your application configures still apply, as usual. | |
| 433 | 480 | |
| 434 | 481 | ### Composer at deploy time |
| 435 | 482 | |
| 436 | -Scaleway documents that it installs the dependencies of a shipped `composer.json` at deploy time. | |
| 437 | -Whether it does so over the shipped `vendor/` is not verified: if it does, a `composer.lock` installing packages from a `path` repository outside the zip, as the example's does, likely fails the deployment. | |
| 483 | +Scaleway runs `composer install --no-dev` in the function's directory when the zip holds a `composer.json` at its root, replacing any shipped `vendor/`. | |
| 484 | +Every locked package must therefore be downloadable from Scaleway, and the function's environment variables are not available to Composer scripts. | |
| 485 | +A failing install or script does not fail the deployment: the function becomes `ready` anyway, so check its first Invocations after a deploy. | |
| 438 | 486 | |
| 439 | 487 | ### Windows |
| 440 | 488 | |
| 441 | -`scaleway-serve` and `scaleway-package` rely on Unix sockets and `/dev/null`: they run on macOS and Linux, not on Windows outside WSL. | |
| 489 | +`scaleway-serve` relies on Unix sockets: it runs on macOS and Linux, not on Windows outside WSL, and `scaleway-package` is only tested on macOS and Linux. | |
| 442 | 490 | Without the `pcntl` extension, stopping the Emulator with `kill` may leave its PHP processes running. |
| 443 | 491 | |
| 444 | -### Unverified platform facts | |
| 492 | +### Not measured | |
| 445 | 493 | |
| 446 | -The runtime and the Emulator rely on these, still unmeasured on Scaleway. | |
| 447 | -The [probe function](probe) measures each of them; until its results are folded into this README, treat them as assumptions. | |
| 494 | +The [probe](probe) settled every platform fact the runtime relies on ([results](docs/probe-results.md)), except: | |
| 448 | 495 | |
| 449 | -- The CRON Event shape: a `POST /` whose body is the trigger's arguments as JSON, and whether it carries a marker HTTP clients cannot forge. | |
| 450 | -- That processes are kept warm between Invocations. | |
| 451 | -- How `X-Forwarded-*` headers sent by clients are handled. | |
| 452 | -- How repeated query keys are folded, and the case of header names. | |
| 453 | -- That multi-value response headers (lists, as for several `Set-Cookie`) are sent as repeated headers. | |
| 454 | -- When request bodies are base64-encoded, and the actual body size limit. | |
| 455 | -- The Content-Type added to responses without one, and how binary responses are served. | |
| 456 | -- The type and content of `$context`. | |
| 457 | -- That plain and secret environment variables reach `$_SERVER`, where `APP_RUNTIME_OPTIONS`, `APP_ENV` and `APP_DEBUG` are read. | |
| 458 | -- That the code directory is read-only and `/tmp` writable. | |
| 459 | -- Whether Composer runs over the shipped `vendor/` at deploy time. | |
| 460 | -- Where stdout, stderr and uncaught exceptions go. | |
| 496 | +- where stdout, stderr and uncaught exceptions are logged (Cockpit); | |
| 497 | +- whether a deployment whose Composer install or script fails serves the new code or keeps the previous version. | |
| 461 | 498 | |
| 462 | 499 | ## Contributing |
| 463 | 500 | |
| @@ -468,14 +505,15 @@ composer cs-fix # applies the coding style | ||
| 468 | 505 | ``` |
| 469 | 506 | |
| 470 | 507 | CI runs `composer validate --strict` and `composer check` on PHP 8.4 and 8.5, each against Symfony 7.4 and 8.x, the whole Symfony stack being pinned through Flex's `SYMFONY_REQUIRE`. |
| 471 | -It also packages the [example application](example), serves its zip read-only in prod through the Emulator, and runs its [end-to-end scenarios](example/e2e/run.sh). | |
| 508 | +It also runs the [end-to-end scenarios](example/e2e/run.sh) of the [example application](example) through the Emulator, in dev and on its zip installed like Scaleway does ([e2e/deploy-locally.sh](example/e2e/deploy-locally.sh)). | |
| 509 | +The example installs the runtime of the checkout with `composer link-runtime`, see its [README](example/README.md#run-it-locally). | |
| 472 | 510 | |
| 473 | 511 | The repository holds: |
| 474 | 512 | |
| 475 | 513 | - `src/`, `bin/`, `resources/`: the package; |
| 476 | -- `tests/`: PHPUnit tests, packaging included (it runs Composer, offline); | |
| 477 | -- `example/`: a demo Symfony application consuming the package through a `path` repository, with Terraform code to deploy it; | |
| 478 | -- `probe/`: a dependency-free function measuring the open platform facts; | |
| 514 | +- `tests/`: PHPUnit tests, packaging included; | |
| 515 | +- `example/`: a demo Symfony application requiring the package, with Terraform code to deploy it; | |
| 516 | +- `probe/`: a dependency-free function measuring the platform facts, and its [results](docs/probe-results.md); | |
| 479 | 517 | - `docs/`: the [implementation plan](docs/plan.md) and the [ADRs](docs/adr). |
| 480 | 518 | |
| 481 | 519 | ## License |
| @@ -9,8 +9,8 @@ It also ships the tools to package the function and to run it locally. | |||
| 9 | Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOSSARY.md), design decisions in [docs/adr](docs/adr). | 9 | Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOSSARY.md), design decisions in [docs/adr](docs/adr). |
| 10 | 10 | ||
| 11 | > [!IMPORTANT] | 11 | > [!IMPORTANT] |
| 12 | -> The runtime has only been run locally, through the Emulator. | 12 | +> The platform facts the runtime relies on were measured on Scaleway with the [probe](probe), see [docs/probe-results.md](docs/probe-results.md). |
| 13 | -> Several platform facts it relies on are still assumptions, see [Unverified platform facts](#unverified-platform-facts). | 13 | +> The runtime itself has only run through the Emulator so far, and the package is not on Packagist yet. |
| 14 | 14 | ||
| 15 | ## Contents | 15 | ## Contents |
| 16 | 16 | ||
| @@ -30,7 +30,7 @@ Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOS | |||
| 30 | 30 | ||
| 31 | - The Scaleway PHP runtimes `php84` or `php85` (PHP 8.4 or later). | 31 | - The Scaleway PHP runtimes `php84` or `php85` (PHP 8.4 or later). |
| 32 | - Symfony 7.4 or 8.x components (`symfony/runtime`, `symfony/http-kernel`, `symfony/http-foundation`). | 32 | - Symfony 7.4 or 8.x components (`symfony/runtime`, `symfony/http-kernel`, `symfony/http-foundation`). |
| 33 | -- To build the deployment zip: the `zip` PHP extension and `composer` on the `PATH`. | 33 | +- To build the deployment zip: the `zip` PHP extension. |
| 34 | - macOS or Linux for the packaging and Emulator tools (see [Windows](#windows)). | 34 | - macOS or Linux for the packaging and Emulator tools (see [Windows](#windows)). |
| 35 | 35 | ||
| 36 | ## Installation | 36 | ## Installation |
| @@ -97,6 +97,7 @@ Set these on the function, in production: | |||
| 97 | | Variable | Kind | Why | | 97 | | Variable | Kind | Why | |
| 98 | | --- | --- | --- | | 98 | | --- | --- | --- | |
| 99 | | `APP_SECRET` | Secret | Symfony's `kernel.secret`, used to sign URLs, cookies and CSRF tokens. | | 99 | | `APP_SECRET` | Secret | Symfony's `kernel.secret`, used to sign URLs, cookies and CSRF tokens. | |
| 100 | +| `SCALEWAY_HOST` | Plain | The public host of the function, for absolute URLs, see [Host](#host). | | ||
| 100 | | `SCALEWAY_CRON_SECRET` | Secret | Only to run console commands from CRON triggers, see [Command Invocations](#command-invocations). | | 101 | | `SCALEWAY_CRON_SECRET` | Secret | Only to run console commands from CRON triggers, see [Command Invocations](#command-invocations). | |
| 101 | | `APP_ENV` | Plain | Only to run another env than `prod`, see below. | | 102 | | `APP_ENV` | Plain | Only to run another env than `prod`, see below. | |
| 102 | 103 | ||
| @@ -109,13 +110,43 @@ The Emulator runs `dev` instead, unless it is started with an `APP_ENV`. | |||
| 109 | For every other variable, real environment variables win over `.env` files, as usual with Symfony's Dotenv. | 110 | For every other variable, real environment variables win over `.env` files, as usual with Symfony's Dotenv. |
| 110 | `.env.local`, `.env.local.php` and `.env.*.local` are never shipped. | 111 | `.env.local`, `.env.local.php` and `.env.*.local` are never shipped. |
| 111 | 112 | ||
| 112 | -### Writable directories | 113 | +### Host |
| 114 | + | ||
| 115 | +No `Host` header reaches the function, so the runtime cannot know the domain it is called on. | ||
| 116 | +Every Request gets the host of the `host` runtime option, which defaults to the `SCALEWAY_HOST` environment variable, then to `localhost`: | ||
| 117 | + | ||
| 118 | +```sh | ||
| 119 | +SCALEWAY_HOST=api.example.com | ||
| 120 | +``` | ||
| 121 | + | ||
| 122 | +The scheme comes from the platform's `X-Forwarded-Proto` (`https` for HTTP calls), the port is the scheme's default. | ||
| 123 | +Absolute URLs (`UrlGeneratorInterface::ABSOLUTE_URL`, signed URLs, redirects built from the Request) are only right once the host is set. | ||
| 124 | +Set it to the function's domain (its `domain_name` on Scaleway) or to its custom domain. | ||
| 125 | +With Terraform, a function cannot reference its own `domain_name` in its variables (the reference would be a cycle): use a custom domain, or set the variable after a first apply. | ||
| 126 | + | ||
| 127 | +### Cache and writable directories | ||
| 128 | + | ||
| 129 | +The code directory is writable on Scaleway, so the Kernel builds its cache in `var/cache` at the first Invocation of each Warm Process, which pays the container compilation once. | ||
| 130 | +To pay it at deploy time instead, warm the cache from a Composer script, which Scaleway runs after installing the dependencies: | ||
| 131 | + | ||
| 132 | +```json | ||
| 133 | +{ | ||
| 134 | + "scripts": { | ||
| 135 | + "post-install-cmd": ["@php bin/console cache:warmup --env=prod"] | ||
| 136 | + } | ||
| 137 | +} | ||
| 138 | +``` | ||
| 139 | + | ||
| 140 | +Only do it when the warmup needs no secret: the function's environment variables are not available at deploy time, and a failing script does not fail the deployment, the first Invocation then building the cache as usual. | ||
| 141 | + | ||
| 142 | +> [!WARNING] | ||
| 143 | +> Symfony Flex's `auto-scripts` also run at deploy time, with the `APP_ENV` of the shipped `.env`, usually `dev`: `cache:clear` then fails silently, its dev bundles not being installed with `--no-dev`. | ||
| 144 | +> Make them env-explicit (`"cache:clear --env=prod": "symfony-cmd"`), or remove `cache:clear` from them and warm the cache as above. | ||
| 113 | 145 | ||
| 114 | -The deployed code may be read-only. | ||
| 115 | When the project's `var/` directory (or the project dir, without `var/`) is not writable, the runtime sets, unless your application already did: | 146 | When the project's `var/` directory (or the project dir, without `var/`) is not writable, the runtime sets, unless your application already did: |
| 116 | 147 | ||
| 117 | -- `APP_BUILD_DIR` to `var/cache`, so the container warmed in the zip is used as is; | 148 | +- `APP_CACHE_DIR` to `<temp dir>/cache` and `APP_LOG_DIR` to `<temp dir>/log`; |
| 118 | -- `APP_CACHE_DIR` to `<temp dir>/cache` and `APP_LOG_DIR` to `<temp dir>/log`. | 149 | +- `APP_BUILD_DIR` to `var/cache`, only when `var/cache/<env>` holds a container warmed before, so that it is used as is. |
| 119 | 150 | ||
| 120 | Kernels using `MicroKernelTrait` read these variables. | 151 | Kernels using `MicroKernelTrait` read these variables. |
| 121 | Other Kernels must override `getCacheDir()`, `getBuildDir()` and `getLogDir()` themselves. | 152 | Other Kernels must override `getCacheDir()`, `getBuildDir()` and `getLogDir()` themselves. |
| @@ -195,9 +226,9 @@ require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | |||
| 195 | return static fn (): RequestHandlerInterface => new ApiHandler(); | 226 | return static fn (): RequestHandlerInterface => new ApiHandler(); |
| 196 | ``` | 227 | ``` |
| 197 | 228 | ||
| 198 | -The PSR-7 request carries the client address, scheme, host and port resolved from the platform's `X-Forwarded-*` headers, in its URI and in the `REMOTE_ADDR`, `HTTPS`, `HTTP_HOST` and `SERVER_PORT` server params. | 229 | +The PSR-7 request carries the client address, scheme, host and port the runtime resolved (see [Client IP and trusted proxies](#client-ip-and-trusted-proxies)), in its URI and in the `REMOTE_ADDR`, `HTTPS`, `HTTP_HOST` and `SERVER_PORT` server params. |
| 199 | A handler implementing `Symfony\Contracts\Service\ResetInterface` is reset before every Invocation but the first. | 230 | A handler implementing `Symfony\Contracts\Service\ResetInterface` is reset before every Invocation but the first. |
| 200 | -A malformed `Host` or `X-Forwarded-Host` is answered with a 400 before reaching the handler. | 231 | +A malformed host is answered with a 400 before reaching the handler. |
| 201 | 232 | ||
| 202 | ### Console Application or Command | 233 | ### Console Application or Command |
| 203 | 234 | ||
| @@ -253,7 +284,7 @@ An invokable object is not taken for a raw callable; return `$object(...)` to ma | |||
| 253 | 284 | ||
| 254 | ### CRON triggers | 285 | ### CRON triggers |
| 255 | 286 | ||
| 256 | -A CRON trigger is assumed to call the Application with a `POST /` whose JSON body holds the trigger's arguments: Scaleway does not document the CRON Event, and the [probe](probe) checks it. | 287 | +A CRON trigger calls the Application with a `POST /` whose JSON body holds the trigger's arguments, over plain `http` and without client address (`getClientIp()` is `127.0.0.1`). |
| 257 | With a Kernel, handle it like any route: | 288 | With a Kernel, handle it like any route: |
| 258 | 289 | ||
| 259 | ```php | 290 | ```php |
| @@ -265,6 +296,8 @@ public function cron(Request $request): Response | |||
| 265 | } | 296 | } |
| 266 | ``` | 297 | ``` |
| 267 | 298 | ||
| 299 | +Any HTTP client can call `POST /` too: tell CRON Invocations apart with a secret of your own in the arguments, or run a command instead. | ||
| 300 | + | ||
| 268 | ### Command Invocations | 301 | ### Command Invocations |
| 269 | 302 | ||
| 270 | A CRON trigger may run a console command instead, through the Console Application of the same Kernel. | 303 | A CRON trigger may run a console command instead, through the Console Application of the same Kernel. |
| @@ -290,10 +323,14 @@ Services are reset around each command, so it shares no state with the Invocatio | |||
| 290 | 323 | ||
| 291 | ### Security model | 324 | ### Security model |
| 292 | 325 | ||
| 293 | -Nothing known in an Event tells a CRON Invocation from an HTTP one, so any internet client can send a body naming a command to a public function. | 326 | +A body only becomes a Command Invocation when both hold: |
| 294 | -A body only becomes a Command Invocation when its `secret` matches `SCALEWAY_CRON_SECRET`, compared in constant time. | 327 | + |
| 295 | -Otherwise the body reaches the Application as a plain HTTP body on `POST /`, and a body carrying a wrong `secret` is logged (without its value). | 328 | +- the Event carries neither `X-Real-Ip` nor `X-Envoy-External-Address`, which the platform sets on every HTTP call, overwriting what the client sends, and which CRON Events lack; |
| 296 | -Without `SCALEWAY_CRON_SECRET`, or with an empty one, Command Invocations are off. | 329 | +- its `secret` matches `SCALEWAY_CRON_SECRET`, compared in constant time. |
| 330 | + | ||
| 331 | +Otherwise the body reaches the Application as a plain HTTP body on `POST /`, and a rejected body carrying a `secret` is logged (without its value). | ||
| 332 | +So no HTTP call can run a command, even with the secret, and without `SCALEWAY_CRON_SECRET`, or with an empty one, Command Invocations are off. | ||
| 333 | +The first check relies on undocumented platform behaviour, which is why the secret stays required. | ||
| 297 | 334 | ||
| 298 | Keep in mind that: | 335 | Keep in mind that: |
| 299 | 336 | ||
| @@ -308,38 +345,36 @@ See [ADR 0005](docs/adr/0005-authenticate-command-invocations-with-a-shared-secr | |||
| 308 | vendor/bin/scaleway-package [function.zip] | 345 | vendor/bin/scaleway-package [function.zip] |
| 309 | ``` | 346 | ``` |
| 310 | 347 | ||
| 311 | -It builds the deployment zip in a private temporary directory, removed afterwards whatever happens: | 348 | +It zips your application as it is, `composer.json` and `composer.lock` included, but without `vendor/` nor `var/`: Scaleway runs `composer install --no-dev` from the shipped lock at deploy time, Composer scripts included, and replaces any shipped `vendor/` ([ADR 0008](docs/adr/0008-let-scaleway-install-the-dependencies.md)). |
| 349 | +Executable bits are kept, and symbolic links are followed: their target is zipped. | ||
| 312 | 350 | ||
| 313 | -1. It copies the application, without the excluded files below and without the zip being written. | 351 | +It refuses to build the zip, as Scaleway would install something else or fail without failing the deployment, when: |
| 314 | -2. It runs `composer install --no-dev --classmap-authoritative` from the committed `composer.lock`, with `APP_ENV=prod` and `APP_DEBUG=0`. | ||
| 315 | - Relative `path` repositories are resolved against your application, and mirrored instead of symlinked. | ||
| 316 | -3. It runs `bin/console cache:warmup`, when `bin/console` exists, to ship a warmed prod cache. | ||
| 317 | -4. It zips the result, keeping the executable bits, and the `composer.lock` exactly as committed. | ||
| 318 | 352 | ||
| 319 | -It requires a committed `composer.lock`, for reproducible builds, and `handler.php`. | 353 | +- `handler.php` or `composer.lock` is missing; |
| 320 | -Symbolic links are followed: their target is copied. | 354 | +- `composer.lock` is not up to date with `composer.json`; |
| 355 | +- `composer.json` declares a `path` repository, or `composer.lock` installs a package from a local path: Scaleway cannot resolve them. | ||
| 321 | 356 | ||
| 322 | | Shipped | Not shipped | | 357 | | Shipped | Not shipped | |
| 323 | | --- | --- | | 358 | | --- | --- | |
| 324 | -| Your code, `composer.json`, `composer.lock`, `handler.php` | `vendor/` and `var/` from your machine (rebuilt) | | 359 | +| Your code, `composer.json`, `composer.lock`, `handler.php` | `vendor/` and `var/` (installed and built on the platform) | |
| 325 | -| `vendor/` without dev dependencies | `tests/`, `phpunit.xml(.dist)`, `.phpunit.cache`, `.phpunit.result.cache`, `.php-cs-fixer.cache` | | 360 | +| `.env`, `.env.prod`... as they are | `tests/`, `phpunit.xml(.dist)`, `.phpunit.cache`, `.phpunit.result.cache`, `.php-cs-fixer.cache` | |
| 326 | -| `var/cache/prod`, warmed | `.env.local`, `.env.local.php`, `.env.*.local` | | 361 | +| | `.env.local`, `.env.local.php`, `.env.*.local` | |
| 327 | -| `.env`, `.env.prod`... as they are | `.git`, `.idea`, `.vscode`, `.DS_Store`, `node_modules` anywhere | | 362 | +| | `.git`, `.idea`, `.vscode`, `.DS_Store`, `node_modules` anywhere | |
| 328 | | | `.terraform`, `*.tfstate`, `*.tfstate.*` anywhere (Terraform state holds secrets in clear) | | 363 | | | `.terraform`, `*.tfstate`, `*.tfstate.*` anywhere (Terraform state holds secrets in clear) | |
| 329 | 364 | ||
| 330 | The full list is `Packager::EXCLUDES` in [src/Packager.php](src/Packager.php). | 365 | The full list is `Packager::EXCLUDES` in [src/Packager.php](src/Packager.php). |
| 331 | -`--classmap-authoritative` means only classes known at build time can be autoloaded. | ||
| 332 | 366 | ||
| 333 | Scaleway accepts zips up to 100 MiB, and up to 500 MiB of unzipped code ([limits](https://www.scaleway.com/en/docs/serverless-functions/reference-content/functions-limitations/)). | 367 | Scaleway accepts zips up to 100 MiB, and up to 500 MiB of unzipped code ([limits](https://www.scaleway.com/en/docs/serverless-functions/reference-content/functions-limitations/)). |
| 334 | -`scaleway-package` warns when the zip exceeds either, but still writes it. | 368 | +`scaleway-package` warns when the zip or its content exceeds either, but still writes it. |
| 335 | 369 | ||
| 336 | ## Deployment | 370 | ## Deployment |
| 337 | 371 | ||
| 338 | -[example/terraform](example/terraform/main.tf) deploys the [example application](example) with its `php85` runtime, `handler.handle`, `APP_ENV=prod`, the `APP_SECRET` and `SCALEWAY_CRON_SECRET` secrets, a CRON trigger calling `POST /` and one running a command. | 372 | +[example/terraform](example/terraform/main.tf) deploys the [example application](example) with its `php85` runtime, `handler.handle`, `APP_ENV=prod`, the `APP_SECRET` and `SCALEWAY_CRON_SECRET` secrets, an optional `SCALEWAY_HOST`, a CRON trigger calling `POST /` and one running a command. |
| 339 | Its [README](example/README.md#deploy-it) gives the commands. | 373 | Its [README](example/README.md#deploy-it) gives the commands. |
| 340 | -That Terraform code was written against the `scaleway/scaleway` provider v2.92 documentation and has never been applied. | 374 | +That Terraform code was written against the `scaleway/scaleway` provider v2.92 documentation and has never been applied; the [probe](probe/terraform/main.tf)'s, written the same way, has. |
| 341 | 375 | ||
| 342 | Redeploy by running `scaleway-package` again, then `terraform apply`. | 376 | Redeploy by running `scaleway-package` again, then `terraform apply`. |
| 377 | +Replacing a function, for instance to change its `runtime`, deletes its CRON triggers on Scaleway's side: give them a `lifecycle { replace_triggered_by = [scaleway_function.<name>.id] }`, as the example does, so the same apply recreates them. | ||
| 343 | 378 | ||
| 344 | ## Local development with the Emulator | 379 | ## Local development with the Emulator |
| 345 | 380 | ||
| @@ -353,40 +388,44 @@ It sets `APP_ENV=dev` unless started with an `APP_ENV`, as the deployed function | |||
| 353 | A crashed process is replaced by a fresh one, as Scaleway starts a new instance. | 388 | A crashed process is replaced by a fresh one, as Scaleway starts a new instance. |
| 354 | 389 | ||
| 355 | PHP's built-in server receives the requests, and forwards the Events to that process over a Unix socket in a private temporary directory. | 390 | PHP's built-in server receives the requests, and forwards the Events to that process over a Unix socket in a private temporary directory. |
| 356 | -The context passed to `handle()` is an empty array. | ||
| 357 | 391 | ||
| 358 | -The Event is built the way the platform is assumed to build it: | 392 | +The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)): |
| 359 | 393 | ||
| 360 | -- header names in `Title-Case`, repeated headers joined with `, `; | 394 | +- the `httpMethod`, `queryStringParameters` (the last value of a repeated key winning, `[]` without query string) and other keys of the platform; |
| 361 | -- the client address appended to `X-Forwarded-For`, and `X-Forwarded-Proto: http`; | 395 | +- header names in `Title-Case`, repeated headers joined with `, `, no `Host`; |
| 362 | -- `queryStringParameters` as a map of strings, the last value of a repeated key winning; | 396 | +- `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through; |
| 363 | -- bodies that are not valid UTF-8 base64-encoded, with `isBase64Encoded: true`. | 397 | +- empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag. |
| 364 | 398 | ||
| 365 | -Simulate a CRON trigger with a `POST /` carrying its arguments as JSON body: | 399 | +So the Requests are `https`, and their host is `localhost` unless you set `SCALEWAY_HOST`, as on the platform. |
| 400 | + | ||
| 401 | +No HTTP call can be a CRON Invocation, so the Emulator turns a `POST` on its reserved `/_scaleway/cron` path into a CRON Event whose arguments are the request body: | ||
| 366 | 402 | ||
| 367 | ```sh | 403 | ```sh |
| 368 | -curl -X POST http://127.0.0.1:8000/ -H 'Content-Type: application/json' --data '{"job": "cleanup"}' | 404 | +curl -X POST http://127.0.0.1:8000/_scaleway/cron --data '{"job": "cleanup"}' |
| 369 | ``` | 405 | ``` |
| 370 | 406 | ||
| 371 | Simulate a Command Invocation by starting the Emulator with `SCALEWAY_CRON_SECRET` set, and sending the [Command Invocation body](#command-invocations) the same way: | 407 | Simulate a Command Invocation by starting the Emulator with `SCALEWAY_CRON_SECRET` set, and sending the [Command Invocation body](#command-invocations) the same way: |
| 372 | 408 | ||
| 373 | ```sh | 409 | ```sh |
| 374 | SCALEWAY_CRON_SECRET=local-secret vendor/bin/scaleway-serve | 410 | SCALEWAY_CRON_SECRET=local-secret vendor/bin/scaleway-serve |
| 375 | -curl -X POST http://127.0.0.1:8000/ --data '{"command": "app:greet", "args": {"name": "Scaleway"}, "secret": "local-secret"}' | 411 | +curl -X POST http://127.0.0.1:8000/_scaleway/cron --data '{"command": "app:greet", "args": {"name": "Scaleway"}, "secret": "local-secret"}' |
| 376 | ``` | 412 | ``` |
| 377 | 413 | ||
| 378 | -To run exactly what gets deployed, serve the packaged zip from a read-only copy, in `prod`: | 414 | +To run what gets deployed, install the zip the way Scaleway does, without the function's variables, then serve it in `prod`: |
| 379 | 415 | ||
| 380 | ```sh | 416 | ```sh |
| 381 | vendor/bin/scaleway-package | 417 | vendor/bin/scaleway-package |
| 382 | -unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function | 418 | +unzip -q function.zip -d /tmp/function |
| 419 | +composer install --working-dir=/tmp/function --no-dev | ||
| 383 | APP_ENV=prod APP_SECRET=local php /tmp/function/vendor/bin/scaleway-serve | 420 | APP_ENV=prod APP_SECRET=local php /tmp/function/vendor/bin/scaleway-serve |
| 384 | ``` | 421 | ``` |
| 385 | 422 | ||
| 423 | +The [example](example/README.md#run-it-locally)'s `e2e/deploy-locally.sh` does the same with the runtime of this repository. | ||
| 424 | + | ||
| 386 | ## Warm Processes | 425 | ## Warm Processes |
| 387 | 426 | ||
| 388 | The Application is built once per Warm Process and reused by the following Invocations, like with FrankenPHP's worker mode or RoadRunner ([ADR 0002](docs/adr/0002-reuse-application-across-invocations.md)). | 427 | The Application is built once per Warm Process and reused by the following Invocations, like with FrankenPHP's worker mode or RoadRunner ([ADR 0002](docs/adr/0002-reuse-application-across-invocations.md)). |
| 389 | -If Scaleway does not keep processes warm, this degrades to one boot per Invocation. | 428 | +Scaleway keeps processes warm: consecutive Invocations of an instance run in the same PHP process. |
| 390 | 429 | ||
| 391 | Between Invocations, for a Kernel: | 430 | Between Invocations, for a Kernel: |
| 392 | 431 | ||
| @@ -408,56 +447,54 @@ Everything else survives, so your application must be safe for long-running exec | |||
| 408 | - **Superglobals**: `$_GET`, `$_POST`, `$_COOKIE`, `$_FILES` and the request entries of `$_SERVER` (`REQUEST_URI`, `HTTP_*`...) are not populated per Invocation. | 447 | - **Superglobals**: `$_GET`, `$_POST`, `$_COOKIE`, `$_FILES` and the request entries of `$_SERVER` (`REQUEST_URI`, `HTTP_*`...) are not populated per Invocation. |
| 409 | Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation. | 448 | Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation. |
| 410 | - **Output**: only the returned `Response` reaches the client. | 449 | - **Output**: only the returned `Response` reaches the client. |
| 411 | - `echo` outside the Response, `header()` and `http_response_code()` do not. | 450 | + `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body. |
| 412 | -- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, likely reaches the Application with one value only. | 451 | +- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only. |
| 413 | Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests. | 452 | Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests. |
| 414 | - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does. | 453 | - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does. |
| 415 | Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not. | 454 | Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not. |
| 416 | - **Cookies**: request cookie names are taken verbatim: unlike PHP's `$_COOKIE`, dots and spaces are not turned into underscores and `a[b]` does not nest. | 455 | - **Cookies**: request cookie names are taken verbatim: unlike PHP's `$_COOKIE`, dots and spaces are not turned into underscores and `a[b]` does not nest. |
| 417 | - **Streaming**: `StreamedResponse` and `BinaryFileResponse` are buffered into one Function Response; Server-Sent Events and long downloads are not possible. | 456 | - **Streaming**: `StreamedResponse` and `BinaryFileResponse` are buffered into one Function Response; Server-Sent Events and long downloads are not possible. |
| 418 | - Bodies that are not valid UTF-8 are sent base64-encoded. | 457 | + Bodies that are not valid UTF-8 are sent base64-encoded, and decoded by the platform. |
| 419 | - **PSR-15 responses** go through Symfony's `Response`, which adds `Cache-Control: no-cache, private` and `Date` when they are missing. | 458 | - **PSR-15 responses** go through Symfony's `Response`, which adds `Cache-Control: no-cache, private` and `Date` when they are missing. |
| 420 | -- **HEAD requests**: only FrameworkBundle's `ResponseListener` strips the body of a `HEAD` response. | 459 | +- **HEAD requests**: only FrameworkBundle's `ResponseListener` strips the body of a `HEAD` response, but the platform sends none anyway. |
| 421 | - A bare `HttpKernelInterface`, a Response Front Controller or a PSR-15 handler returns the full body, unless it handles `HEAD` itself. | 460 | +- **Request size**: Scaleway rejects request bodies over 6 MiB with a `413`, before they reach the function. |
| 422 | -- **Request size**: Scaleway documents a 6 MiB limit for request bodies. | 461 | +- **CORS**: Scaleway adds `Access-Control-Allow-Origin: *` and `Access-Control-Allow-Headers: Content-Type` to every response, whatever the Application sends. |
| 462 | + Any website can therefore read the responses of a public function from a browser, though without the user's cookies: do not rely on the same-origin policy to protect them. | ||
| 463 | +- **Uncaught failures**: the runtime turns exceptions into a 500 itself, but a fatal error or `exit()` ends the Warm Process, and Scaleway answers a failed Invocation with a `500` whose body is `Error while executing function`. | ||
| 423 | 464 | ||
| 424 | ### Client IP and trusted proxies | 465 | ### Client IP and trusted proxies |
| 425 | 466 | ||
| 426 | -The `Request` always sees `127.0.0.1` as its peer, standing for the platform proxy, which the runtime trusts for `X-Forwarded-For`, `-Host`, `-Port` and `-Proto`. | 467 | +The runtime trusts no proxy, as clients can forge most forwarding headers on Scaleway ([ADR 0007](docs/adr/0007-trust-only-headers-the-platform-overwrites.md)): |
| 427 | -If your application configures its own trusted proxies (`framework.trusted_proxies`), the runtime leaves them alone: include `127.0.0.1`, or `getClientIp()`, `getHost()` and `isSecure()` will ignore the platform's headers. | 468 | + |
| 469 | +| Request | From | Why | | ||
| 470 | +| --- | --- | --- | | ||
| 471 | +| `getClientIp()` | `X-Real-Ip`, `127.0.0.1` without it (CRON Invocations) | Set by the platform, whatever the client sends | | ||
| 472 | +| `isSecure()`, `getScheme()`, port | `X-Forwarded-Proto`, the scheme's default port | Set by the platform (`https`, `http` for CRON Invocations) | | ||
| 473 | +| `getHost()` | The [`host` option](#host) | No `Host` header reaches the function | | ||
| 474 | + | ||
| 475 | +`X-Forwarded-For` (the client's value is kept in front), `X-Forwarded-Host`, `X-Forwarded-Port` and `Forwarded` are passed through from the client: they reach the Application as plain headers, and nothing trusts them. | ||
| 428 | 476 | ||
| 429 | > [!CAUTION] | 477 | > [!CAUTION] |
| 430 | -> Whether Scaleway overwrites, appends to or passes through the `X-Forwarded-*` headers a client sends is not verified yet. | 478 | +> Do not configure `framework.trusted_proxies`: the client is already resolved, and trusting `REMOTE_ADDR` or private ranges would make Symfony trust those forgeable headers again. |
| 431 | -> If it passes them through, a client can choose the IP returned by `getClientIp()` and the host used to generate absolute URLs. | 479 | +> Trusted proxies your application configures still apply, as usual. |
| 432 | -> Do not rely on them for access control or rate limiting until the probe settles it. | ||
| 433 | 480 | ||
| 434 | ### Composer at deploy time | 481 | ### Composer at deploy time |
| 435 | 482 | ||
| 436 | -Scaleway documents that it installs the dependencies of a shipped `composer.json` at deploy time. | 483 | +Scaleway runs `composer install --no-dev` in the function's directory when the zip holds a `composer.json` at its root, replacing any shipped `vendor/`. |
| 437 | -Whether it does so over the shipped `vendor/` is not verified: if it does, a `composer.lock` installing packages from a `path` repository outside the zip, as the example's does, likely fails the deployment. | 484 | +Every locked package must therefore be downloadable from Scaleway, and the function's environment variables are not available to Composer scripts. |
| 485 | +A failing install or script does not fail the deployment: the function becomes `ready` anyway, so check its first Invocations after a deploy. | ||
| 438 | 486 | ||
| 439 | ### Windows | 487 | ### Windows |
| 440 | 488 | ||
| 441 | -`scaleway-serve` and `scaleway-package` rely on Unix sockets and `/dev/null`: they run on macOS and Linux, not on Windows outside WSL. | 489 | +`scaleway-serve` relies on Unix sockets: it runs on macOS and Linux, not on Windows outside WSL, and `scaleway-package` is only tested on macOS and Linux. |
| 442 | Without the `pcntl` extension, stopping the Emulator with `kill` may leave its PHP processes running. | 490 | Without the `pcntl` extension, stopping the Emulator with `kill` may leave its PHP processes running. |
| 443 | 491 | ||
| 444 | -### Unverified platform facts | 492 | +### Not measured |
| 445 | 493 | ||
| 446 | -The runtime and the Emulator rely on these, still unmeasured on Scaleway. | 494 | +The [probe](probe) settled every platform fact the runtime relies on ([results](docs/probe-results.md)), except: |
| 447 | -The [probe function](probe) measures each of them; until its results are folded into this README, treat them as assumptions. | ||
| 448 | 495 | ||
| 449 | -- The CRON Event shape: a `POST /` whose body is the trigger's arguments as JSON, and whether it carries a marker HTTP clients cannot forge. | 496 | +- where stdout, stderr and uncaught exceptions are logged (Cockpit); |
| 450 | -- That processes are kept warm between Invocations. | 497 | +- whether a deployment whose Composer install or script fails serves the new code or keeps the previous version. |
| 451 | -- How `X-Forwarded-*` headers sent by clients are handled. | ||
| 452 | -- How repeated query keys are folded, and the case of header names. | ||
| 453 | -- That multi-value response headers (lists, as for several `Set-Cookie`) are sent as repeated headers. | ||
| 454 | -- When request bodies are base64-encoded, and the actual body size limit. | ||
| 455 | -- The Content-Type added to responses without one, and how binary responses are served. | ||
| 456 | -- The type and content of `$context`. | ||
| 457 | -- That plain and secret environment variables reach `$_SERVER`, where `APP_RUNTIME_OPTIONS`, `APP_ENV` and `APP_DEBUG` are read. | ||
| 458 | -- That the code directory is read-only and `/tmp` writable. | ||
| 459 | -- Whether Composer runs over the shipped `vendor/` at deploy time. | ||
| 460 | -- Where stdout, stderr and uncaught exceptions go. | ||
| 461 | 498 | ||
| 462 | ## Contributing | 499 | ## Contributing |
| 463 | 500 | ||
| @@ -468,14 +505,15 @@ composer cs-fix # applies the coding style | |||
| 468 | ``` | 505 | ``` |
| 469 | 506 | ||
| 470 | CI runs `composer validate --strict` and `composer check` on PHP 8.4 and 8.5, each against Symfony 7.4 and 8.x, the whole Symfony stack being pinned through Flex's `SYMFONY_REQUIRE`. | 507 | CI runs `composer validate --strict` and `composer check` on PHP 8.4 and 8.5, each against Symfony 7.4 and 8.x, the whole Symfony stack being pinned through Flex's `SYMFONY_REQUIRE`. |
| 471 | -It also packages the [example application](example), serves its zip read-only in prod through the Emulator, and runs its [end-to-end scenarios](example/e2e/run.sh). | 508 | +It also runs the [end-to-end scenarios](example/e2e/run.sh) of the [example application](example) through the Emulator, in dev and on its zip installed like Scaleway does ([e2e/deploy-locally.sh](example/e2e/deploy-locally.sh)). |
| 509 | +The example installs the runtime of the checkout with `composer link-runtime`, see its [README](example/README.md#run-it-locally). | ||
| 472 | 510 | ||
| 473 | The repository holds: | 511 | The repository holds: |
| 474 | 512 | ||
| 475 | - `src/`, `bin/`, `resources/`: the package; | 513 | - `src/`, `bin/`, `resources/`: the package; |
| 476 | -- `tests/`: PHPUnit tests, packaging included (it runs Composer, offline); | 514 | +- `tests/`: PHPUnit tests, packaging included; |
| 477 | -- `example/`: a demo Symfony application consuming the package through a `path` repository, with Terraform code to deploy it; | 515 | +- `example/`: a demo Symfony application requiring the package, with Terraform code to deploy it; |
| 478 | -- `probe/`: a dependency-free function measuring the open platform facts; | 516 | +- `probe/`: a dependency-free function measuring the platform facts, and its [results](docs/probe-results.md); |
| 479 | - `docs/`: the [implementation plan](docs/plan.md) and the [ADRs](docs/adr). | 517 | - `docs/`: the [implementation plan](docs/plan.md) and the [ADRs](docs/adr). |
| 480 | 518 | ||
| 481 | ## License | 519 | ## License |
modified
docs/plan.md +14 -16 | @@ -5,27 +5,27 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc | ||
| 5 | 5 | ## Settled design |
| 6 | 6 | |
| 7 | 7 | - Package `jdecool/scaleway-runtime`, namespace `JDecool\ScalewayRuntime`, MIT, PHP >=8.4, Symfony ^7.4 || ^8.0. |
| 8 | -- Repository: the package at the root, a demo Symfony app in `example/` consuming it through a Composer `path` repository. | |
| 8 | +- Repository: the package at the root, a demo Symfony app in `example/` requiring it from Packagist, `composer link-runtime` adding a `path` repository on the fly for local work and CI. | |
| 9 | 9 | - The application commits a 3-line `handler.php` stub (generated by `vendor/bin/scaleway-init`); the Scaleway handler setting is `handler.handle`. |
| 10 | 10 | - The Handler drives `ScalewayRuntime extends SymfonyRuntime` itself, without `vendor/autoload_runtime.php` (ADR 0001). |
| 11 | 11 | - The Front Controller is the unchanged `public/index.php`; its path is the `front_controller` runtime option (`extra.runtime` or `APP_RUNTIME_OPTIONS`). |
| 12 | 12 | - The Application is booted once per Warm Process; between Invocations the runtime calls `terminate()` and the `services_resetter`; a failed boot is retried on the next Invocation (ADR 0002). |
| 13 | 13 | - A Front Controller returning a Response is called for every Invocation, a `Request` argument receiving the Invocation's Request (ADR 0004). |
| 14 | -- A Front Controller returning a PSR-15 `RequestHandlerInterface` is built once like a Kernel and served through `symfony/psr-http-message-bridge` (suggested, with `nyholm/psr7`); the PSR-7 request carries the client resolved from the trusted `X-Forwarded-*` headers in its server params (`REMOTE_ADDR`, `HTTPS`, `HTTP_HOST`, `SERVER_PORT`) and URI; a handler implementing `ResetInterface` is reset before every Invocation but the first. | |
| 15 | -- HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; `X-Forwarded-*` trusted; platform limits documented. | |
| 14 | +- A Front Controller returning a PSR-15 `RequestHandlerInterface` is built once like a Kernel and served through `symfony/psr-http-message-bridge` (suggested, with `nyholm/psr7`); the PSR-7 request carries the client the runtime resolved in its server params (`REMOTE_ADDR`, `HTTPS`, `HTTP_HOST`, `SERVER_PORT`) and URI; a handler implementing `ResetInterface` is reset before every Invocation but the first. | |
| 15 | +- HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; no proxy trusted, the client taken from `X-Real-Ip`, the scheme from `X-Forwarded-Proto` and the host from the `host` runtime option (`SCALEWAY_HOST`, `localhost` by default), as clients can forge the other forwarding headers and no `Host` reaches the function (ADR 0007); platform limits documented. | |
| 16 | 16 | - Errors escaping the Application are logged to stderr and returned as a 500 Function Response (generic body, details when `APP_DEBUG=1`). |
| 17 | 17 | - The Handler runs `prod` when the process environment sets no `APP_ENV`, whatever the shipped `.env` says, and the Emulator `dev` (ADR 0006); `APP_RUNTIME_MODE` defaults to `web=1&worker=1`, so error pages use the web renderer. |
| 18 | -- Cache is warmed at build time; cache and log dirs fall back to `/tmp` when the project dir is not writable; the runtime logs its own errors to stderr, the application's logs are its own configuration. | |
| 19 | -- `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). | |
| 20 | -- CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). | |
| 18 | +- Scaleway installs the dependencies at deploy time from the shipped `composer.lock`, scripts included; the cache is built at the first Invocation of each Warm Process, or by an optional `post-install-cmd`; cache and log dirs fall back to `/tmp` when the project dir is not writable; the runtime logs its own errors to stderr, the application's logs are its own configuration (ADR 0008). | |
| 19 | +- `vendor/bin/scaleway-package` zips the sources with `composer.json` and `composer.lock`, without `vendor/`, `var/`, `tests/`, `.env.local`, and refuses a missing or stale lock and `path` repositories (ADR 0008). | |
| 20 | +- CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET`, in an Event without the `X-Real-Ip` and `X-Envoy-External-Address` of HTTP calls, makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). | |
| 21 | 21 | - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset. |
| 22 | 22 | - A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. |
| 23 | -- Tests: PHPUnit on conversions, runners and packaging (offline, from path repositories), GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. | |
| 23 | +- Tests: PHPUnit on conversions, runners and packaging (offline), GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x; the example's scenarios through the Emulator, in dev and on its zip installed like Scaleway does), local Emulator `vendor/bin/scaleway-serve` building Events like the platform and CRON Events on `POST /_scaleway/cron`, manual real E2E deployed with Terraform. | |
| 24 | 24 | |
| 25 | 25 | ## Phases |
| 26 | 26 | |
| 27 | 27 | Each phase ends with green CI checks locally, a commit series, and an independent code review whose findings are addressed before the next phase. |
| 28 | -Phases 1 to 8 are done; what remains is listed under [Remaining work](#remaining-work). | |
| 28 | +Phases 1 to 9 are done; what remains is listed under [Remaining work](#remaining-work). | |
| 29 | 29 | |
| 30 | 30 | 1. **Bootstrap**: `composer.json`, directory layout, PHPUnit, PHPStan (max), PHP-CS-Fixer, GitHub Actions matrix, `.gitignore`, `LICENSE`, minimal `README.md`. |
| 31 | 31 | 2. **HttpKernel Application**: `ScalewayRuntime`, Handler bootstrap, Event to `Request` and `Response` to Function Response conversions, runner, reset between Invocations, boot retry, error handling, `front_controller` option, writable dir fallback. |
| @@ -35,6 +35,7 @@ Phases 1 to 8 are done; what remains is listed under [Remaining work](#remaining | ||
| 35 | 35 | 6. **Console Application and CRON routing**: Command Invocations, Console `Application` / `Command` Front Controllers. |
| 36 | 36 | 7. **Raw callable Application**: `fn(array $event, mixed $context): array`. |
| 37 | 37 | 8. **Documentation**: full README (install, configure, deploy, limitations), packaging tests; folding the probe results in is left for after the probe is deployed. |
| 38 | +9. **Fold in the probe results** ([docs/probe-results.md](probe-results.md)): Scaleway installs the dependencies (ADR 0008, superseding ADR 0003), so `scaleway-package` zips the sources and lock only; only the headers the platform overwrites are trusted and the host is a runtime option (ADR 0007); Command Invocations also require a CRON-shaped Event (ADR 0005); converter tests on real Events; Emulator Events and context like the platform's, CRON simulated on a reserved path; the example requires the runtime from Packagist; Terraform recreates CRON triggers when the function is replaced. | |
| 38 | 39 | |
| 39 | 40 | ## Deviations from the original plan |
| 40 | 41 | |
| @@ -47,14 +48,11 @@ Recorded per phase; the settled design above already reflects them. | ||
| 47 | 48 | - **Phase 6**: the Command Invocation body carries the CRON Secret, matched against `SCALEWAY_CRON_SECRET`, as nothing known tells a CRON Invocation from an HTTP one (ADR 0005); Front Controllers returning a Console `Application` or a `Command` serve Command Invocations only, and answer a 403 to the others. |
| 48 | 49 | - **Phase 7**: the raw callable is a `Closure` returned by the Front Controller closure, not any callable, and must return the documented Function Response shape, undocumented keys being dropped. |
| 49 | 50 | - **Phase 8**: documenting the environment variables showed that a function deployed without `APP_ENV` ran the shipped `.env`'s `dev`, and that error pages dumped the exception even in `prod`, through FrameworkBundle's CLI error renderer: the Handler now defaults to `prod` (ADR 0006) and to the web runtime mode, and `symfony/dotenv` is a dev dependency so the tests boot like a Flex application. |
| 51 | +- **Phase 9**: the example's committed `composer.lock` stays the one `composer link-runtime` writes until the package is on Packagist, as no lock resolving it from Packagist can be generated before; CI proves the zip by installing it like Scaleway with this checkout standing in for Packagist, through a Composer repository configured outside `composer.json` (`e2e/deploy-locally.sh`); a read-only project only keeps `var/cache` as build dir when a container was warmed there, as the zip no longer ships one. | |
| 50 | 52 | |
| 51 | 53 | ## Remaining work |
| 52 | 54 | |
| 53 | -1. Deploy the [probe](../probe) on `php85` and `php84`, and collect the results its README asks for. | |
| 54 | -2. Fold the results in: | |
| 55 | - - the README's unverified platform facts, client IP and Composer sections; | |
| 56 | - - the Emulator's Event shape (`Emulator::toEvent()`): header case, repeated query keys, `X-Forwarded-*`, base64 bodies, context; | |
| 57 | - - `HttpConverter`: trusted `X-Forwarded-*` headers, multi-value response headers; | |
| 58 | - - `CommandInvocation::fromEvent()` and ADR 0005, if CRON Events carry a marker HTTP clients cannot forge; | |
| 59 | - - ADR 0003 and `scaleway-package`, depending on whether Composer runs over the shipped `vendor/`. | |
| 60 | -3. Apply the example's Terraform, never applied so far, and run `example/e2e/run.sh` against the deployment. | |
| 55 | +1. Push the repository to GitHub (https://github.com/jdecool/scaleway-runtime) and get CI green there. | |
| 56 | +2. Publish the package on Packagist, then regenerate `example/composer.lock` against it (`composer update jdecool/scaleway-runtime` without the local repository) and commit it. | |
| 57 | +3. Apply the example's Terraform, never applied so far, and run `example/e2e/run.sh` against the deployment, with `host` set on a second apply. | |
| 58 | +4. Measure what the probe left open: where logs go (Cockpit), and what a deployment with a failing Composer script serves. | |
| @@ -5,27 +5,27 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc | |||
| 5 | ## Settled design | 5 | ## Settled design |
| 6 | 6 | ||
| 7 | - Package `jdecool/scaleway-runtime`, namespace `JDecool\ScalewayRuntime`, MIT, PHP >=8.4, Symfony ^7.4 || ^8.0. | 7 | - Package `jdecool/scaleway-runtime`, namespace `JDecool\ScalewayRuntime`, MIT, PHP >=8.4, Symfony ^7.4 || ^8.0. |
| 8 | -- Repository: the package at the root, a demo Symfony app in `example/` consuming it through a Composer `path` repository. | 8 | +- Repository: the package at the root, a demo Symfony app in `example/` requiring it from Packagist, `composer link-runtime` adding a `path` repository on the fly for local work and CI. |
| 9 | - The application commits a 3-line `handler.php` stub (generated by `vendor/bin/scaleway-init`); the Scaleway handler setting is `handler.handle`. | 9 | - The application commits a 3-line `handler.php` stub (generated by `vendor/bin/scaleway-init`); the Scaleway handler setting is `handler.handle`. |
| 10 | - The Handler drives `ScalewayRuntime extends SymfonyRuntime` itself, without `vendor/autoload_runtime.php` (ADR 0001). | 10 | - The Handler drives `ScalewayRuntime extends SymfonyRuntime` itself, without `vendor/autoload_runtime.php` (ADR 0001). |
| 11 | - The Front Controller is the unchanged `public/index.php`; its path is the `front_controller` runtime option (`extra.runtime` or `APP_RUNTIME_OPTIONS`). | 11 | - The Front Controller is the unchanged `public/index.php`; its path is the `front_controller` runtime option (`extra.runtime` or `APP_RUNTIME_OPTIONS`). |
| 12 | - The Application is booted once per Warm Process; between Invocations the runtime calls `terminate()` and the `services_resetter`; a failed boot is retried on the next Invocation (ADR 0002). | 12 | - The Application is booted once per Warm Process; between Invocations the runtime calls `terminate()` and the `services_resetter`; a failed boot is retried on the next Invocation (ADR 0002). |
| 13 | - A Front Controller returning a Response is called for every Invocation, a `Request` argument receiving the Invocation's Request (ADR 0004). | 13 | - A Front Controller returning a Response is called for every Invocation, a `Request` argument receiving the Invocation's Request (ADR 0004). |
| 14 | -- A Front Controller returning a PSR-15 `RequestHandlerInterface` is built once like a Kernel and served through `symfony/psr-http-message-bridge` (suggested, with `nyholm/psr7`); the PSR-7 request carries the client resolved from the trusted `X-Forwarded-*` headers in its server params (`REMOTE_ADDR`, `HTTPS`, `HTTP_HOST`, `SERVER_PORT`) and URI; a handler implementing `ResetInterface` is reset before every Invocation but the first. | 14 | +- A Front Controller returning a PSR-15 `RequestHandlerInterface` is built once like a Kernel and served through `symfony/psr-http-message-bridge` (suggested, with `nyholm/psr7`); the PSR-7 request carries the client the runtime resolved in its server params (`REMOTE_ADDR`, `HTTPS`, `HTTP_HOST`, `SERVER_PORT`) and URI; a handler implementing `ResetInterface` is reset before every Invocation but the first. |
| 15 | -- HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; `X-Forwarded-*` trusted; platform limits documented. | 15 | +- HTTP mapping: base64 bodies decoded on input; output marked `isBase64Encoded` when the body is not valid UTF-8; multi-value headers sent as arrays; streamed and binary file responses buffered; query string rebuilt from `queryStringParameters`; no proxy trusted, the client taken from `X-Real-Ip`, the scheme from `X-Forwarded-Proto` and the host from the `host` runtime option (`SCALEWAY_HOST`, `localhost` by default), as clients can forge the other forwarding headers and no `Host` reaches the function (ADR 0007); platform limits documented. |
| 16 | - Errors escaping the Application are logged to stderr and returned as a 500 Function Response (generic body, details when `APP_DEBUG=1`). | 16 | - Errors escaping the Application are logged to stderr and returned as a 500 Function Response (generic body, details when `APP_DEBUG=1`). |
| 17 | - The Handler runs `prod` when the process environment sets no `APP_ENV`, whatever the shipped `.env` says, and the Emulator `dev` (ADR 0006); `APP_RUNTIME_MODE` defaults to `web=1&worker=1`, so error pages use the web renderer. | 17 | - The Handler runs `prod` when the process environment sets no `APP_ENV`, whatever the shipped `.env` says, and the Emulator `dev` (ADR 0006); `APP_RUNTIME_MODE` defaults to `web=1&worker=1`, so error pages use the web renderer. |
| 18 | -- Cache is warmed at build time; cache and log dirs fall back to `/tmp` when the project dir is not writable; the runtime logs its own errors to stderr, the application's logs are its own configuration. | 18 | +- Scaleway installs the dependencies at deploy time from the shipped `composer.lock`, scripts included; the cache is built at the first Invocation of each Warm Process, or by an optional `post-install-cmd`; cache and log dirs fall back to `/tmp` when the project dir is not writable; the runtime logs its own errors to stderr, the application's logs are its own configuration (ADR 0008). |
| 19 | -- `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). | 19 | +- `vendor/bin/scaleway-package` zips the sources with `composer.json` and `composer.lock`, without `vendor/`, `var/`, `tests/`, `.env.local`, and refuses a missing or stale lock and `path` repositories (ADR 0008). |
| 20 | -- CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). | 20 | +- CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET`, in an Event without the `X-Real-Ip` and `X-Envoy-External-Address` of HTTP calls, makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). |
| 21 | - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset. | 21 | - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset. |
| 22 | - A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. | 22 | - A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. |
| 23 | -- Tests: PHPUnit on conversions, runners and packaging (offline, from path repositories), GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. | 23 | +- Tests: PHPUnit on conversions, runners and packaging (offline), GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x; the example's scenarios through the Emulator, in dev and on its zip installed like Scaleway does), local Emulator `vendor/bin/scaleway-serve` building Events like the platform and CRON Events on `POST /_scaleway/cron`, manual real E2E deployed with Terraform. |
| 24 | 24 | ||
| 25 | ## Phases | 25 | ## Phases |
| 26 | 26 | ||
| 27 | Each phase ends with green CI checks locally, a commit series, and an independent code review whose findings are addressed before the next phase. | 27 | Each phase ends with green CI checks locally, a commit series, and an independent code review whose findings are addressed before the next phase. |
| 28 | -Phases 1 to 8 are done; what remains is listed under [Remaining work](#remaining-work). | 28 | +Phases 1 to 9 are done; what remains is listed under [Remaining work](#remaining-work). |
| 29 | 29 | ||
| 30 | 1. **Bootstrap**: `composer.json`, directory layout, PHPUnit, PHPStan (max), PHP-CS-Fixer, GitHub Actions matrix, `.gitignore`, `LICENSE`, minimal `README.md`. | 30 | 1. **Bootstrap**: `composer.json`, directory layout, PHPUnit, PHPStan (max), PHP-CS-Fixer, GitHub Actions matrix, `.gitignore`, `LICENSE`, minimal `README.md`. |
| 31 | 2. **HttpKernel Application**: `ScalewayRuntime`, Handler bootstrap, Event to `Request` and `Response` to Function Response conversions, runner, reset between Invocations, boot retry, error handling, `front_controller` option, writable dir fallback. | 31 | 2. **HttpKernel Application**: `ScalewayRuntime`, Handler bootstrap, Event to `Request` and `Response` to Function Response conversions, runner, reset between Invocations, boot retry, error handling, `front_controller` option, writable dir fallback. |
| @@ -35,6 +35,7 @@ Phases 1 to 8 are done; what remains is listed under [Remaining work](#remaining | |||
| 35 | 6. **Console Application and CRON routing**: Command Invocations, Console `Application` / `Command` Front Controllers. | 35 | 6. **Console Application and CRON routing**: Command Invocations, Console `Application` / `Command` Front Controllers. |
| 36 | 7. **Raw callable Application**: `fn(array $event, mixed $context): array`. | 36 | 7. **Raw callable Application**: `fn(array $event, mixed $context): array`. |
| 37 | 8. **Documentation**: full README (install, configure, deploy, limitations), packaging tests; folding the probe results in is left for after the probe is deployed. | 37 | 8. **Documentation**: full README (install, configure, deploy, limitations), packaging tests; folding the probe results in is left for after the probe is deployed. |
| 38 | +9. **Fold in the probe results** ([docs/probe-results.md](probe-results.md)): Scaleway installs the dependencies (ADR 0008, superseding ADR 0003), so `scaleway-package` zips the sources and lock only; only the headers the platform overwrites are trusted and the host is a runtime option (ADR 0007); Command Invocations also require a CRON-shaped Event (ADR 0005); converter tests on real Events; Emulator Events and context like the platform's, CRON simulated on a reserved path; the example requires the runtime from Packagist; Terraform recreates CRON triggers when the function is replaced. | ||
| 38 | 39 | ||
| 39 | ## Deviations from the original plan | 40 | ## Deviations from the original plan |
| 40 | 41 | ||
| @@ -47,14 +48,11 @@ Recorded per phase; the settled design above already reflects them. | |||
| 47 | - **Phase 6**: the Command Invocation body carries the CRON Secret, matched against `SCALEWAY_CRON_SECRET`, as nothing known tells a CRON Invocation from an HTTP one (ADR 0005); Front Controllers returning a Console `Application` or a `Command` serve Command Invocations only, and answer a 403 to the others. | 48 | - **Phase 6**: the Command Invocation body carries the CRON Secret, matched against `SCALEWAY_CRON_SECRET`, as nothing known tells a CRON Invocation from an HTTP one (ADR 0005); Front Controllers returning a Console `Application` or a `Command` serve Command Invocations only, and answer a 403 to the others. |
| 48 | - **Phase 7**: the raw callable is a `Closure` returned by the Front Controller closure, not any callable, and must return the documented Function Response shape, undocumented keys being dropped. | 49 | - **Phase 7**: the raw callable is a `Closure` returned by the Front Controller closure, not any callable, and must return the documented Function Response shape, undocumented keys being dropped. |
| 49 | - **Phase 8**: documenting the environment variables showed that a function deployed without `APP_ENV` ran the shipped `.env`'s `dev`, and that error pages dumped the exception even in `prod`, through FrameworkBundle's CLI error renderer: the Handler now defaults to `prod` (ADR 0006) and to the web runtime mode, and `symfony/dotenv` is a dev dependency so the tests boot like a Flex application. | 50 | - **Phase 8**: documenting the environment variables showed that a function deployed without `APP_ENV` ran the shipped `.env`'s `dev`, and that error pages dumped the exception even in `prod`, through FrameworkBundle's CLI error renderer: the Handler now defaults to `prod` (ADR 0006) and to the web runtime mode, and `symfony/dotenv` is a dev dependency so the tests boot like a Flex application. |
| 51 | +- **Phase 9**: the example's committed `composer.lock` stays the one `composer link-runtime` writes until the package is on Packagist, as no lock resolving it from Packagist can be generated before; CI proves the zip by installing it like Scaleway with this checkout standing in for Packagist, through a Composer repository configured outside `composer.json` (`e2e/deploy-locally.sh`); a read-only project only keeps `var/cache` as build dir when a container was warmed there, as the zip no longer ships one. | ||
| 50 | 52 | ||
| 51 | ## Remaining work | 53 | ## Remaining work |
| 52 | 54 | ||
| 53 | -1. Deploy the [probe](../probe) on `php85` and `php84`, and collect the results its README asks for. | 55 | +1. Push the repository to GitHub (https://github.com/jdecool/scaleway-runtime) and get CI green there. |
| 54 | -2. Fold the results in: | 56 | +2. Publish the package on Packagist, then regenerate `example/composer.lock` against it (`composer update jdecool/scaleway-runtime` without the local repository) and commit it. |
| 55 | - - the README's unverified platform facts, client IP and Composer sections; | 57 | +3. Apply the example's Terraform, never applied so far, and run `example/e2e/run.sh` against the deployment, with `host` set on a second apply. |
| 56 | - - the Emulator's Event shape (`Emulator::toEvent()`): header case, repeated query keys, `X-Forwarded-*`, base64 bodies, context; | 58 | +4. Measure what the probe left open: where logs go (Cockpit), and what a deployment with a failing Composer script serves. |
| 57 | - - `HttpConverter`: trusted `X-Forwarded-*` headers, multi-value response headers; | ||
| 58 | - - `CommandInvocation::fromEvent()` and ADR 0005, if CRON Events carry a marker HTTP clients cannot forge; | ||
| 59 | - - ADR 0003 and `scaleway-package`, depending on whether Composer runs over the shipped `vendor/`. | ||
| 60 | -3. Apply the example's Terraform, never applied so far, and run `example/e2e/run.sh` against the deployment. | ||
modified
probe/README.md +3 -2 | @@ -3,7 +3,8 @@ | ||
| 3 | 3 | A dependency-free PHP function that reports what Scaleway Serverless Functions pass to, and accept from, a PHP handler. |
| 4 | 4 | Its answers settle the open questions of the [implementation plan](../docs/plan.md) before the runtime relies on them. |
| 5 | 5 | |
| 6 | -The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. | |
| 6 | +It was run on 2026-11-14 on `php85` and `php84`: the results are in [docs/probe-results.md](../docs/probe-results.md), and the runtime follows them. | |
| 7 | +Rerun it to check a platform change, and update that file. | |
| 7 | 8 | |
| 8 | 9 | ## Deploy |
| 9 | 10 | |
| @@ -16,7 +17,7 @@ terraform -chdir=probe/terraform apply | ||
| 16 | 17 | export PROBE_URL=$(terraform -chdir=probe/terraform output -raw endpoint) |
| 17 | 18 | ``` |
| 18 | 19 | |
| 19 | -To probe the PHP 8.4 runtime too, run `terraform -chdir=probe/terraform apply -var runtime=php84` afterwards: it replaces the function, so export `PROBE_URL` again and repeat the checks in a separate results directory. | |
| 20 | +To probe the PHP 8.4 runtime too, run `terraform -chdir=probe/terraform apply -var runtime=php84` afterwards: it replaces the function, and its CRON trigger with it, so export `PROBE_URL` again and repeat the checks in a separate results directory. | |
| 20 | 21 | |
| 21 | 22 | ## Run the checks |
| 22 | 23 | |
| @@ -3,7 +3,8 @@ | |||
| 3 | A dependency-free PHP function that reports what Scaleway Serverless Functions pass to, and accept from, a PHP handler. | 3 | A dependency-free PHP function that reports what Scaleway Serverless Functions pass to, and accept from, a PHP handler. |
| 4 | Its answers settle the open questions of the [implementation plan](../docs/plan.md) before the runtime relies on them. | 4 | Its answers settle the open questions of the [implementation plan](../docs/plan.md) before the runtime relies on them. |
| 5 | 5 | ||
| 6 | -The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. | 6 | +It was run on 2026-11-14 on `php85` and `php84`: the results are in [docs/probe-results.md](../docs/probe-results.md), and the runtime follows them. |
| 7 | +Rerun it to check a platform change, and update that file. | ||
| 7 | 8 | ||
| 8 | ## Deploy | 9 | ## Deploy |
| 9 | 10 | ||
| @@ -16,7 +17,7 @@ terraform -chdir=probe/terraform apply | |||
| 16 | export PROBE_URL=$(terraform -chdir=probe/terraform output -raw endpoint) | 17 | export PROBE_URL=$(terraform -chdir=probe/terraform output -raw endpoint) |
| 17 | ``` | 18 | ``` |
| 18 | 19 | ||
| 19 | -To probe the PHP 8.4 runtime too, run `terraform -chdir=probe/terraform apply -var runtime=php84` afterwards: it replaces the function, so export `PROBE_URL` again and repeat the checks in a separate results directory. | 20 | +To probe the PHP 8.4 runtime too, run `terraform -chdir=probe/terraform apply -var runtime=php84` afterwards: it replaces the function, and its CRON trigger with it, so export `PROBE_URL` again and repeat the checks in a separate results directory. |
| 20 | 21 | ||
| 21 | ## Run the checks | 22 | ## Run the checks |
| 22 | 23 | ||
modified
probe/terraform/main.tf +6 -1 | @@ -1,4 +1,4 @@ | ||
| 1 | -# Deploys the probe function. Unvalidated: written against the scaleway provider docs (v2.92), never applied. | |
| 1 | +# Deploys the probe function, applied on 2026-11-14 with the scaleway provider v2.92 (see docs/probe-results.md). | |
| 2 | 2 | # Credentials come from the usual SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID variables. |
| 3 | 3 | |
| 4 | 4 | terraform { |
| @@ -70,6 +70,11 @@ resource "scaleway_function_cron" "probe" { | ||
| 70 | 70 | name = "probe-cron" |
| 71 | 71 | schedule = "*/5 * * * *" |
| 72 | 72 | args = jsonencode({ probe = "cron", nested = { list = [1, 2] } }) |
| 73 | + | |
| 74 | + # Replacing the function (e.g. -var runtime=php84) deletes its CRON triggers server side: recreate them in the same apply. | |
| 75 | + lifecycle { | |
| 76 | + replace_triggered_by = [scaleway_function.probe.id] | |
| 77 | + } | |
| 73 | 78 | } |
| 74 | 79 | |
| 75 | 80 | output "endpoint" { |
| @@ -1,4 +1,4 @@ | |||
| 1 | -# Deploys the probe function. Unvalidated: written against the scaleway provider docs (v2.92), never applied. | 1 | +# Deploys the probe function, applied on 2026-11-14 with the scaleway provider v2.92 (see docs/probe-results.md). |
| 2 | # Credentials come from the usual SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID variables. | 2 | # Credentials come from the usual SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID variables. |
| 3 | 3 | ||
| 4 | terraform { | 4 | terraform { |
| @@ -70,6 +70,11 @@ resource "scaleway_function_cron" "probe" { | |||
| 70 | name = "probe-cron" | 70 | name = "probe-cron" |
| 71 | schedule = "*/5 * * * *" | 71 | schedule = "*/5 * * * *" |
| 72 | args = jsonencode({ probe = "cron", nested = { list = [1, 2] } }) | 72 | args = jsonencode({ probe = "cron", nested = { list = [1, 2] } }) |
| 73 | + | ||
| 74 | + # Replacing the function (e.g. -var runtime=php84) deletes its CRON triggers server side: recreate them in the same apply. | ||
| 75 | + lifecycle { | ||
| 76 | + replace_triggered_by = [scaleway_function.probe.id] | ||
| 77 | + } | ||
| 73 | } | 78 | } |
| 74 | 79 | ||
| 75 | output "endpoint" { | 80 | output "endpoint" { |