Add sandbox for experimentation

This commit is contained in:
Petri Autero
2019-02-01 12:09:36 +02:00
parent 941c7c10f9
commit dfb4eb8538
6 changed files with 211 additions and 3 deletions

View File

@@ -0,0 +1,78 @@
provider "google-beta" {
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"
}
variable "region" {
default = "europe-north1"
}
variable "project" {
default = "dev-sandbox-228703"
}
variable "zone" {
default = "europe-north1-a"
}
variable "mysql_version" {
default = "MYSQL_5_6"
}
resource "random_id" "name" {
byte_length = 2
}
resource "google_compute_network" "private_network" {
provider = "google-beta"
name = "private-network"
}
resource "google_compute_global_address" "private_ip_address" {
provider = "google-beta"
name = "private-ip-address"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "${google_compute_network.private_network.self_link}"
}
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}"]
}
module "mysql-db" {
source = "../../modules/cloud-sql"
name = "example-mysql-${random_id.name.hex}"
region = "${var.region}"
engine = "${var.mysql_version}"
project = "${var.project}"
machine_type = "db-f1-micro"
ip_configuration = [
{
ipv4_enabled = "true"
private_network = "${google_compute_network.private_network.self_link}"
}
]
# https://cloud.google.com/sql/docs/mysql/flags
flags = [
]
}
output "mysql_conn" {
value = "${var.project}:${var.region}:${module.mysql-db.instance_name}"
}

45
examples/sandbox/main.tf Normal file
View File

@@ -0,0 +1,45 @@
provider "google-beta" {
region = "${var.region}"
}
variable "region" {
default = "europe-north1"
}
variable "project" {
default = "petri-sandbox"
}
variable "endpoints" {
type = "list"
default = ["192.168.11.1", "192.168.11.2"]
}
data "template_file" "single_ip" {
count = "${length(var.endpoints)}"
template = <<YAML
{
name = "$${name}"
value = "$${val}"
}
YAML
vars {
name = "network-${count.index}"
val = "${var.endpoints[count.index]}"
}
}
output "demo" {
value = "${data.template_file.ip_configuration.rendered}"
}
data "template_file" "ip_configuration" {
template = <<YAML
authorized_endpoints = [
$${cidrs}
]
YAML
vars {
cidrs = "${join(",", var.endpoints)}"
}
}

61
modules/cloud-sql/main.tf Normal file
View File

@@ -0,0 +1,61 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# DEPLOY A CLOUD SQL CLUSTER
# This module deploys an Cloud SQL cluster. The cluster is managed by Google and automatically handles leader
# election, replication, failover, backups, patching, and encryption.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ------------------------------------------------------------------------------
# CREATE THE CLOUD SQL CLUSTER
#
# NOTE: We have multiple google_sql_database_instance resources, based on
# HA, encryption and replication configuration options.
# ------------------------------------------------------------------------------
resource "google_sql_database_instance" "default" {
name = "${var.name}"
project = "${var.project}"
region = "${var.region}"
database_version = "${var.engine}"
master_instance_name = "${var.master_instance_name}"
settings {
tier = "${var.machine_type}"
activation_policy = "${var.activation_policy}"
authorized_gae_applications = ["${var.authorized_gae_applications}"]
disk_autoresize = "${var.disk_autoresize}"
backup_configuration = ["${var.backup_configuration}"]
ip_configuration = ["${var.ip_configuration}"]
location_preference = ["${var.location_preference}"]
maintenance_window = ["${var.maintenance_window}"]
disk_size = "${var.disk_size}"
disk_type = "${var.disk_type}"
pricing_plan = "${var.pricing_plan}"
replication_type = "${var.replication_type}"
database_flags = ["${var.flags}"]
availability_type = "${var.availability_type}"
}
replica_configuration = ["${var.replica_configuration}"]
}
# ------------------------------------------------------------------------------
# CREATE A DATABASE
# ------------------------------------------------------------------------------
resource "google_sql_database" "default" {
count = "${var.master_instance_name == "" ? 1 : 0}"
name = "${var.db_name}"
project = "${var.project}"
instance = "${google_sql_database_instance.default.name}"
charset = "${var.db_charset}"
collation = "${var.db_collation}"
}
resource "google_sql_user" "default" {
count = "${var.master_instance_name == "" ? 1 : 0}"
name = "${var.db_name}"
project = "${var.project}"
instance = "${google_sql_database_instance.default.name}"
host = "${var.db_user_host}"
password = "${var.db_password}"
}

View File

@@ -0,0 +1,19 @@
output instance_name {
description = "The name of the database instance"
value = "${google_sql_database_instance.default.name}"
}
output instance_address {
description = "The IPv4 address of the master database instance"
value = "${google_sql_database_instance.default.ip_address.0.ip_address}"
}
output instance_address_time_to_retire {
description = "The time the master instance IP address will be reitred. RFC 3339 format."
value = "${google_sql_database_instance.default.ip_address.0.time_to_retire}"
}
output self_link {
description = "Self link to the master instance"
value = "${google_sql_database_instance.default.self_link}"
}

View File

@@ -29,6 +29,11 @@ variable "machine_type" {
default = "db-f1-micro"
}
variable "availability_type" {
description = "This specifies whether a PostgreSQL instance should be set up for high availability (REGIONAL) or single zone (ZONAL)."
default = "ZONAL"
}
variable "db_name" {
description = "Name of the default database to create"
default = "default"
@@ -44,17 +49,17 @@ variable "db_collation" {
default = ""
}
variable "master_user_name" {
variable "db_user" {
description = "The name of the default user"
default = "default"
}
variable "master_user_host" {
variable "db_user_host" {
description = "The host for the default user"
default = "%"
}
variable "master_user_password" {
variable "db_password" {
description = "The password for the default user."
default = ""
}