Search code examples
google-cloud-platformdeploymentterraformterraform-provider-gcp

Terraform deploy different resource for enviroment


I'm new on Terraform so I'm sure it is a easy question.

I'm trying to deploy into GCP using terraform.

I have 2 different enviroments both on same GCP project:

  • nonlive
  • live

I have alerts for each enviroment so that is what I intend to create:

If I deploy into an enviroment then Terraform must create/update resources for this enviromet but don't update resources for rest of enviroments.

I'm trying to user modules and conditions, it's similar to this:

module "enviroment_live" {
  source = "./live"
  module_create = (var.environment=="live")
}

resource "google_monitoring_alert_policy" "alert_policy_live" {
  count = var.module_create  ? 1 : 0
  display_name = "Alert CPU LPProxy Live"

Problem:

When I deploy on live enviroment Terraform delete alerts for nonlive enviroment and vice versa.

Is it possible to update resources of one enviroment without deleting those of the other?

Regards


Solution

  • As Marko E suggested solution was to use workspaces:

    Terraform workspaces

    The steps must be:

    1. Create a workspace for each enviroment.

    2. On deploy (CI/CD) select workspace befor plan/apply:

      terraform workspace select $ENVIROMENT

    3. Use conditions (as I explained before) to create/configure the resource.