|
| 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 | +} |
0 commit comments