Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit 7367df3

Browse files
authored
Update README to match Network module, add root example. (#31)
Update README to match Network module, add root example.
2 parents fbc8b17 + a01bb2f commit 7367df3

File tree

4 files changed

+233
-5
lines changed

4 files changed

+233
-5
lines changed

README.md

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@
22

33
# Cloud SQL Modules
44

5-
This repo contains modules for running relational databases such as MySQL and PostgreSQL on Google's
6-
[Cloud SQL](https://cloud.google.com/sql/) on [GCP](https://cloud.google.com/).
5+
This repo contains modules for running relational databases such as MySQL and PostgreSQL on
6+
[Google Cloud Platform (GCP)](https://cloud.google.com/) using [Cloud SQL](https://cloud.google.com/sql/).
77

8-
## Code included in this Module
8+
## Quickstart
99

10-
* [cloud-sql](/modules/cloud-sql): Deploy a Cloud SQL [MySQL](https://cloud.google.com/sql/docs/mysql/) or [PostgreSQL](https://cloud.google.com/sql/docs/postgres/) cluster.
10+
If you want to quickly spin up a Cloud SQL database, you can run the example that is in the root of this repo. Check out
11+
[postgres-private-ip example documentation](https://github.com/gruntwork-io/terraform-google-sql/blob/master/examples/postgres-private-ip)
12+
for instructions.
1113

14+
## What's in this repo
15+
16+
This repo has the following folder structure:
17+
18+
* [root](https://github.com/gruntwork-io/terraform-google-sql/tree/master): The root folder contains an example of how
19+
to deploy a private PostgreSQL instance in Cloud SQL. See [postgres-private-ip](https://github.com/gruntwork-io/terraform-google-sql/blob/master/examples/postgres-private-ip)
20+
for the documentation.
21+
22+
* [modules](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules): This folder contains the
23+
main implementation code for this Module, broken down into multiple standalone submodules.
24+
25+
The primary module is:
26+
27+
* [cloud-sql](/modules/cloud-sql): Deploy a Cloud SQL [MySQL](https://cloud.google.com/sql/docs/mysql/) or
28+
[PostgreSQL](https://cloud.google.com/sql/docs/postgres/) database.
29+
30+
* [examples](https://github.com/gruntwork-io/terraform-google-sql/tree/master/examples): This folder contains
31+
examples of how to use the submodules.
32+
33+
* [test](https://github.com/gruntwork-io/terraform-google-sql/tree/master/test): Automated tests for the submodules
34+
and examples.
1235

1336
## What is Cloud SQL?
1437

@@ -22,6 +45,18 @@ your relational databases on Google Cloud Platform. Cloud SQL automatically incl
2245

2346
You can learn more about Cloud SQL from [the official documentation](https://cloud.google.com/sql/docs/).
2447

48+
## What's a Module?
49+
50+
A Module is a canonical, reusable, best-practices definition for how to run a single piece of infrastructure, such
51+
as a database or server cluster. Each Module is written using a combination of [Terraform](https://www.terraform.io/)
52+
and scripts (mostly bash) and include automated tests, documentation, and examples. It is maintained both by the open
53+
source community and companies that provide commercial support.
54+
55+
Instead of figuring out the details of how to run a piece of infrastructure from scratch, you can reuse
56+
existing code that has been proven in production. And instead of maintaining all that infrastructure code yourself,
57+
you can leverage the work of the Module community to pick up infrastructure improvements through
58+
a version number bump.
59+
2560
## Who maintains this Module?
2661

2762
This Module and its Submodules are maintained by [Gruntwork](http://www.gruntwork.io/). Read the [Gruntwork Philosophy](/GRUNTWORK_PHILOSOPHY.md) document to learn more about how Gruntwork builds production grade infrastructure code. If you are looking for help or
@@ -54,4 +89,6 @@ MINOR, and PATCH versions on each release to indicate any incompatibilities.
5489

5590
## License
5691

57-
Please see [LICENSE.txt](/LICENSE.txt) for details on how the code in this repo is licensed.
92+
Please see [LICENSE](/LICENSE) for how the code in this repo is licensed.
93+
94+
Copyright © 2019 Gruntwork, Inc.

main.tf

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# ------------------------------------------------------------------------------
2+
# LAUNCH A POSTGRES CLOUD SQL PRIVATE IP INSTANCE
3+
# ------------------------------------------------------------------------------
4+
5+
# ------------------------------------------------------------------------------
6+
# CONFIGURE OUR GCP CONNECTION
7+
# ------------------------------------------------------------------------------
8+
9+
provider "google-beta" {
10+
version = "~> 2.1.0"
11+
region = "${var.region}"
12+
project = "${var.project}"
13+
}
14+
15+
# Use Terraform 0.10.x so that we can take advantage of Terraform GCP functionality as a separate provider via
16+
# https://github.com/terraform-providers/terraform-provider-google
17+
terraform {
18+
required_version = ">= 0.10.3"
19+
}
20+
21+
# ------------------------------------------------------------------------------
22+
# CREATE A RANDOM SUFFIX AND PREPARE RESOURCE NAMES
23+
# ------------------------------------------------------------------------------
24+
25+
resource "random_id" "name" {
26+
byte_length = 2
27+
}
28+
29+
locals {
30+
# If name_override is specified, use that - otherwise use the name_prefix with a random string
31+
instance_name = "${length(var.name_override) == 0 ? format("%s-%s", var.name_prefix, random_id.name.hex) : var.name_override}"
32+
private_network_name = "private-network-${random_id.name.hex}"
33+
private_ip_name = "private-ip-${random_id.name.hex}"
34+
}
35+
36+
# ------------------------------------------------------------------------------
37+
# CREATE COMPUTE NETWORKS
38+
# ------------------------------------------------------------------------------
39+
40+
# Simple network, auto-creates subnetworks
41+
resource "google_compute_network" "private_network" {
42+
provider = "google-beta"
43+
name = "${local.private_network_name}"
44+
}
45+
46+
# Reserve global internal address range for the peering
47+
resource "google_compute_global_address" "private_ip_address" {
48+
provider = "google-beta"
49+
name = "${local.private_ip_name}"
50+
purpose = "VPC_PEERING"
51+
address_type = "INTERNAL"
52+
prefix_length = 16
53+
network = "${google_compute_network.private_network.self_link}"
54+
}
55+
56+
# Establish VPC network peering connection using the reserved address range
57+
resource "google_service_networking_connection" "private_vpc_connection" {
58+
provider = "google-beta"
59+
network = "${google_compute_network.private_network.self_link}"
60+
service = "servicenetworking.googleapis.com"
61+
reserved_peering_ranges = ["${google_compute_global_address.private_ip_address.name}"]
62+
}
63+
64+
# ------------------------------------------------------------------------------
65+
# CREATE DATABASE INSTANCE WITH PRIVATE IP
66+
# ------------------------------------------------------------------------------
67+
68+
module "postgres" {
69+
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
70+
# to a specific version of the modules, such as the following example:
71+
# source = "git::git@github.com:gruntwork-io/terraform-google-sql.git//modules/cloud-sql?ref=v0.1.0"
72+
source = "./modules/cloud-sql"
73+
74+
project = "${var.project}"
75+
region = "${var.region}"
76+
name = "${local.instance_name}"
77+
db_name = "${var.db_name}"
78+
79+
engine = "${var.postgres_version}"
80+
machine_type = "${var.machine_type}"
81+
82+
# These together will construct the master_user privileges, i.e.
83+
# 'master_user_name'@'master_user_host' IDENTIFIED BY 'master_user_password'.
84+
# These should typically be set as the environment variable TF_VAR_master_user_password, etc.
85+
# so you don't check these into source control."
86+
master_user_password = "${var.master_user_password}"
87+
88+
master_user_name = "${var.master_user_name}"
89+
master_user_host = "%"
90+
91+
# Pass the private network link to the module
92+
private_network = "${google_compute_network.private_network.self_link}"
93+
94+
# Wait for the vpc connection to complete
95+
wait_for = "${google_service_networking_connection.private_vpc_connection.network}"
96+
97+
custom_labels = {
98+
test-id = "postgres-private-ip-example"
99+
}
100+
}

outputs.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ------------------------------------------------------------------------------
2+
# MASTER OUTPUTS
3+
# ------------------------------------------------------------------------------
4+
5+
output "master_instance_name" {
6+
description = "The name of the database instance"
7+
value = "${module.postgres.master_instance_name}"
8+
}
9+
10+
output "master_ip_addresses" {
11+
description = "All IP addresses of the instance as list of maps, see https://www.terraform.io/docs/providers/google/r/sql_database_instance.html#ip_address-0-ip_address"
12+
value = "${module.postgres.master_ip_addresses}"
13+
}
14+
15+
output "master_private_ip" {
16+
description = "The private IPv4 address of the master instance"
17+
value = "${module.postgres.master_private_ip_address}"
18+
}
19+
20+
output "master_instance" {
21+
description = "Self link to the master instance"
22+
value = "${module.postgres.master_instance}"
23+
}
24+
25+
output "master_proxy_connection" {
26+
description = "Instance path for connecting with Cloud SQL Proxy. Read more at https://cloud.google.com/sql/docs/mysql/sql-proxy"
27+
value = "${module.postgres.master_proxy_connection}"
28+
}
29+
30+
# ------------------------------------------------------------------------------
31+
# DB OUTPUTS
32+
# ------------------------------------------------------------------------------
33+
34+
output "db_name" {
35+
description = "Name of the default database"
36+
value = "${module.postgres.db_name}"
37+
}
38+
39+
output "db" {
40+
description = "Self link to the default database"
41+
value = "${module.postgres.db}"
42+
}

variables.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# REQUIRED PARAMETERS
3+
# These variables are expected to be passed in by the operator
4+
# ---------------------------------------------------------------------------------------------------------------------
5+
6+
variable "project" {
7+
description = "The project ID to host the database in."
8+
}
9+
10+
variable "region" {
11+
description = "The region to host the database in."
12+
}
13+
14+
# Note, after a name db instance is used, it cannot be reused for up to one week.
15+
variable "name_prefix" {
16+
description = "The name prefix for the database instance. Will be appended with a random string. Use lowercase letters, numbers, and hyphens. Start with a letter."
17+
}
18+
19+
variable "master_user_name" {
20+
description = "The username part for the default user credentials, i.e. 'master_user_name'@'master_user_host' IDENTIFIED BY 'master_user_password'. This should typically be set as the environment variable TF_VAR_master_user_name so you don't check it into source control."
21+
}
22+
23+
variable "master_user_password" {
24+
description = "The password part for the default user credentials, i.e. 'master_user_name'@'master_user_host' IDENTIFIED BY 'master_user_password'. This should typically be set as the environment variable TF_VAR_master_user_password so you don't check it into source control."
25+
}
26+
27+
# ---------------------------------------------------------------------------------------------------------------------
28+
# OPTIONAL PARAMETERS
29+
# Generally, these values won't need to be changed.
30+
# ---------------------------------------------------------------------------------------------------------------------
31+
variable "postgres_version" {
32+
description = "The engine version of the database, e.g. `POSTGRES_9_6`. See https://cloud.google.com/sql/docs/db-versions for supported versions."
33+
default = "POSTGRES_9_6"
34+
}
35+
36+
variable "machine_type" {
37+
description = "The machine type to use, see https://cloud.google.com/sql/pricing for more details"
38+
default = "db-f1-micro"
39+
}
40+
41+
variable "db_name" {
42+
description = "Name for the db"
43+
default = "default"
44+
}
45+
46+
variable "name_override" {
47+
description = "You may optionally override the name_prefix + random string by specifying an override"
48+
default = ""
49+
}

0 commit comments

Comments
 (0)