### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/google/guide/variables) # Google - Variables The Serverless framework provides a powerful variable system which allows you to add dynamic data into your `serverless.yml`. With Serverless Variables, you'll be able to do the following: - Reference & load variables from environment variables - Reference & load variables from CLI options - Recursively reference properties of any type from the same `serverless.yml` file - Recursively reference properties of any type from other YAML / JSON files - Recursively nest variable references within each other for ultimate flexibility - Combine multiple variable references to overwrite each other **Note:** You can only use variables in `serverless.yml` property **values**, not property keys. So you can't use variables to generate dynamic logical IDs in the custom resources section for example. ## Reference Properties In serverless.yml To self-reference properties in `serverless.yml`, use the `${self:someProperty}` syntax in your `serverless.yml`. This functionality is recursive, so you can go as deep in the object tree as you want. ```yml service: new-service provider: google custom: resource: projects/*/topics/my-topic functions: first: handler: firstPubSub events: - event: eventType: providers/cloud.pubsub/eventTypes/topics.publish resource: ${self:custom.resource} second: handler: secondPubSub events: - event: eventType: providers/cloud.pubsub/eventTypes/topics.publish resource: ${self:custom.resource} ``` In the above example you're setting a global event resource for all functions by referencing the `resource` property in the same `serverless.yml` file. This way, you can easily change the event resource for all functions whenever you like. ## Reference Variables in JavaScript Files To add dynamic data into your variables, reference javascript files by putting `${file(./myFile.js):someModule}` syntax in your `serverless.yml`. Here's an example: ```javascript // myCustomFile.js module.exports.resource = () => { // Code that generates dynamic data return 'projects/*/topics/my-topic'; } ``` ```yml # serverless.yml service: new-service provider: google functions: first: handler: pubSub events: - event: eventType: providers/cloud.pubsub/eventTypes/topics.publish resource: ${file(./myCustomFile.js):resource} # Reference a specific module ``` You can also return an object and reference a specific property. Just make sure you are returning a valid object and referencing a valid property: ```javascript // myCustomFile.js module.exports.pubSub = () => { // Code that generates dynamic data return { resource: 'projects/*/topics/my-topic' }; } ``` ```yml # serverless.yml service: new-service provider: google functions: first: handler: pubSub events: - event: eventType: providers/cloud.pubsub/eventTypes/topics.publish resource: ${file(./myCustomFile.js):pubSub.resource} # Reference a specific module ``` ## Multiple Configuration Files Adding many custom resources to your `serverless.yml` file could bloat the whole file, so you can use the Serverless Variable syntax to split this up. ```yml resources: Resources: ${file(google-cloud-resources.json)} ``` The corresponding resources which are defined inside the `google-cloud-resources.json` file will be resolved and loaded into the `Resources` section. ## Migrating serverless.env.yml Previously we used the `serverless.env.yml` file to track Serverless Variables. It was a completely different system with different concepts. To migrate your variables from `serverless.env.yml`, you'll need to decide where you want to store your variables. **Using a config file:** You can still use `serverless.env.yml`, but the difference now is that you can structure the file however you want, and you'll need to reference each variable/property correctly in `serverless.yml`. For more info, you can check the file reference section above. **Using the same `serverless.yml` file:** You can store your variables in `serverless.yml` if they don't contain sensitive data, and then reference them elsewhere in the file using `self:someProperty`. For more info, you can check the self reference section above. **Using environment variables:** You can instead store your variables in environment variables and reference them with `env.someEnvVar`. For more info, you can check the environment variable reference section above. Now you don't need `serverless.env.yml` at all, but you can still use it if you want. It's just not required anymore. Migrating to the new variable system is easy and you just need to know how the new system works and make small adjustments to how you store & reference your variables.