Skip to content

Commit 5123ffb

Browse files
committed
Switch to a single layer
1 parent 91aaae3 commit 5123ffb

4 files changed

Lines changed: 59 additions & 52 deletions

File tree

index.js

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -173,62 +173,83 @@ class ServerlessPlugin {
173173
* Process the `php-xx` runtimes to turn them into `provided.al2` runtimes + Bref layers.
174174
*/
175175
processPhpRuntimes() {
176-
const includeBrefLayers = (runtime, existingLayers, isArm) => {
177-
let layerName = runtime;
176+
const includeBrefLayers = (existingLayers, phpVersion, isArm) => {
177+
let layerName = 'php-' + phpVersion;
178178
// Automatically use ARM layers if the function is deployed to an ARM architecture
179179
if (isArm) {
180180
layerName = 'arm-' + layerName;
181181
}
182-
if (layerName.endsWith('-console')) {
183-
layerName = layerName.substring(0, layerName.length - '-console'.length);
184-
existingLayers.unshift(this.getLayerArn('console', this.provider.getRegion()));
185-
existingLayers.unshift(this.getLayerArn(layerName, this.provider.getRegion()));
186-
} else {
187-
existingLayers.unshift(this.getLayerArn(layerName, this.provider.getRegion()));
188-
}
182+
existingLayers.unshift(this.getLayerArn(layerName, this.provider.getRegion()));
189183
return existingLayers;
190184
}
185+
/**
186+
* @param {string} runtime
187+
* @return {string|undefined}
188+
*/
189+
const runtimeStringToRuntimeClass = (runtime) => {
190+
if (! runtime.startsWith('php-')) {
191+
return undefined;
192+
}
193+
if (runtime.endsWith('-console')) {
194+
return 'Bref\\ConsoleRuntime\\Main';
195+
}
196+
if (runtime.endsWith('-fpm')) {
197+
return 'Bref\\FpmRuntime\\Main';
198+
}
199+
return 'Bref\\FunctionRuntime\\Main';
200+
};
201+
const configureFunctionRuntime = (f) => {
202+
// `php-\d\d(-fpm|console)?`
203+
const fullRuntimeString = f.runtime || config.provider.runtime;
204+
if (! fullRuntimeString || ! fullRuntimeString.startsWith('php-')) {
205+
return;
206+
}
207+
const phpVersion = fullRuntimeString.substring('php-'.length).split('-')[0];
208+
const runtimeClass = runtimeStringToRuntimeClass(fullRuntimeString);
209+
if (! runtimeClass) return;
210+
211+
// The logic here is a bit custom:
212+
// If there are layers on the function, we preserve them
213+
let existingLayers = f.layers || []; // make sure it's an array
214+
// Else, we merge with the layers defined at the root.
215+
// Indeed, SF overrides the layers defined at the root with the ones defined on the function.
216+
if (existingLayers.length === 0) {
217+
// for some reason it's not always an array
218+
existingLayers = Array.from(config.provider.layers || []);
219+
}
220+
221+
f.layers = includeBrefLayers(
222+
existingLayers,
223+
phpVersion,
224+
f.architecture === 'arm64' || (isArmGlobally && !f.architecture),
225+
);
226+
f.runtime = 'provided.al2';
227+
// Add the `BREF_RUNTIME` environment variable
228+
// to let the function know which runtime it is using
229+
// (this is used by the Bref runtime)
230+
if (!f.environment) {
231+
f.environment = {};
232+
}
233+
if (!f.environment.BREF_RUNTIME) {
234+
f.environment.BREF_RUNTIME = runtimeClass;
235+
}
236+
}
191237

192238
const config = this.serverless.service;
193239
const isArmGlobally = config.provider.architecture === 'arm64';
194240
const isBrefRuntimeGlobally = this.runtimes.includes(config.provider.runtime || '');
195241

196242
// Check functions config
197243
for (const f of Object.values(config.functions || {})) {
198-
if (
199-
(f.runtime && this.runtimes.includes(f.runtime)) ||
200-
(!f.runtime && isBrefRuntimeGlobally)
201-
) {
202-
// The logic here is a bit custom:
203-
// If there are layers on the function, we preserve them
204-
let existingLayers = f.layers || []; // make sure it's an array
205-
// Else, we merge with the layers defined at the root.
206-
// Indeed, SF overrides the layers defined at the root with the ones defined on the function.
207-
if (existingLayers.length === 0) {
208-
// for some reason it's not always an array
209-
existingLayers = Array.from(config.provider.layers || []);
210-
}
211-
212-
f.layers = includeBrefLayers(
213-
f.runtime || config.provider.runtime,
214-
existingLayers,
215-
f.architecture === 'arm64' || (isArmGlobally && !f.architecture),
216-
);
217-
f.runtime = 'provided.al2';
218-
}
244+
configureFunctionRuntime(f);
219245
}
220246

221247
// Check Lift constructs config
222248
for (const construct of Object.values(this.serverless.configurationInput.constructs || {})) {
223249
if (construct.type !== 'queue' && construct.type !== 'webhook') continue;
224250
const f = construct.type === 'queue' ? construct.worker : construct.authorizer;
225-
if (f && (f.runtime && this.runtimes.includes(f.runtime) || !f.runtime && isBrefRuntimeGlobally) ) {
226-
f.layers = includeBrefLayers(
227-
f.runtime || config.provider.runtime,
228-
f.layers || [], // make sure it's an array
229-
f.architecture === 'arm64' || (isArmGlobally && !f.architecture),
230-
);
231-
f.runtime = 'provided.al2';
251+
if (f) {
252+
configureFunctionRuntime(f);
232253
}
233254
}
234255
}

src/FpmRuntime/FpmHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function start(): void
7676
/**
7777
* --nodaemonize: we want to keep control of the process
7878
* --force-stderr: force logs to be sent to stderr, which will allow us to send them to CloudWatch
79+
* TODO set `max_execution_time` to the timeout of the Lambda function?
7980
*/
8081
$resource = @proc_open(['php-fpm', '--nodaemonize', '--force-stderr', '--fpm-config', $this->configFile], [], $pipes);
8182

src/FpmRuntime/Main.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ class Main
1616
{
1717
public static function run(): void
1818
{
19-
// In the FPM runtime process (our process) we want to log all errors and warnings
20-
ini_set('display_errors', '1');
21-
error_reporting(E_ALL);
22-
2319
ColdStartTracker::init();
2420

2521
LazySecretsLoader::loadSecretEnvironmentVariables();

utils/layers.json/update.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,15 @@
1414

1515
const LAYER_NAMES = [
1616
'php-84',
17-
'php-84-fpm',
1817
'php-83',
19-
'php-83-fpm',
2018
'php-82',
21-
'php-82-fpm',
2219
'php-81',
23-
'php-81-fpm',
2420
'php-80',
25-
'php-80-fpm',
2621
'arm-php-84',
27-
'arm-php-84-fpm',
2822
'arm-php-83',
29-
'arm-php-83-fpm',
3023
'arm-php-82',
31-
'arm-php-82-fpm',
3224
'arm-php-81',
33-
'arm-php-81-fpm',
3425
'arm-php-80',
35-
'arm-php-80-fpm',
36-
'console',
3726
];
3827

3928
$regions = json_decode(file_get_contents(__DIR__ . '/regions.json'), true);

0 commit comments

Comments
 (0)