This commit is contained in:
Seth 2017-08-02 19:07:01 +10:00
parent 54dd2f64fc
commit 6a4aa3c8d3

View File

@ -124,7 +124,7 @@ functions:
```
In the above example, the value for `myKey` in the `myBucket` S3 bucket will be looked up and used to populate the variable.
## Reference Variables in Other Files
To reference variables in other YAML or JSON files, use the `${file(./myFile.yml):someProperty}` syntax in your `serverless.yml` configuration file. This functionality is recursive, so you can go as deep in that file as you want. Here's an example:
You can reference variables in other YAML or JSON files. To reference variables in other YAML files use the `${file(./myFile.yml):someProperty}` syntax in your `serverless.yml` configuration file. To reference variables in other JSON files use the `${file(./myFile.json):someProperty}` syntax. It is important that the file you are referencing has the correct suffix, or file extension, for its file type (`.yml` for YAML or `.json` for JSON) in order for it to be interpreted correctly. Here's an example:
```yml
# myCustomFile.yml
@ -147,7 +147,41 @@ functions:
- schedule: ${self:custom.globalSchedule} # This would also work in this case
```
In the above example, you're referencing the entire `myCustomFile.yml` file in the `custom` property. You need to pass the path relative to your service directory. You can also request specific properties in that file as shown in the `schedule` property. It's completely recursive and you can go as deep as you want.
In the above example, you're referencing the entire `myCustomFile.yml` file in the `custom` property. You need to pass the path relative to your service directory. You can also request specific properties in that file as shown in the `schedule` property. It's completely recursive and you can go as deep as you want. Additionally you can request properties that contain arrays from either YAML or JSON reference files. Here's a YAML example for an events array:
```yml
myevents:
- schedule:
rate: rate(1 minute)
```
and for JSON:
```json
{
"myevents": [{
"schedule" : {
"rate" : "rate(1 minute)"
}
}]
}
```
In your serverless.yml, depending on the type of your source file, either have the following syntax for YAML
```yml
functions:
hello:
handler: handler.hello
events: ${file(./myCustomFile.yml):myevents
```
or for a JSON reference file use this sytax:
```yml
functions:
hello:
handler: handler.hello
events: ${file(./myCustomFile.json):myevents
```
## Reference Variables in Javascript Files