getting if statements to work in app.yaml for Google App Engine

84 Views Asked by At

I’m trying to get if statements to work in app.yaml for Google App Engine.

I have the same project running in different instances in order to keep data separated in different regions (europe and us). Since the project is otherwise the same I was hoping I could use if statements in the app.yaml file to select which database instance name to use based on the GOOGLE_CLOUD_PROJECT env variable.

I tried the following code (which is the only syntax I could find online for if statements):

env_variables:
   ${{ if eq(GOOGLE_CLOUD_PROJECT, 'example-project-europe') }}:
    INSTANCE_CONNECTION_NAME: 'example-project-europe:europe-west1:database-name
   ${{ if eq(GOOGLE_CLOUD_PROJECT, 'example-project-us') }}:
    INSTANCE_CONNECTION_NAME: 'example-project-us:us-central1:database-name

it is not correct and produces the following error:

Value '${if eq(GOOGLE_CLOUD_PROJECT, 'example-project-europe') }' for key in EnvironmentVariables does not match expression '^(?:[a-zA-Z_][a-zA-Z0-9_]*)$'

I probably also need to change GOOGLE_CLOUD_PROJECT to something like variables['GOOGLE_CLOUD_PROJECT'] but I’m also unsure what the exact array is called for Google Cloud, or where to get this information.

1

There are 1 best solutions below

0
Alex On

I don't think yaml supports if statements. I think what you found is a syntax specific for azure DevOps, something they made up and look for in their yaml files.

To do what you're doing, I made a local script that would take my base app.yaml and would compile the actual app.yaml for a specific environment right before I would deploy to that environment. I'm a python person so I used fabric & jinja, but every language has a templating engine.

It wasn't perfect, but it worked. A more appropriate solution seemed to be using terraform for app engine instead, but I never got that far.