github.com/thiagoyeds/go-cloud@v0.26.0/postgres/gcppostgres/main.tf (about)

     1  # Copyright 2018 The Go Cloud Development Kit Authors
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     https://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  # Harness for Cloud SQL Postgres tests.
    16  
    17  terraform {
    18    required_version = "~>0.12"
    19  }
    20  
    21  provider "google" {
    22    version = "~> 2.5"
    23    project = var.project
    24  }
    25  
    26  provider "random" {
    27    version = "~> 2.1"
    28  }
    29  
    30  variable "project" {
    31    type        = string
    32    description = "Project to set up."
    33  }
    34  
    35  variable "region" {
    36    default     = "us-central1"
    37    description = "GCP region to create database and storage in, for example 'us-central1'. See https://cloud.google.com/compute/docs/regions-zones/ for valid values."
    38  }
    39  
    40  locals {
    41    sql_instance = "go-cloud-test-${random_id.sql_instance.hex}"
    42  }
    43  
    44  resource "google_project_service" "sql" {
    45    service            = "sql-component.googleapis.com"
    46    disable_on_destroy = false
    47  }
    48  
    49  resource "google_project_service" "sqladmin" {
    50    service            = "sqladmin.googleapis.com"
    51    disable_on_destroy = false
    52  }
    53  
    54  resource "random_id" "sql_instance" {
    55    keepers = {
    56      project = var.project
    57      region  = var.region
    58    }
    59  
    60    byte_length = 12
    61  }
    62  
    63  resource "google_sql_database_instance" "main" {
    64    name             = local.sql_instance
    65    database_version = "POSTGRES_9_6"
    66    region           = var.region
    67    project          = var.project
    68  
    69    settings {
    70      tier      = "db-f1-micro"
    71      disk_size = 10 # GiB
    72    }
    73  
    74    depends_on = [
    75      google_project_service.sql,
    76      google_project_service.sqladmin,
    77    ]
    78  }
    79  
    80  resource "google_sql_database" "main" {
    81    name     = "testdb"
    82    instance = google_sql_database_instance.main.name
    83  }
    84  
    85  resource "random_string" "db_password" {
    86    keepers = {
    87      project = var.project
    88      db_name = local.sql_instance
    89      region  = var.region
    90    }
    91  
    92    special = false
    93    length  = 20
    94  }
    95  
    96  resource "google_sql_user" "root" {
    97    name     = "root"
    98    instance = google_sql_database_instance.main.name
    99    password = random_string.db_password.result
   100  }
   101  
   102  output "project" {
   103    value       = var.project
   104    description = "The GCP project ID."
   105  }
   106  
   107  output "region" {
   108    value       = var.region
   109    description = "The Cloud SQL instance region."
   110  }
   111  
   112  output "instance" {
   113    value       = local.sql_instance
   114    description = "The Cloud SQL instance region."
   115  }
   116  
   117  output "username" {
   118    value       = "root"
   119    description = "The Cloud SQL username to connect with."
   120  }
   121  
   122  output "password" {
   123    value       = random_string.db_password.result
   124    sensitive   = true
   125    description = "The Cloud SQL instance password for the user."
   126  }
   127  
   128  output "database" {
   129    value       = "testdb"
   130    description = "The name of the database inside the Cloud SQL instance."
   131  }
   132