Skip to content

Commit fc17c3f

Browse files
committed
update to example
1 parent 9857ac7 commit fc17c3f

File tree

3 files changed

+85
-43
lines changed

3 files changed

+85
-43
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**/terraform.tfstate*
2+
**/.terraform*
3+
**/backend.tf
4+
**/terraform.tfvars
5+
**/terraform.tfplan
6+
**/values-*.yaml

examples/mysql-and-postgres/README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,55 @@ terraform apply
1616

1717
## Test connection to database
1818

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:
19+
1. Install the Cloud SQL Proxy:
2220

21+
```bash
22+
wget https://dl.google.com/cloudsql/cloud_sql_proxy.$(uname | tr '[:upper:]' '[:lower:]').amd64 -O cloud_sql_proxy
23+
chmod +x cloud_sql_proxy
2324
```
24-
export 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}"
2825

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)
26+
2. Run the Cloud SQL proxy in the background:
3927

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}"
28+
```bash
29+
MYSQL_CONN_NAME=$(terraform output mysql_conn)
30+
PSQL_CONN_NAME=$(terraform output psql_conn)
4231

43-
./cloud_sql_proxy -instances=${PGSQL_CONN_NAME}=tcp:3306
32+
./cloud_sql_proxy -instances=${MYSQL_CONN_NAME}=tcp:3306,${PSQL_CONN_NAME}=tcp:5432 &
4433
```
4534

46-
Get the generated password:
35+
3. Get the generated user passwords:
4736

4837
```
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')
38+
echo MYSQL_PASSWORD=$(terraform output mysql_user_pass)
39+
echo PSQL_PASSWORD=$(terraform output psql_user_pass)
5140
```
5241

53-
Test the MySQL connection:
42+
4. Test the MySQL connection:
5443

5544
```
5645
mysql -udefault -p --host 127.0.0.1 default
5746
```
5847

5948
> When prompted, enter the value of MYSQL_PASSWORD
6049
61-
Test the PostgreSQL connection:
50+
5. Test the PostgreSQL connection:
6251

6352
```
6453
psql -h 127.0.0.1 --user default
6554
```
6655

67-
> When prompted, enter the value of PGSQL_PASSWORD
56+
> When prompted, enter the value of PSQL_PASSWORD
57+
58+
## Cleanup
59+
60+
1. Stop the Cloud SQL Proxy:
61+
62+
```bash
63+
killall cloud_sql_proxy
64+
```
65+
66+
2. Remove all resources created by terraform:
67+
68+
```bash
69+
terraform destroy
70+
```

examples/mysql-and-postgres/main.tf

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,62 @@
1414
* limitations under the License.
1515
*/
1616

17-
variable region {
17+
variable "region" {
1818
default = "us-central1"
1919
}
2020

21-
variable network {
21+
variable "network" {
2222
default = "default"
2323
}
2424

25-
variable zone {
25+
variable "zone" {
2626
default = "us-central1-b"
2727
}
2828

29-
provider google {
29+
variable "mysql_version" {
30+
default = "MYSQL_5_6"
31+
}
32+
33+
variable "postgresql_version" {
34+
default = "POSTGRES_9_6"
35+
}
36+
37+
provider "google" {
3038
region = "${var.region}"
3139
}
3240

33-
data "google_client_config" "current" {}
41+
variable "network_name" {
42+
default = "mysql-psql-example"
43+
}
44+
45+
resource "google_compute_network" "default" {
46+
name = "${var.network_name}"
47+
auto_create_subnetworks = "false"
48+
}
3449

35-
data "google_compute_subnetwork" "default-us-central1" {
36-
project = "${data.google_client_config.current.project}"
37-
region = "${var.region}"
38-
name = "default"
50+
resource "google_compute_subnetwork" "default" {
51+
name = "${var.network_name}"
52+
ip_cidr_range = "10.127.0.0/20"
53+
network = "${google_compute_network.default.self_link}"
54+
region = "${var.region}"
55+
private_ip_google_access = true
3956
}
4057

58+
data "google_client_config" "current" {}
59+
4160
resource "random_id" "name" {
4261
byte_length = 2
4362
}
4463

4564
module "mysql-db" {
46-
// source = "github.com/GoogleCloudPlatform/terraform-google-sql-db"
4765
source = "../../"
4866
name = "example-mysql-${random_id.name.hex}"
49-
database_version = "MYSQL_5_6"
67+
database_version = "${var.mysql_version}"
5068

5169
ip_configuration = [{
5270
authorized_networks = [{
53-
name = "default"
54-
value = "${data.google_compute_subnetwork.default-us-central1.ip_cidr_range}"
71+
name = "${var.network_name}"
72+
value = "${google_compute_subnetwork.default.ip_cidr_range}"
5573
}]
5674
}]
5775

@@ -64,16 +82,31 @@ module "mysql-db" {
6482
}
6583

6684
module "postgresql-db" {
67-
// source = "github.com/GoogleCloudPlatform/terraform-google-sql-db"
6885
source = "../../"
6986
name = "example-postgresql-${random_id.name.hex}"
7087
user_host = ""
71-
database_version = "POSTGRES_9_6"
88+
database_version = "${var.postgresql_version}"
7289

7390
ip_configuration = [{
7491
authorized_networks = [{
75-
name = "default"
76-
value = "${data.google_compute_subnetwork.default-us-central1.ip_cidr_range}"
92+
name = "${var.network_name}"
93+
value = "${google_compute_subnetwork.default.ip_cidr_range}"
7794
}]
7895
}]
7996
}
97+
98+
output "mysql_conn" {
99+
value = "${data.google_client_config.current.project}:${var.region}:${module.mysql-db.instance_name}"
100+
}
101+
102+
output "mysql_user_pass" {
103+
value = "${module.mysql-db.generated_user_password}"
104+
}
105+
106+
output "psql_conn" {
107+
value = "${data.google_client_config.current.project}:${var.region}:${module.postgresql-db.instance_name}"
108+
}
109+
110+
output "psql_user_pass" {
111+
value = "${module.postgresql-db.generated_user_password}"
112+
}

0 commit comments

Comments
 (0)