11 Commits

Author SHA1 Message Date
autero1
400d8b6e13 [skip ci] Merge pull request #34 from gruntwork-io/fix_links
[skip ci] Fix links
2019-05-09 20:29:44 +03:00
Petri Autero
73a2344d6e [skip ci] Fix missing links 2019-05-09 19:53:22 +03:00
Petri Autero
6e2f52d9f6 [skip ci] Fix links 2019-05-09 17:56:33 +03:00
autero1
fdcd2e0c91 [skip ci] Merge pull request #33 from gruntwork-io/yorinasub17-patch-1
Update README.md
2019-05-09 17:08:17 +03:00
Yoriyasu Yano
33032faf64 Update README.md 2019-05-08 10:27:17 -07:00
autero1
24cb0ec259 Merge pull request #32 from gruntwork-io/module_dependencies
Standardizing dependencies to a list input
2019-05-04 09:03:41 +03:00
Petri Autero
2d11fdcf21 Revert back to local-exec 2019-05-03 11:50:42 +03:00
Riley Karson
7367df3e1e Update README to match Network module, add root example. (#31)
Update README to match Network module, add root example.
2019-05-02 09:05:25 -07:00
Petri Autero
efe37a1e64 Try to fix test failures 2019-05-02 14:22:52 +03:00
Petri Autero
6312a707c7 Standardizing to a list input 2019-05-02 10:34:20 +03:00
Riley Karson
a01bb2f379 Update README to match Network module, add root example. 2019-04-30 14:26:57 -07:00
17 changed files with 302 additions and 30 deletions

View File

@@ -1,5 +1,9 @@
# Contribution Guidelines
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
Contributions to this Module are very welcome! We follow a fairly standard [pull request process](
https://help.github.com/articles/about-pull-requests/) for contributions, subject to the following guidelines:

View File

@@ -1,14 +1,42 @@
[![Maintained by Gruntwork.io](https://img.shields.io/badge/maintained%20by-gruntwork.io-%235849a6.svg)](https://gruntwork.io/?ref=repo_google_cloudsql)
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gruntwork-io/terraform-google-sql.svg?label=latest)](http://github.com/gruntwork-io/terraform-google-sql/releases/latest)
# Cloud SQL Modules
This repo contains modules for running relational databases such as MySQL and PostgreSQL on Google's
[Cloud SQL](https://cloud.google.com/sql/) on [GCP](https://cloud.google.com/).
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
## Code included in this Module
This repo contains modules for running relational databases such as MySQL and PostgreSQL on
[Google Cloud Platform (GCP)](https://cloud.google.com/) using [Cloud SQL](https://cloud.google.com/sql/).
* [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.
## Quickstart
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
[postgres-private-ip example documentation](https://github.com/gruntwork-io/terraform-google-sql/blob/master/examples/postgres-private-ip)
for instructions.
## What's in this repo
This repo has the following folder structure:
* [root](https://github.com/gruntwork-io/terraform-google-sql/tree/master): The root folder contains an example of how
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)
for the documentation.
* [modules](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules): This folder contains the
main implementation code for this Module, broken down into multiple standalone submodules.
The primary module is:
* [cloud-sql](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql): Deploy a Cloud SQL [MySQL](https://cloud.google.com/sql/docs/mysql/) or
[PostgreSQL](https://cloud.google.com/sql/docs/postgres/) database.
* [examples](https://github.com/gruntwork-io/terraform-google-sql/tree/master/examples): This folder contains
examples of how to use the submodules.
* [test](https://github.com/gruntwork-io/terraform-google-sql/tree/master/test): Automated tests for the submodules
and examples.
## What is Cloud SQL?
@@ -22,9 +50,21 @@ your relational databases on Google Cloud Platform. Cloud SQL automatically incl
You can learn more about Cloud SQL from [the official documentation](https://cloud.google.com/sql/docs/).
## What's a Module?
A Module is a canonical, reusable, best-practices definition for how to run a single piece of infrastructure, such
as a database or server cluster. Each Module is written using a combination of [Terraform](https://www.terraform.io/)
and scripts (mostly bash) and include automated tests, documentation, and examples. It is maintained both by the open
source community and companies that provide commercial support.
Instead of figuring out the details of how to run a piece of infrastructure from scratch, you can reuse
existing code that has been proven in production. And instead of maintaining all that infrastructure code yourself,
you can leverage the work of the Module community to pick up infrastructure improvements through
a version number bump.
## Who maintains this Module?
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
This Module and its Submodules are maintained by [Gruntwork](http://www.gruntwork.io/). Read the [Gruntwork Philosophy](https://github.com/gruntwork-io/terraform-google-sql/blob/master/GRUNTWORK_PHILOSOPHY.md) document to learn more about how Gruntwork builds production grade infrastructure code. If you are looking for help or
commercial support, send an email to
[support@gruntwork.io](mailto:support@gruntwork.io?Subject=Google%20SQL%20Module).
@@ -39,13 +79,13 @@ Gruntwork can help with:
## How do I contribute to this Module?
Contributions are very welcome! Check out the [Contribution Guidelines](/CONTRIBUTING.md) for instructions.
Contributions are very welcome! Check out the [Contribution Guidelines](https://github.com/gruntwork-io/terraform-google-sql/blob/master/CONTRIBUTING.md) for instructions.
## How is this Module versioned?
This Module follows the principles of [Semantic Versioning](http://semver.org/). You can find each new release, along
with the changelog, in the [Releases Page](../../releases).
with the changelog, in the [Releases Page](https://github.com/gruntwork-io/terraform-google-sql/releases).
During initial development, the major version will be 0 (e.g., `0.x.y`), which indicates the code does not yet have a
stable API. Once we hit `1.0.0`, we will make every effort to maintain a backwards compatible API and use the MAJOR,
@@ -54,4 +94,6 @@ MINOR, and PATCH versions on each release to indicate any incompatibilities.
## License
Please see [LICENSE.txt](/LICENSE.txt) for details on how the code in this repo is licensed.
Please see [LICENSE](https://github.com/gruntwork-io/terraform-google-sql/blob/master/LICENSE.txt) for how the code in this repo is licensed.
Copyright &copy; 2019 Gruntwork, Inc.

View File

@@ -13,7 +13,7 @@ Creating the certificate while there are other operations ongoing will result in
To run this example, you need to:
1. Install [Terraform](https://www.terraform.io/).
1. Open up `vars.tf` and set secrets at the top of the file as environment variables and fill in any other variables in
1. Open up `variables.tf` and set secrets at the top of the file as environment variables and fill in any other variables in
the file that don't have defaults.
1. `terraform init`.
1. `terraform plan`.

View File

@@ -1,6 +1,6 @@
# MySQL Cloud SQL Private IP Example
This folder contains an example of how to use the [Cloud SQL module](/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
This folder contains an example of how to use the [Cloud SQL module](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
[MySQL](https://cloud.google.com/sql/docs/mysql/) database instance with a [private IP address](https://cloud.google.com/sql/docs/mysql/private-ip).
## How do you run this example?
@@ -18,4 +18,4 @@ When the templates are applied, Terraform will output the IP address of the inst
and the instance path for [connecting using the Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/connect-admin-proxy).
Note that you cannot connect to the private IP instance from outside Google Cloud Platform.
If you want to experiment with connecting from your own workstation, see the [public IP example](../mysql-public-ip)
If you want to experiment with connecting from your own workstation, see the [public IP example](https://github.com/gruntwork-io/terraform-google-sql/tree/master/examples/mysql-public-ip)

View File

@@ -92,7 +92,7 @@ module "mysql" {
private_network = "${google_compute_network.private_network.self_link}"
# Wait for the vpc connection to complete
wait_for = "${google_service_networking_connection.private_vpc_connection.network}"
dependencies = ["${google_service_networking_connection.private_vpc_connection.network}"]
# Set auto-increment flags to test the
# feature during automated testing

View File

@@ -1,6 +1,6 @@
# MySQL Cloud SQL Public IP Example
This folder contains an example of how to use the [Cloud SQL module](/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
This folder contains an example of how to use the [Cloud SQL module](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
[MySQL](https://cloud.google.com/sql/docs/mysql/) database instance with a [public IP address](https://cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP).
## How do you run this example?

View File

@@ -1,6 +1,10 @@
# MySQL Cloud SQL HA Example
This folder contains an example of how to use the [Cloud SQL module](/modules/cloud-sql) to create a [High Availability](https://cloud.google.com/sql/docs/mysql/configure-ha) [Google Cloud SQL](https://cloud.google.com/sql/)
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
This folder contains an example of how to use the [Cloud SQL module](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql) to create a [High Availability](https://cloud.google.com/sql/docs/mysql/configure-ha) [Google Cloud SQL](https://cloud.google.com/sql/)
[MySQL](https://cloud.google.com/sql/docs/mysql/) database cluster with a [public IP](https://cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP) and failover and [read replicas](https://cloud.google.com/sql/docs/mysql/replication/).
## How do you run this example?

View File

@@ -1,6 +1,10 @@
# PostgreSQL Cloud SQL Private IP Example
This folder contains an example of how to use the [Cloud SQL module](/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
This folder contains an example of how to use the [Cloud SQL module](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
[PostgreSQL](https://cloud.google.com/sql/docs/postgres/) database instance with a [private IP address](https://cloud.google.com/sql/docs/postgres/private-ip).
## How do you run this example?
@@ -18,4 +22,4 @@ When the templates are applied, Terraform will output the IP address of the inst
and the instance path for [connecting using the Cloud SQL Proxy](https://cloud.google.com/sql/docs/postgres/sql-proxy).
Note that you cannot connect to the private IP instance from outside Google Cloud Platform.
If you want to experiment with connecting from your own workstation, see the [public IP example](../postgres-public-ip)
If you want to experiment with connecting from your own workstation, see the [public IP example](https://github.com/gruntwork-io/terraform-google-sql/tree/master/examples/postgres-public-ip)

View File

@@ -92,7 +92,7 @@ module "postgres" {
private_network = "${google_compute_network.private_network.self_link}"
# Wait for the vpc connection to complete
wait_for = "${google_service_networking_connection.private_vpc_connection.network}"
dependencies = ["${google_service_networking_connection.private_vpc_connection.network}"]
custom_labels = {
test-id = "postgres-private-ip-example"

View File

@@ -1,6 +1,10 @@
# PostgreSQL Cloud SQL Public IP Example
This folder contains an example of how to use the [Cloud SQL module](/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
This folder contains an example of how to use the [Cloud SQL module](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql) to create a [Google Cloud SQL](https://cloud.google.com/sql/)
[PostgreSQL](https://cloud.google.com/sql/docs/postgres/) database instance with a [public IP address](https://cloud.google.com/sql/docs/postgres/connect-external-app#appaccessIP).
## How do you run this example?

View File

@@ -1,6 +1,10 @@
# PostgreSQL Cloud SQL HA Example
This folder contains an example of how to use the [Cloud SQL module](/modules/cloud-sql) to create a [High Availability](https://cloud.google.com/sql/docs/postgres/high-availability) [Google Cloud SQL](https://cloud.google.com/sql/)
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
This folder contains an example of how to use the [Cloud SQL module](https://github.com/gruntwork-io/terraform-google-sql/tree/master/modules/cloud-sql) to create a [High Availability](https://cloud.google.com/sql/docs/postgres/high-availability) [Google Cloud SQL](https://cloud.google.com/sql/)
[PostgreSQL](https://cloud.google.com/sql/docs/postgres/) database cluster with a [public IP address](https://cloud.google.com/sql/docs/postgres/connect-external-app#appaccessIP) and a [read replica](https://cloud.google.com/sql/docs/postgres/replication/).
## How do you run this example?

100
main.tf Normal file
View File

@@ -0,0 +1,100 @@
# ------------------------------------------------------------------------------
# LAUNCH A POSTGRES CLOUD SQL PRIVATE IP INSTANCE
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# CONFIGURE OUR GCP CONNECTION
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}
# Use Terraform 0.10.x so that we can take advantage of Terraform GCP functionality as a separate provider via
# https://github.com/terraform-providers/terraform-provider-google
terraform {
required_version = ">= 0.10.3"
}
# ------------------------------------------------------------------------------
# CREATE A RANDOM SUFFIX AND PREPARE RESOURCE NAMES
# ------------------------------------------------------------------------------
resource "random_id" "name" {
byte_length = 2
}
locals {
# If name_override is specified, use that - otherwise use the name_prefix with a random string
instance_name = "${length(var.name_override) == 0 ? format("%s-%s", var.name_prefix, random_id.name.hex) : var.name_override}"
private_network_name = "private-network-${random_id.name.hex}"
private_ip_name = "private-ip-${random_id.name.hex}"
}
# ------------------------------------------------------------------------------
# CREATE COMPUTE NETWORKS
# ------------------------------------------------------------------------------
# Simple network, auto-creates subnetworks
resource "google_compute_network" "private_network" {
provider = "google-beta"
name = "${local.private_network_name}"
}
# Reserve global internal address range for the peering
resource "google_compute_global_address" "private_ip_address" {
provider = "google-beta"
name = "${local.private_ip_name}"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "${google_compute_network.private_network.self_link}"
}
# Establish VPC network peering connection using the reserved address range
resource "google_service_networking_connection" "private_vpc_connection" {
provider = "google-beta"
network = "${google_compute_network.private_network.self_link}"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["${google_compute_global_address.private_ip_address.name}"]
}
# ------------------------------------------------------------------------------
# CREATE DATABASE INSTANCE WITH PRIVATE IP
# ------------------------------------------------------------------------------
module "postgres" {
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
# to a specific version of the modules, such as the following example:
# source = "git::git@github.com:gruntwork-io/terraform-google-sql.git//modules/cloud-sql?ref=v0.1.0"
source = "./modules/cloud-sql"
project = "${var.project}"
region = "${var.region}"
name = "${local.instance_name}"
db_name = "${var.db_name}"
engine = "${var.postgres_version}"
machine_type = "${var.machine_type}"
# These together will construct the master_user privileges, i.e.
# 'master_user_name'@'master_user_host' IDENTIFIED BY 'master_user_password'.
# These should typically be set as the environment variable TF_VAR_master_user_password, etc.
# so you don't check these into source control."
master_user_password = "${var.master_user_password}"
master_user_name = "${var.master_user_name}"
master_user_host = "%"
# Pass the private network link to the module
private_network = "${google_compute_network.private_network.self_link}"
# Wait for the vpc connection to complete
wait_for = "${google_service_networking_connection.private_vpc_connection.network}"
custom_labels = {
test-id = "postgres-private-ip-example"
}
}

View File

@@ -1,5 +1,9 @@
# Cloud SQL Module
<!-- NOTE: We use absolute linking here instead of relative linking, because the terraform registry does not support
relative linking correctly.
-->
This module creates a [Google Cloud SQL](https://cloud.google.com/sql/) cluster.
The cluster is managed by Google, automating backups, replication, patches, and updates.
@@ -7,12 +11,12 @@ This module helps you run [MySQL](https://cloud.google.com/sql/docs/mysql/) and
## How do you use this module?
See the [examples](/examples) folder for an example.
See the [examples](https://github.com/gruntwork-io/terraform-google-sql/tree/master/examples) folder for an example.
## How do you configure this module?
This module allows you to configure a number of parameters, such as high availability, backup windows, maintenance window and replicas.
For a list of all available variables and their descriptions, see [variables.tf](./variables.tf).
For a list of all available variables and their descriptions, see [variables.tf](https://github.com/gruntwork-io/terraform-google-sql/blob/master/modules/cloud-sql/variables.tf).
## How do you connect to the database?
@@ -92,7 +96,7 @@ See https://github.com/terraform-providers/terraform-provider-google/issues/2446
Retrieving and outputting distinct values from list of maps is not possible with resources using `count` prior to `terraform 0.12`.
Instead we have to output the values JSON encoded - for example `read_replica_server_ca_certs`. For full details of the outputs and
their format, see [outputs.tf](./outputs.tf).
their format, see [outputs.tf](https://github.com/gruntwork-io/terraform-google-sql/blob/master/modules/cloud-sql/outputs.tf).
See https://github.com/hashicorp/terraform/issues/17048

View File

@@ -30,7 +30,7 @@ locals {
# ------------------------------------------------------------------------------
resource "google_sql_database_instance" "master" {
depends_on = ["null_resource.wait_for"]
depends_on = ["null_resource.dependency_getter"]
provider = "google-beta"
name = "${var.name}"
@@ -111,11 +111,16 @@ resource "google_sql_user" "default" {
}
# ------------------------------------------------------------------------------
# CREATE A NULL RESOURCE TO EMULATE DEPENDENCIES
# SET MODULE DEPENDENCY RESOURCE
# This works around a terraform limitation where we can not specify module dependencies natively.
# See https://github.com/hashicorp/terraform/issues/1178 for more discussion.
# By resolving and computing the dependencies list, we are able to make all the resources in this module depend on the
# resources backing the values in the dependencies list.
# ------------------------------------------------------------------------------
resource "null_resource" "wait_for" {
triggers = {
instance = "${var.wait_for}"
resource "null_resource" "dependency_getter" {
provisioner "local-exec" {
command = "echo ${length(var.dependencies)}"
}
}

View File

@@ -208,7 +208,17 @@ variable "resource_timeout" {
default = "60m"
}
variable "wait_for" {
description = "By passing a value to this variable, you can effectively tell this module to wait to deploy until the given variable's value is resolved, which is a way to require that this module depend on some other module. Note that the actual value of this variable doesn't matter."
default = ""
# ---------------------------------------------------------------------------------------------------------------------
# MODULE DEPENDENCIES
# Workaround Terraform limitation where there is no module depends_on.
# See https://github.com/hashicorp/terraform/issues/1178 for more details.
# This can be used to make sure the module resources are created after other bootstrapping resources have been created.
# For example:
# dependencies = ["${google_service_networking_connection.private_vpc_connection.network}"]
# ---------------------------------------------------------------------------------------------------------------------
variable "dependencies" {
description = "Create a dependency between the resources in this module to the interpolated values in this list (and thus the source resources). In other words, the resources in this module will now depend on the resources backing the values in this list such that those resources need to be created before the resources in this module, and the resources in this module need to be destroyed before the resources in the list."
type = "list"
default = []
}

42
outputs.tf Normal file
View File

@@ -0,0 +1,42 @@
# ------------------------------------------------------------------------------
# MASTER OUTPUTS
# ------------------------------------------------------------------------------
output "master_instance_name" {
description = "The name of the database instance"
value = "${module.postgres.master_instance_name}"
}
output "master_ip_addresses" {
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"
value = "${module.postgres.master_ip_addresses}"
}
output "master_private_ip" {
description = "The private IPv4 address of the master instance"
value = "${module.postgres.master_private_ip_address}"
}
output "master_instance" {
description = "Self link to the master instance"
value = "${module.postgres.master_instance}"
}
output "master_proxy_connection" {
description = "Instance path for connecting with Cloud SQL Proxy. Read more at https://cloud.google.com/sql/docs/mysql/sql-proxy"
value = "${module.postgres.master_proxy_connection}"
}
# ------------------------------------------------------------------------------
# DB OUTPUTS
# ------------------------------------------------------------------------------
output "db_name" {
description = "Name of the default database"
value = "${module.postgres.db_name}"
}
output "db" {
description = "Self link to the default database"
value = "${module.postgres.db}"
}

49
variables.tf Normal file
View File

@@ -0,0 +1,49 @@
# ---------------------------------------------------------------------------------------------------------------------
# REQUIRED PARAMETERS
# These variables are expected to be passed in by the operator
# ---------------------------------------------------------------------------------------------------------------------
variable "project" {
description = "The project ID to host the database in."
}
variable "region" {
description = "The region to host the database in."
}
# Note, after a name db instance is used, it cannot be reused for up to one week.
variable "name_prefix" {
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."
}
variable "master_user_name" {
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."
}
variable "master_user_password" {
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."
}
# ---------------------------------------------------------------------------------------------------------------------
# OPTIONAL PARAMETERS
# Generally, these values won't need to be changed.
# ---------------------------------------------------------------------------------------------------------------------
variable "postgres_version" {
description = "The engine version of the database, e.g. `POSTGRES_9_6`. See https://cloud.google.com/sql/docs/db-versions for supported versions."
default = "POSTGRES_9_6"
}
variable "machine_type" {
description = "The machine type to use, see https://cloud.google.com/sql/pricing for more details"
default = "db-f1-micro"
}
variable "db_name" {
description = "Name for the db"
default = "default"
}
variable "name_override" {
description = "You may optionally override the name_prefix + random string by specifying an override"
default = ""
}