Skip to content

Commit e442320

Browse files
committed
import of examples
1 parent ddf58e4 commit e442320

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cloud_sql_proxy
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Cloud SQL Database Example
2+
3+
This example shows how create Cloud SQL databases for for MySQL and PostgreSQL using the Terraform module.
4+
5+
**Figure 1.** *diagram of Google Cloud resources*
6+
7+
![architecture diagram](./diagram.png)
8+
9+
## Run Terraform
10+
11+
```
12+
terraform init
13+
terraform plan
14+
terraform apply
15+
```
16+
17+
## Test connection to database
18+
19+
Install the Cloud SQL Proxy: https://cloud.google.com/sql/docs/mysql/sql-proxy#install
20+
21+
Run the Cloud SQL proxy for MySQL instance:
22+
23+
```
24+
GOOGLE_PROJECT=$(gcloud config get-value project)
25+
26+
MYSQL_DB_NAME=$(terraform output -module mysql-db -json | jq -r '.instance_name.value')
27+
MYSQL_CONN_NAME="${GOOGLE_PROJECT}:us-central1:${MYSQL_DB_NAME}"
28+
29+
PGSQL_DB_NAME=$(terraform output -module postgresql-db -json | jq -r '.instance_name.value')
30+
PGSQL_CONN_NAME="${GOOGLE_PROJECT}:us-central1:${PGSQL_DB_NAME}"
31+
32+
./cloud_sql_proxy -instances=${MYSQL_CONN_NAME}=tcp:3306,${PGSQL_CONN_NAME}=tcp:5432
33+
```
34+
35+
Start Cloud SQL Proxy for Postgres instance:
36+
37+
```
38+
GOOGLE_PROJECT=$(gcloud config get-value project)
39+
40+
PGSQL_DB_NAME=$(terraform output -module postgresql-db -json | jq -r '.instance_name.value')
41+
PGSQL_CONN_NAME="${GOOGLE_PROJECT}:us-central1:${PGSQL_DB_NAME}"
42+
43+
./cloud_sql_proxy -instances=${PGSQL_CONN_NAME}=tcp:3306
44+
```
45+
46+
Get the generated password:
47+
48+
```
49+
echo MYSQL_PASSWORD=$(terraform output -module mysql-db -json | jq -r '.generated_user_password.value')
50+
echo PGSQL_PASSWORD=$(terraform output -module postgresql-db -json | jq -r '.generated_user_password.value')
51+
```
52+
53+
Test the MySQL connection:
54+
55+
```
56+
mysql -udefault -p --host 127.0.0.1 default
57+
```
58+
59+
> When prompted, enter the value of MYSQL_PASSWORD
60+
61+
Test the PostgreSQL connection:
62+
63+
```
64+
psql -h 127.0.0.1 --user default
65+
```
66+
67+
> When prompted, enter the value of PGSQL_PASSWORD
53.5 KB
Loading

examples/mysql-and-postgre/main.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable region {
18+
default = "us-central1"
19+
}
20+
21+
variable network {
22+
default = "default"
23+
}
24+
25+
variable zone {
26+
default = "us-central1-b"
27+
}
28+
29+
provider google {
30+
region = "${var.region}"
31+
}
32+
33+
resource "random_id" "name" {
34+
byte_length = 2
35+
}
36+
37+
module "mysql-db" {
38+
// source = "github.com/GoogleCloudPlatform/terraform-google-sql-db"
39+
source = "../../"
40+
name = "example-mysql-${random_id.name.hex}"
41+
database_version = "MYSQL_5_6"
42+
}
43+
44+
module "postgresql-db" {
45+
// source = "github.com/GoogleCloudPlatform/terraform-google-sql-db"
46+
source = "../../"
47+
name = "example-postgresql-${random_id.name.hex}"
48+
user_host = ""
49+
database_version = "POSTGRES_9_6"
50+
}

0 commit comments

Comments
 (0)