What does beta_settings.cloud_sql_instances do in app.yaml and how may I read this value in my Django settings?

870 views
Skip to first unread message

Neuron Teckid

unread,
Feb 28, 2019, 10:58:15 AM2/28/19
to Google App Engine
Hi there. I'm using GAE Flex environment and following the example in the official site.

In app.yaml there's one configuration

  1. # Replace project and instance with the values obtained  when configuring your
    # Cloud SQL instance.
    beta_settings:
        cloud_sql_instances: INSTANCE_CONNECTION_NAME
It simply says that I should insert correct value without further explanation.

And in my Django settings file, I also need this instance name for DATABASE HOST value. However I don't want to hard code it again somewhere and wonder how I can read it from app.yaml

My questions are:

1. Where can I find documentation about it ?

2. How may I reference this value in Python code?

George (Cloud Platform Support)

unread,
Mar 1, 2019, 6:37:14 PM3/1/19
to Google App Engine
App Engine allows developers to focus on doing what they do best, writing code. Based on Google Compute Engine, the App Engine flexible environment automatically scales your app up and down while balancing the load. Microservices, authorization, SQL and NoSQL databases, traffic splitting, logging, versioning, security scanning, and content delivery networks are all supported natively. In addition, the App Engine flexible environment allows you to customize the runtime and even the operating system of your virtual machine using Dockerfiles. You may find related information on the "App Engine Flexible Environment" documentation page

The "cloud_sql_instances: INSTANCE_CONNECTION_NAME" instruction from app.yaml is meant for this Flexible managed environment. It is difficult to view it as hard coding. In what concerns Django, this is your framework of choice, and you are in a position to take the most advantageous decisions. Instead of hard coding, you could, for instance, make use of the already set environment variables: 

env_variables:
    # Replace user, password, database, and instance connection name with the values obtained
    # when configuring your Cloud SQL instance.
    SQLALCHEMY_DATABASE_URI: >-
      mysql+pymysql://USER:PASSWORD@/DATABASE?unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME
#[END gae_flex_mysql_env]

This allows you to reference environment variables in your Python code. For an example, you could follow the example on the "Using Cloud SQL for MySQL" documentation page
 
# Environment variables are defined in app.yaml.
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

The above code excerpt illustrates how you may refer environment variables in your Python code. 
Reply all
Reply to author
Forward
0 new messages