4 Commits

Author SHA1 Message Date
autero1
fbc8b17362 Merge pull request #30 from gruntwork-io/ip_address_fix
IP address outputs and private_network cleanup
2019-03-28 19:50:20 +02:00
Petri Autero
9de4b4296c Provider now allows empty private_network 2019-03-28 14:05:43 +02:00
Petri Autero
19a826afa9 Restore the master_ip_addresses because they're required by output validation 2019-03-28 13:16:18 +02:00
Petri Autero
b4a4262839 Use the recently introduced ip outputs 2019-03-28 12:45:31 +02:00
15 changed files with 75 additions and 83 deletions

View File

@@ -7,6 +7,7 @@
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}

View File

@@ -13,8 +13,8 @@ output "master_ip_addresses" {
}
output "master_private_ip" {
description = "The first IPv4 address of the addresses assigned to the instance. As this instance has only private IP, it is the private IP address."
value = "${module.mysql.master_first_ip_address}"
description = "The private IPv4 address of the master instance."
value = "${module.mysql.master_private_ip_address}"
}
output "master_instance" {

View File

@@ -7,6 +7,7 @@
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}

View File

@@ -7,14 +7,9 @@ output "master_instance_name" {
value = "${module.mysql.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.mysql.master_ip_addresses}"
}
output "master_public_ip" {
description = "The first IPv4 address of the addresses assigned to the instance. As this instance has only public IP, it is the public IP address."
value = "${module.mysql.master_first_ip_address}"
description = "The public IPv4 address of the master instance."
value = "${module.mysql.master_public_ip_address}"
}
output "master_ca_cert" {

View File

@@ -7,6 +7,7 @@
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}

View File

