Search code examples
terraformemulationgcloudgoogle-cloud-pubsubgoogle-cloud-pubsub-emulator

Can you use the Google Cloud emulator with Terraform?


When I setup the emulator and Terraform correctly, will I be able to run terraform with the results inside the emulator and not inside my project in Google Cloud?

I could not find an answer on the web and cannot start before I know.

Thanks in advance!


Solution

  • Yes you can! We use it to bootstrap the Google PubSub emulator by reusing our existing production environment configuration.

    The trick is that you need to override the API Endpoints in the provider configuration:

    terraform {
      required_providers {
        google = {
          source  = "hashicorp/google"
          version = "4.33.0"
        }
      }
    }
    
    provider "google" {
      project = "some-project-id"
      pubsub_custom_endpoint = "http://localhost:8085/v1/"
    }
    

    To apply this then, I start the emulator like this:

    $ gcloud beta emulators pubsub start --project=some-project-id
    

    Note:

    • The project-id is specified via the argument and must match the project-id you configure in the terraform provider
    • Port 8085 is the default port the emulator starts on

    Drawbacks

    Since you're overriding only specific endpoint, you must be careful which resources you create. For example, creating a google_service_account will sent that request to the actual Google endpoint.

    There are emulators for some Google service, but not all! Alternatively, you can override all endpoints in the Terraform Provider to non-existing local ports. This will then fail to apply the plan instead of silently creating resources on prem that you might not expect.