Skip to content

Commit a630b39

Browse files
Add ability to configure ephemeral storage size (#71)
* Add ability to configure ephemeral storage * Update tests * Update docs * Bump minimum AWS SDK version
1 parent ccd625f commit a630b39

13 files changed

+130
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"illuminate/console": "^7.0|^8.0|^9.0",
1717
"maennchen/zipstream-php": "^2.1",
1818
"guzzlehttp/guzzle": "^6.3|^7.2",
19-
"aws/aws-sdk-php": "^3.20.0"
19+
"aws/aws-sdk-php": "^3.216.1"
2020
},
2121
"require-dev": {
2222
"orchestra/testbench": "^5|^6|^7",

config/sidecar.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
*/
3131
'memory' => env('SIDECAR_MEMORY', 512),
3232

33+
/*
34+
* The default ephemeral storage for your functions, in megabytes.
35+
* This can be overridden per function.
36+
*/
37+
'storage' => env('SIDECAR_STORAGE', 512),
38+
3339
/*
3440
* The default architecture your function runs on.
3541
* Available options are: x86_64, arm64

docs/configuration.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Configuring Sidecar
33

4-
After running `php artisan sidecar:install`, you should have a `sidecar.php` file in your `config` folder.
4+
After running `php artisan sidecar:install`, you should have a `sidecar.php` file in your `config` folder.
55

66
There are several configuration options in that file, which we'll cover in this section.
77

@@ -17,11 +17,11 @@ To get started, run the following command:
1717
php artisan sidecar:configure
1818
```
1919

20-
The first thing it will do is guide you through creating a new AWS user in the web interface, which it will then use to create everything else it needs.
20+
The first thing it will do is guide you through creating a new AWS user in the web interface, which it will then use to create everything else it needs.
2121

2222
Note that this won't start any services, it just creates some policies in IAM.
2323

24-
This is the same general method that Laravel Vapor uses: you provide it with Admin Access and then it configures itself. Sidecar takes it a step further and provides you the option to self-destruct the admin keys once it has configured itself.
24+
This is the same general method that Laravel Vapor uses: you provide it with Admin Access and then it configures itself. Sidecar takes it a step further and provides you the option to self-destruct the admin keys once it has configured itself.
2525

2626
If you'd like to manually set everything up, take a look at the command to see exactly what it's doing, and you can recreate it in the IAM portal.
2727

@@ -62,15 +62,29 @@ return [
6262
];
6363
```
6464

65+
## Function Storage
66+
67+
The ephemeral storage can also be customized, and again, if it isn't, the default from your `sidecar.php` file will be used.
68+
69+
```php
70+
return [
71+
/*
72+
* The default ephemeral storage for your functions, in megabytes.
73+
* This can be overridden per function.
74+
*/
75+
'storage' => env('SIDECAR_STORAGE', 512),
76+
];
77+
```
78+
6579
## Package Base Path
6680

67-
By default, all of your Lambda resources are going to be relative to the `base_path()` of your application. That means when you're defining your code packages, you'll use the root of your application as the starting point.
81+
By default, all of your Lambda resources are going to be relative to the `base_path()` of your application. That means when you're defining your code packages, you'll use the root of your application as the starting point.
6882

6983
If all of your Lambda code lives in e.g. `resources/lambda`, then you can update your `package_base_path` to reflect that.
7084

7185
config/sidecar.php {.filename}
7286
```php
73-
return [
87+
return [
7488
/*
7589
* The base path for your package files. If you e.g. keep
7690
* all your Lambda package files in your resource path,
@@ -91,14 +105,14 @@ By default, the environment name that Sidecar uses is your `APP_ENV` from your `
91105
If you'd like to use something other than the `APP_ENV`, you can do so by providing a `SIDECAR_ENV` environment variable.
92106

93107
```php
94-
return [
95-
/*
108+
return [
109+
/*
96110
* Sidecar separates functions by environment. If you'd like to change
97111
* your Sidecar environment without changing your entire application
98112
* environment, you may do so here.
99113
*/
100114
'env' => env('SIDECAR_ENV', env('APP_ENV')),
101115
];
102116
```
103-
104-
To learn much more about environments and how to use them, see the [Environments](/environments) section.
117+
118+
To learn much more about environments and how to use them, see the [Environments](/environments) section.

docs/functions/customization.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ class ExampleFunction extends LambdaFunction
7878
}
7979
```
8080

81+
## Storage
82+
83+
Lambda functions can configure the amount of ephemeral storage available to them in the `/tmp` directory. This storage is shared between function instances, which means it persists across _invocations_ of a single warm Lambda function instance but is cleaned up everywhere else (e.g. between cold starts, when scaling up to more concurrent invocations, etc.).
84+
85+
Sidecar uses the storage value from you `sidecar.php` configuration file, which defaults to 512MB.
86+
87+
You are free to change this per function by returning a value from the `storage` method.
88+
89+
```php
90+
class ExampleFunction extends LambdaFunction
91+
{
92+
public function storage() // [tl! focus:4]
93+
{
94+
// 2 GB
95+
return 2048;
96+
}
97+
}
98+
```
99+
81100
## Layers
82101

83102
Some functions require extra code or data beyond what is in your code package. From [Amazon's documentation](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html):
@@ -139,7 +158,7 @@ class ExampleFunction extends LambdaFunction
139158
}
140159
```
141160

142-
It is **very important** to note that if you allow Sidecar to manage your Lambda's environment variables, any changes made to environment variables in the AWS UI will be overwritten next time you deploy your function.
161+
It is **very important** to note that if you allow Sidecar to manage your Lambda's environment variables, any changes made to environment variables in the AWS UI will be overwritten next time you deploy your function.
143162

144163
By default, Sidecar doesn't touch your Lambda's environment at all. Only when you return an array from `variables` will Sidecar take control of the env vars.
145164

@@ -162,7 +181,7 @@ class ExampleFunction extends LambdaFunction
162181

163182
public function variables() // [tl! focus:start]
164183
{
165-
// Values will come from the "activating" machine.
184+
// Values will come from the "activating" machine.
166185
return [
167186
'aws_key' => config('services.aws.key'),
168187
'aws_secret' => config('services.aws.secret'),

src/LambdaFunction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@ public function timeout()
312312
return config('sidecar.timeout');
313313
}
314314

315+
/**
316+
* The ephemeral storage, in MB, your Lambda function is given.
317+
*
318+
* @return int
319+
*/
320+
public function storage()
321+
{
322+
return config('sidecar.storage');
323+
}
324+
315325
public function preparePayload($payload)
316326
{
317327
return $payload;
@@ -392,6 +402,9 @@ public function toDeploymentArray()
392402
'Description' => $this->description(),
393403
'Timeout' => (int)$this->timeout(),
394404
'MemorySize' => (int)$this->memory(),
405+
'EphemeralStorage' => [
406+
'Size' => (int)$this->storage(),
407+
],
395408
'Layers' => $this->layers(),
396409
'Publish' => true,
397410
'PackageType' => $this->packageType(),

tests/Unit/DeploymentTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public function mockCreatingFunction()
5757
],
5858
'Description' => 'test-Description',
5959
'Timeout' => 'test-Timeout',
60+
'EphemeralStorage' => [
61+
'Size' => 'test-EphemeralStorage'
62+
],
6063
'MemorySize' => 'test-MemorySize',
6164
'Layers' => 'test-Layers',
6265
'Publish' => 'test-Publish',

tests/Unit/FunctionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ public function app_name_with_a_space_gets_dashed()
2323
}
2424

2525
/** @test */
26-
public function memory_and_timeout_get_cast_to_ints()
26+
public function memory_and_timeout_and_storage_get_cast_to_ints()
2727
{
2828
config([
2929
'sidecar.timeout' => '5',
30-
'sidecar.memory' => '500'
30+
'sidecar.memory' => '500',
31+
'sidecar.storage' => '1024'
3132
]);
3233

3334
$array = (new EmptyTestFunction)->toDeploymentArray();
3435

3536
$this->assertSame(5, $array['Timeout']);
3637
$this->assertSame(500, $array['MemorySize']);
38+
$this->assertSame(1024, $array['EphemeralStorage']['Size']);
3739
}
3840
}

tests/Unit/LambdaClientTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function update_existing_function()
172172
$this->lambda->shouldReceive('functionExists')
173173
->once()
174174
->withArgs(function ($f, $checksum) use ($function) {
175-
return $f === $function && $checksum === 'b3422109';
175+
return $f === $function && $checksum === 'e827998e';
176176
})
177177
->andReturn(false);
178178

@@ -183,8 +183,11 @@ public function update_existing_function()
183183
'Runtime' => 'test-Runtime',
184184
'Role' => 'test-Role',
185185
'Handler' => 'test-Handler',
186-
'Description' => 'test-Description [b3422109]',
186+
'Description' => 'test-Description [e827998e]',
187187
'Timeout' => 'test-Timeout',
188+
'EphemeralStorage' => [
189+
'Size' => 'test-EphemeralStorage'
190+
],
188191
'MemorySize' => 'test-MemorySize',
189192
'Layers' => 'test-Layers',
190193
'Architectures' => [
@@ -221,9 +224,12 @@ public function update_existing_image_function()
221224
->with([
222225
'FunctionName' => 'test-FunctionName',
223226
'Role' => null,
224-
'Description' => 'test-Description [57084773]',
227+
'Description' => 'test-Description [ac420e45]',
225228
'Timeout' => 300,
226229
'MemorySize' => 512,
230+
'EphemeralStorage' => [
231+
'Size' => 512
232+
],
227233
'Layers' => [],
228234
'PackageType' => 'Image',
229235
'Architectures' => [
@@ -253,7 +259,7 @@ public function existing_function_unchanged()
253259
$this->lambda->shouldReceive('functionExists')
254260
->once()
255261
->withArgs(function ($f, $checksum) use ($function) {
256-
return $f === $function && $checksum === 'b3422109';
262+
return $f === $function && $checksum === 'e827998e';
257263
})
258264
->andReturn(true);
259265

tests/Unit/Support/DeploymentTestFunction.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function toDeploymentArray()
4646
'Description' => 'test-Description',
4747
'Timeout' => 'test-Timeout',
4848
'MemorySize' => 'test-MemorySize',
49+
'EphemeralStorage' => [
50+
'Size' => 'test-EphemeralStorage'
51+
],
4952
'Layers' => 'test-Layers',
5053
'Publish' => 'test-Publish',
5154
'Architectures' => ['x86_64'],

tests/Unit/Support/Responses/getFunction.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"Description": "Laravel [local]: Sidecar function `App\\Sidecar\\OgImage`. [ab0f64a5]",
1010
"Timeout": 300,
1111
"MemorySize": 512,
12+
"EphemeralStorage": {
13+
"Size": 512
14+
},
1215
"LastModified": "2021-06-12T14:04:51.802+0000",
1316
"CodeSha256": "hHe3JC+E9cHvgZjRgf39adwq2yzNOftvN0qA\/1kaukU=",
1417
"Version": "$LATEST",
@@ -55,4 +58,4 @@
5558
]
5659
}
5760
}
58-
}
61+
}

0 commit comments

Comments
 (0)