Google cloud platform Terraform GCP以错误的顺序执行资源

Google cloud platform Terraform GCP以错误的顺序执行资源,google-cloud-platform,terraform,terraform-provider-gcp,Google Cloud Platform,Terraform,Terraform Provider Gcp,我有一个main.tf文件: provider "google" { project = var.projNumber region = var.regName zone = var.zoneName } resource "google_storage_bucket" "bucket_for_python_application" { name = "python_bucket_exam" l

我有一个main.tf文件:

provider "google" {
  project = var.projNumber
  region = var.regName
  zone = var.zoneName
}

resource "google_storage_bucket" "bucket_for_python_application" {
  name = "python_bucket_exam"
  location = var.regName
  force_destroy = true
}

resource "google_storage_bucket_object" "file-hello-py" {
  name = "src/hello.py"
  source = "app-files/src/hello.py"
  bucket = "python_bucket_exam"
}

resource "google_storage_bucket_object" "file-main-py" {
  name = "main.py"
  source = "app-files/main.py"
  bucket = "python_bucket_exam"
}
当第一次执行时,它运行良好,但在
地形破坏
和再次
地形计划
->
地形应用
之后,我注意到地形在实际创建桶之前尝试创建对象:


Ofc它不能在不存在的东西里面创建对象。为什么会这样?

您必须在对象和bucket之间创建依赖关系(请参见下面的代码)。否则,Terraform将不知道必须先创建bucket,然后再创建对象。这与Terraform存储资源的方式有关

通过这样做,您可以声明一个隐式顺序:bucket,然后是objects。这相当于在
google\u storage\u bucket\u对象中使用
dependens\u
,但在这种情况下,我建议在对象中使用对bucket的引用,而不是使用显式的
dependens\u

resource "google_storage_bucket_object" "file-hello-py" {
  name   = "src/hello.py"
  source = "app-files/src/hello.py"
  bucket = google_storage_bucket.bucket_for_python_application.name
}

resource "google_storage_bucket_object" "file-main-py" {
  name   = "main.py"
  source = "app-files/main.py"
  bucket = google_storage_bucket.bucket_for_python_application.name
}