Terraform code to create a Cloud SQL PostgreSQL instance in GCP:
Create a Cloud SQL PostgreSQL instance in GCP, using terraform
In this example, the `provider` block specifies the GCP project and region to use. The resource `google_sql_database_instance` represents the Cloud SQL instance for the PostgreSQL database with its name, region, IP configuration, database flags, and tier. The `depends_on` parameter ensures that Google SQL Admin API has been enabled for your Google Cloud project. The `resource` block for `google_sql_database` represents the actual PostgreSQL database that will be created inside the instance.
Once you have written this code, you can run `terraform init`, `terraform plan` and `terraform apply` commands to initialize the Terraform working directory, generate an execution plan, and apply the changes to create the Cloud SQL instance and PostgreSQL database on GCP.
provider "google" {
project = "your-project-id"
region = "us-central1"
}
resource "google_sql_database_instance" "postgres_instance" {
name = "my-postgres-instance"
database_version = "POSTGRES_13"
depends_on = [
google_project_service.sqladmin
]
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = true
}
database_flags {
name = "log_statement"
value = "ALL"
}
}
}
resource "google_sql_database" "postgres_db" {
name = "my-postgres-database"
instance = google_sql_database_instance.postgres_instance.name
}