You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for environment variable configuration. (#25)
* Add support for environment variable configuration.
* Add docs.
* Set environment variables before activation, only if they are an array
* Docs
Co-authored-by: Adrian Brown <adrian.brown@plantracker.com.au>
Co-authored-by: Aaron Francis <aarondfrancis@gmail.com>
Copy file name to clipboardExpand all lines: docs/functions/customization.md
+75-16Lines changed: 75 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ The only two things _required_ for a Sidecar function are the [package and the h
5
5
6
6
## Runtime
7
7
8
-
Lambda supports multiple languages through the use of runtimes. You can choose any of the following runtimes by returning its corresponding identifier:
8
+
Lambda supports multiple languages through the use of runtimes. You can choose any of the following runtimes by returning its corresponding identifier:
9
9
10
10
- Node.js 14: `nodejs14.x`
11
11
- Node.js 12: `nodejs12.x`
@@ -20,16 +20,16 @@ Lambda supports multiple languages through the use of runtimes. You can choose a
20
20
- Java 8: `java8`
21
21
- Go 1.x: `go1.x`
22
22
- .NET Core 3.1: `dotnetcore3.1`
23
-
- .NET Core 2.1: `dotnetcore2.1`
23
+
- .NET Core 2.1: `dotnetcore2.1`
24
24
25
25
E.g. to use the Go runtime, you would return `go1.x`:
26
26
27
27
```php
28
28
class ExampleFunction extends LambdaFunction
29
29
{
30
-
public function runtime() // [tl! focus:3]
30
+
public function runtime() // [tl! focus:3]
31
31
{
32
-
return 'go1.x';
32
+
return 'go1.x';
33
33
}
34
34
}
35
35
```
@@ -49,7 +49,7 @@ To change the allocated memory of your function, return the number in megabytes.
49
49
```php
50
50
class ExampleFunction extends LambdaFunction
51
51
{
52
-
public function memory() // [tl! focus:4]
52
+
public function memory() // [tl! focus:4]
53
53
{
54
54
// 2GB of memory
55
55
return 2048;
@@ -80,7 +80,7 @@ class ExampleFunction extends LambdaFunction
80
80
81
81
## Layers
82
82
83
-
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):
83
+
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):
84
84
85
85
> A Lambda layer is a .zip file archive that can contain additional code or data. A layer can contain libraries, a custom runtime, data, or configuration files. Layers promote code sharing and separation of responsibilities so that you can iterate faster on writing business logic.
86
86
@@ -92,43 +92,102 @@ class ExampleFunction extends LambdaFunction
92
92
{
93
93
public function handler()
94
94
{
95
-
//
95
+
//
96
96
}
97
97
98
98
public function package()
99
99
{
100
100
//
101
101
}
102
-
103
-
public function layers() // [tl! focus:start]
102
+
103
+
public function layers() // [tl! focus:start]
104
104
{
105
105
return [
106
-
// Node Canvas from https://github.com/jwerre/node-canvas-lambda
Note that your layers must be in the same AWS region as your Lambdas!
114
114
115
+
## Environment Variables
116
+
117
+
Some functions or layers may require configuration via Lambda environment variables. The Lambda runtime makes environment variables available to the code.
118
+
119
+
In this example below, we're providing a path to a font directory as an environment variable.
120
+
```php
121
+
class ExampleFunction extends LambdaFunction
122
+
{
123
+
public function handler()
124
+
{
125
+
//
126
+
}
127
+
128
+
public function package()
129
+
{
130
+
//
131
+
}
132
+
133
+
public function variables() // [tl! focus:start]
134
+
{
135
+
return [
136
+
'FONTCONFIG_PATH' => '/opt/etc/fonts',
137
+
];
138
+
} // [tl! focus:end]
139
+
}
140
+
```
141
+
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.
143
+
144
+
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.
145
+
146
+
Another important thing to note is that Sidecar sets your environment variables as a part of the _activation_ process, not the _deploy_ process. This means that the environment variables will be pulled from the machine that calls `sidecar:activate`.
147
+
148
+
For example, if you are sharing secrets with your Lambda, then the values passed to your Lambda will be equal to the ones on the machine that called `sidecar:activate`, not the one that called `sidecar:deploy`:
149
+
150
+
```php
151
+
class ExampleFunction extends LambdaFunction
152
+
{
153
+
public function handler()
154
+
{
155
+
//
156
+
}
157
+
158
+
public function package()
159
+
{
160
+
//
161
+
}
162
+
163
+
public function variables() // [tl! focus:start]
164
+
{
165
+
// Values will come from the "activating" machine.
166
+
return [
167
+
'aws_key' => config('services.aws.key'),
168
+
'aws_secret' => config('services.aws.secret'),
169
+
];
170
+
} // [tl! focus:end]
171
+
}
172
+
```
173
+
115
174
## Name
116
175
117
176
Your function has a `name` method that determines how Sidecar names your Lambda functions. By default it is based on the name and path of your PHP class. You are free to change this if you want, but you're unlikely to need to.
118
177
119
178
```php
120
179
class ExampleFunction extends LambdaFunction
121
180
{
122
-
public function name() // [tl! focus:3]
181
+
public function name() // [tl! focus:3]
123
182
{
124
-
return 'Function Name';
183
+
return 'Function Name';
125
184
}
126
185
}
127
186
```
128
187
129
188
### Name Prefix
130
189
131
-
Regardless of what you choose for your function names, Sidecar will prepend the name of your app and the current environment.
190
+
Regardless of what you choose for your function names, Sidecar will prepend the name of your app and the current environment.
132
191
133
192
Lambda function names must be unique, so adding these variables to your function names prevents collisions between different apps and environments.
134
193
@@ -143,7 +202,7 @@ You likely won't need to change this, but if you do, *you must include the envir
0 commit comments