@@ -7,14 +7,9 @@ output "master_instance_name" {
value = "${module.mysql.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.mysql.master_ip_addresses}"
}
output "master_public_ip" {
description = "The first IPv4 address of the addresses assigned to the master instance. As this instance has only public IP, it is the public IP address."
value = "${module.mysql.master_first_ip_address}"
description = "The public IPv4 address of the master instance."
value = "${module.mysql.master_public_ip_address}"
}
output "master_instance" {
@@ -56,8 +51,8 @@ output "failover_instance_name" {
}
output "failover_public_ip" {
description = "The first IPv4 address of the addresses assigned to the failover instance. As this instance has only public IP, it is the public IP address."
value = "${module.mysql.failover_first_ip_address}"
description = "The public IPv4 address of the failover instance"
value = "${module.mysql.failover_public_ip_address}"
}
output "failover_proxy_connection" {
@@ -75,8 +70,8 @@ output "read_replica_instance_names" {
}
output "read_replica_public_ips" {
description = "List of first IPv4 addresses of the addresses assigned to the read replica instances. As the instances have only public IP in the example, the are the public IP addresses."
value = ["${module.mysql.read_replica_first_ip_addresses}"]
description = "List of public IPv4 addresses of the read replica instances"
value = ["${module.mysql.read_replica_public_ip_addresses}"]
}
output "read_replica_instances" {

View File

@@ -7,6 +7,7 @@
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}

View File

@@ -13,8 +13,8 @@ output "master_ip_addresses" {
}
output "master_private_ip" {
description = "The first IPv4 address of the addresses assigned to the instance. As this instance has only private IP, it is the private IP address."
value = "${module.postgres.master_first_ip_address}"
description = "The private IPv4 address of the master instance"
value = "${module.postgres.master_private_ip_address}"
}
output "master_instance" {

View File

@@ -7,6 +7,7 @@
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}

View File

@@ -7,14 +7,9 @@ output "master_instance_name" {
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_public_ip" {
description = "The first IPv4 address of the addresses assigned to the instance. As this instance has only public IP, it is the public IP address."
value = "${module.postgres.master_first_ip_address}"
description = "The public IPv4 address of the master instance"
value = "${module.postgres.master_public_ip_address}"
}
output "master_ca_cert" {

View File

@@ -7,6 +7,7 @@
# ------------------------------------------------------------------------------
provider "google-beta" {
version = "~> 2.1.0"
region = "${var.region}"
project = "${var.project}"
}

View File

@@ -7,14 +7,9 @@ output "master_instance_name" {
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_public_ip" {
description = "The first IPv4 address of the addresses assigned to the master instance. As this instance has only public IP, it is the public IP address."
value = "${module.postgres.master_first_ip_address}"
description = "The public IPv4 address of the master instance"
value = "${module.postgres.master_public_ip_address}"
}
output "master_instance" {
@@ -51,8 +46,8 @@ output "read_replica_instance_names" {
}
output "read_replica_public_ips" {
description = "List of first IPv4 addresses of the addresses assigned to the read replica instances. As the instances have only public IP in the example, the are the public IP addresses."
value = ["${module.postgres.read_replica_first_ip_addresses}"]
description = "List of public IPv4 addresses of the read replica instances"
value = ["${module.postgres.read_replica_public_ip_addresses}"]
}
output "read_replica_instances" {

View File

@@ -25,16 +25,17 @@ You can also use the [Cloud SQL Proxy for MySQL](https://cloud.google.com/sql/do
to connect to an instance that is also configured to use private IP. The proxy can connect using either the private IP address or a public IP address.
This module provides the connection details as [Terraform output
variables](https://www.terraform.io/intro/getting-started/outputs.html):
variables](https://www.terraform.io/intro/getting-started/outputs.html). Use the public / private addresses depending on your configuration:
1. **Master First IP Address** `master_first_ip_address`: The first IPv4 address of the addresses assigned to the instance. If the instance has only public IP, it is the [public IP address](https://cloud.google.com/sql/docs/mysql/connect-admin-ip). If it has only private IP, it the [private IP address](https://cloud.google.com/sql/docs/mysql/private-ip). If it has both, it is the first item in the list and full IP address details are in `master_ip_addresses`.
1. **Master Public IP Address** `master_public_ip_address`: The public IPv4 address of the master instance.
1. **Master Private IP Address** `master_private_ip_address`: The private IPv4 address of the master instance.
1. **Master Proxy connection** `master_proxy_connection`: Instance path for connecting with Cloud SQL Proxy; see [Connecting mysql Client Using the Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/connect-admin-proxy).
1. **Read Replica First IP Addresses** `read_replica_first_ip_addresses`: A list of all read replica IP addresses in the cluster. Use these addresses for reads (see "How do you scale this database?" below).
1. **Read Replica Public IP Addresses** `read_replica_public_ip_addresses`: A list of read replica public IP addresses in the cluster. Use these addresses for reads (see "How do you scale this database?" below).
1. **Read Replica Private IP Addresses** `read_replica_private_ip_addresses`: A list of read replica private IP addresses in the cluster. Use these addresses for reads (see "How do you scale this database?" below).
1. **Read Replica Proxy Connections** `read_replica_proxy_connections`: A list of instance paths for connecting with Cloud SQL Proxy; see [Connecting Using the Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/connect-admin-proxy).
You can programmatically extract these variables in your Terraform templates and pass them to other resources.
You'll also see the variables at the end of each `terraform apply` call or if you run `terraform output`.

View File

@@ -20,31 +20,6 @@ locals {
actual_binary_log_enabled = "${local.is_postgres ? false : var.mysql_binary_log_enabled}"
actual_availability_type = "${local.is_postgres && var.enable_failover_replica ? "REGIONAL" : "ZONAL"}"
actual_failover_replica_count = "${local.is_postgres ? 0 : var.enable_failover_replica ? 1 : 0}"
# Terraform does not allow using lists of maps with coditionals, so we have to
# trick terraform by creating a string conditional first.
# See https://github.com/hashicorp/terraform/issues/12453
ip_configuration_key = "${var.private_network != "" ? "PRIVATE" : "PUBLIC"}"
ip_configuration_def = {
"PRIVATE" = [{
authorized_networks = ["${var.authorized_networks}"]
ipv4_enabled = "${var.enable_public_internet_access}"
private_network = "${var.private_network}"
require_ssl = "${var.require_ssl}"
}]
"PUBLIC" = [{
authorized_networks = ["${var.authorized_networks}"]
ipv4_enabled = "${var.enable_public_internet_access}"
require_ssl = "${var.require_ssl}"
}]
}
# We have to construct the sub-block dynamically. If the user creates a public-ip only instance,
# passing an empty string into 'private_network' causes
# 'private_network" ("") doesn't match regexp "projects/...'
ip_configuration = "${local.ip_configuration_def[local.ip_configuration_key]}"
}
# ------------------------------------------------------------------------------
@@ -69,7 +44,12 @@ resource "google_sql_database_instance" "master" {
authorized_gae_applications = ["${var.authorized_gae_applications}"]
disk_autoresize = "${var.disk_autoresize}"
ip_configuration = ["${local.ip_configuration}"]
ip_configuration {
authorized_networks = ["${var.authorized_networks}"]
ipv4_enabled = "${var.enable_public_internet_access}"
private_network = "${var.private_network}"
require_ssl = "${var.require_ssl}"
}
location_preference {
follow_gae_application = "${var.follow_gae_application}"
@@ -173,7 +153,12 @@ resource "google_sql_database_instance" "failover_replica" {
authorized_gae_applications = ["${var.authorized_gae_applications}"]
disk_autoresize = "${var.disk_autoresize}"
ip_configuration = ["${local.ip_configuration}"]
ip_configuration {
authorized_networks = ["${var.authorized_networks}"]
ipv4_enabled = "${var.enable_public_internet_access}"
private_network = "${var.private_network}"
require_ssl = "${var.require_ssl}"
}
location_preference {
follow_gae_application = "${var.follow_gae_application}"
@@ -230,7 +215,12 @@ resource "google_sql_database_instance" "read_replica" {
authorized_gae_applications = ["${var.authorized_gae_applications}"]
disk_autoresize = "${var.disk_autoresize}"
ip_configuration = ["${local.ip_configuration}"]
ip_configuration {
authorized_networks = ["${var.authorized_networks}"]
ipv4_enabled = "${var.enable_public_internet_access}"
private_network = "${var.private_network}"
require_ssl = "${var.require_ssl}"
}
location_preference {
follow_gae_application = "${var.follow_gae_application}"

View File

@@ -7,16 +7,21 @@ output "master_instance_name" {
value = "${google_sql_database_instance.master.name}"
}
output "master_public_ip_address" {
description = "The public IPv4 address of the master instance."
value = "${google_sql_database_instance.master.public_ip_address}"
}
output "master_private_ip_address" {
description = "The public IPv4 address of the master instance."
value = "${google_sql_database_instance.master.private_ip_address}"
}
output "master_ip_addresses" {
description = "All IP addresses of the master instance JSON encoded, see https://www.terraform.io/docs/providers/google/r/sql_database_instance.html#ip_address-0-ip_address"
value = "${jsonencode(google_sql_database_instance.master.ip_address)}"
}
output "master_first_ip_address" {
description = "The first IPv4 address of the addresses assigned to the master instance. If the instance has only public IP, it is the public IP address. If it has only private IP, it the private IP address. If it has both, it is the first item in the list and full IP address details are in 'instance_ip_addresses'"
value = "${google_sql_database_instance.master.first_ip_address}"
}
output "master_instance" {
description = "Self link to the master instance"
value = "${google_sql_database_instance.master.self_link}"
@@ -79,16 +84,21 @@ output "failover_instance_name" {
value = "${join("", google_sql_database_instance.failover_replica.*.name)}"
}
output "failover_public_ip_address" {
description = "The public IPv4 address of the failover instance."
value = "${join("", google_sql_database_instance.failover_replica.*.public_ip_address)}"
}
output "failover_private_ip_address" {
description = "The private IPv4 address of the failover instance."
value = "${join("", google_sql_database_instance.failover_replica.*.private_ip_address)}"
}
output "failover_ip_addresses" {
description = "All IP addresses of the failover instance JSON encoded, see https://www.terraform.io/docs/providers/google/r/sql_database_instance.html#ip_address-0-ip_address"
value = "${jsonencode(google_sql_database_instance.failover_replica.*.ip_address)}"
}
output "failover_first_ip_address" {
description = "The first IPv4 address of the addresses assigned to the failover instance. If the instance has only public IP, it is the public IP address. If it has only private IP, it the private IP address. If it has both, it is the first item in the list and full IP address details are in 'instance_ip_addresses'"
value = "${join("", google_sql_database_instance.failover_replica.*.first_ip_address)}"
}
output "failover_instance" {
description = "Self link to the failover instance"
value = "${join("", google_sql_database_instance.failover_replica.*.self_link)}"
@@ -142,9 +152,14 @@ output "read_replica_ip_addresses" {
value = "${jsonencode(google_sql_database_instance.read_replica.*.ip_address)}"
}
output "read_replica_first_ip_addresses" {
description = "List of first IPv4 addresses of the addresses assigned to the read replica instances. If the instance has only public IP, it is the public IP address. If it has only private IP, it the private IP address. If it has both, it is the first item in the list and full IP address details are in 'instance_ip_addresses'"
value = ["${google_sql_database_instance.read_replica.*.first_ip_address}"]
output "read_replica_public_ip_addresses" {
description = "List of public IPv4 addresses of the read replica instances."
value = ["${google_sql_database_instance.read_replica.*.public_ip_address}"]
}
output "read_replica_private_ip_addresses" {
description = "List of private IPv4 addresses of the read replica instances."
value = ["${google_sql_database_instance.read_replica.*.private_ip_address}"]
}
output "read_replica_instances" {