1
votes

I am new to Terraform, is there any straight forward way to manage and create Google Cloud Composer environment using Terraform?

I checked the supported list of components for GCP seems like Google Cloud Composer is not there as of now. As a work around I am thinking of creating a shell script including required gcloud composer cli commands and run it using Terraform, is it a right approach? Please suggest alternatives.

3

3 Answers

3
votes

Google Cloud Composer is now supported in Terraform
https://www.terraform.io/docs/providers/google/r/composer_environment.html

It can be used as below

resource "google_composer_environment" "test" {
  name   = "my-composer-env"
  region = "us-central1"
}
2
votes

That is an option. You can use a null_resource and local-exec to run commands:

resource "null_resource" "composer" {
  provisioner "local-exec" {
    inline = [
      "gcloud beta composer <etc..>"
    ]
  }
}

Just keep in mind when using local-exec:

Note that even though the resource will be fully created when the provisioner is run, there is no guarantee that it will be in an operable state

It looks like Google Cloud Composer is really new and still in beta. Hopefully Terraform will support it in the future.

1
votes

I found that I had to use a slightly different syntax with the provisioner that included a command parameter.

resource "null_resource" "composer" {
  provisioner "local-exec" {
    command = "gcloud composer environments create <name> --project <project> --location us-central1 --zone us-central1-a --machine-type n1-standard-8"
  }
}

While this works, it is disconnected from the actual resource state in GCP. It'll rely on the state file to say whether it exists, and I found I had to taint it to get the command to run again.