Environment variables can be set for each individual Serverless function or at the provider level for all functions.
Set Environment Variables for Individual Functions
To add environment variables to a Serverless function, define them using environment under the function name. In the example below, the function function1 will have the environment variable S3_BUCKET_NAME defined:
| |
Set Environment Variables for All Functions
It’s also possible to add environment variables to all functions in the Serverless template by defining variables under provider:. In the example below, the functions function1 and function2 will have the environment variable S3_BUCKET_NAME defined:
| |
Combining Both Ways of Setting Environment Variables
Finally, it’s possible to add environment variables under provider as well as under each function.
| |
In this case, both function1 and function2 will have both the STAGE and REGION environment variables set, but function1 will have S3_BUCKET_NAME set while function2 will have TABLE_NAME set.