Back to Prisma1

Using Variables

docs/1.5/04-Reference/02-Service-Configuration/02-prisma.yml/03-Using-Variables.md

1.34.123.3 KB
Original Source

Using Variables

Variables allow you to dynamically replace configuration values in your service definition file prisma.yml.

To use variables inside prisma.yml, you need to reference the values enclosed in ${} brackets. Inside the brackes, you first need to specify the variable source and the variable name, separated by a colon.

yml
# prisma.yml file
yamlKeyXYZ: ${src:myVariable} # see list of current variable sources below
# this is an example of providing a default value as the second parameter
otherYamlKey: ${src:myVariable, "someDefaultValue"}

Note: The quotes around the default value are required.

A variable source can be either of the following three options:

  • An environment variable
  • A self-reference to another value inside the same service
  • An option from the command line

Note that you can only use variables in property values - not in property keys.

Environment variables

You can reference environment variables inside the service definition file.

When using an environment variable, the value that you put into the bracket is composed of:

  • the prefix env:
  • the name of the environment variable

In the following example, environment variables are used to declare the stage, the cluster and the secret the example service:

yml
service: example
stage: ${env:PRISMA_STAGE}
cluster: ${env:PRISMA_CLUSTER}
secret: ${env:PRISMA_SECRET}
datamodel: database/datamodel.graphql

Note that the CLI will load environment variables from 3 different locations and in the following order:

  1. The local environment
  2. A file specified with the --dotenv parameter
  3. if the --dotenvargument was omitted, a file called .env in the same directory

Self-references

You can recursively reference other property values inside the same prisma.yml file.

When using a recursive self-reference as a variable, the value that you put into the bracket is composed of:

  • the prefix self:
  • (optional) the path to the referenced property

If no path is specified, the value of the variable will be the full YAML file.

In the following example, the createCRMEntry function uses the same subscription query as the sendWelcomeEmail function:

yml
subscriptions:
  sendWelcomeEmail:
    query: database/subscriptions/createUserSubscription.graphql
    webhook:
      url: ${self:custom.severlessEndpoint}/sendWelcomeEmail
      headers: ${self:custom.headers}
  createCRMEntry:
    query: ${self:functions.subscriptions.sendWelcomeEmail.query}
    webhook:
      url: ${self:custom.severlessEndpoint}/createCRMEntry
      headers: ${self:custom.headers}

custom:
  serverlessEndpoint: 'https://bcdeaxokbj.execute-api.eu-west-1.amazonaws.com/dev'
  headers:
    Authorization: Bearer wohngaeveishuomeiphohph1ls

CLI options

You can reference CLI options that are passed when invoking a prisma command.

When referencing a CLI option, the value that you put into the bracket is composed of:

  • the prefix opt:
  • the name of the CLI option

As an example, consider this prisma.yml file:

yml
service: example
stage: ${opt:stage}
secret: secret123
datamodel: datamodel.graphql

Now, you can pass a --stage option when running prisma deploy. The CLI will pick the value provided value up and set it as the stage in prisma.yml.

sh
prisma deploy --stage dev