This sample application demonstrates how to store data in Google Cloud SQL with a SQL Server database when running in Google App Engine Flexible Environment.
-
Follow the set-up instructions in the documentation.
-
Enable APIs for your project. Click here to visit Cloud Platform Console and enable the Google Cloud SQL API.
-
Install the Google Cloud SDK. The Google Cloud SDK is required to deploy .NET applications to App Engine.
-
Install the .NET Core SDK, version 2.0 or newer.
-
Create a Cloud SQL for SQL Server instance by following these instructions. Note the instance connection name, database user, and database password that you create.
-
Create a database for your application by following these instructions. Note the database name.
-
Replace
your-project-id
in appsettings.json with your Google Cloud Project's project id.
To run this application locally, download and install the cloud_sql_proxy
by
following the instructions
here.
Instructions are provided below for using the proxy with a TCP connection or a Unix Domain Socket. On Linux or Mac OS you can use either option, but on Windows the proxy currently requires a TCP connection.
To run the sample locally with a TCP connection, set environment variables and launch the proxy as shown below.
Use these terminal commands to initialize environment variables:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
export INSTANCE_HOST='127.0.0.1'
export DB_USER='<DB_USER_NAME>'
export DB_PASS='<DB_PASSWORD>'
export DB_NAME='<DB_NAME>'
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as Cloud Secret Manager to help keep secrets safe.
Then use this command to launch the proxy in the background:
./cloud_sql_proxy -instances=<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>=tcp:1433 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
Finally, run the following commands:
dotnet restore
dotnet run
Use these PowerShell commands to initialize environment variables:
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
$env:INSTANCE_HOST="127.0.0.1"
$env:DB_USER="<DB_USER_NAME>"
$env:DB_PASS="<DB_PASSWORD>"
$env:DB_NAME="<DB_NAME>"
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as Cloud Secret Manager to help keep secrets safe.
Then use this command to launch the proxy in a separate PowerShell session:
Start-Process -filepath "C:\<path to proxy exe>" -ArgumentList "-instances=<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>=tcp:1433 -credential_file=<CREDENTIALS_JSON_FILE>"
Finally, run the following commands:
PS > dotnet restore
PS > dotnet run
Download and run the Google Cloud SQL Proxy.
Then, navigate to the project options menu. Select the default configuration, then set the following environment variables:
INSTANCE_HOST : '127.0.0.1'
DB_USER : '<DB_USER_NAME>'
DB_PASS : '<DB_PASSWORD>'
DB_NAME : '<DB_NAME>'
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as Cloud Secret Manager to help keep secrets safe.
Finally, run the application by clicking the arrow button in the main toolbar, or by pressing F5.
-
Edit app.yaml. Replace
<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
with your instance connection name. Update the values forDB_USER
,DB_PASS
, andDB_NAME
-
Build and deploy the application:
PS > dotnet restore PS > dotnet publish PS > gcloud beta app deploy .\bin\Debug\netcoreapp2.1\publish\app.yaml
dotnet restore dotnet publish gcloud beta app deploy ./bin/Debug/netcoreapp2.1/publish/app.yaml