不,很遗憾,Terraform没有应用特定 .tf 文件的功能。Terraform应用同一目录中的所有 .tf 文件。
但是您可以应用带有注释和取消注释的特定代码。例如,您在同一目录中有 2 个 .tf 文件“1st.tf”和“2nd.tf”来为GCP(Google Cloud Platform)创建资源:
然后,“1st.tf”的代码如下:
provider "google" {
credentials = file("myCredentials.json")
project = "myproject-113738"
region = "asia-northeast1"
}
resource "google_project_service" "project" {
service = "iam.googleapis.com"
disable_dependent_services = true
}
而“2nd.tf”的代码如下:
resource "google_service_account" "service_account_1" {
display_name = "Service Account 1"
account_id = "service-account-1"
}
resource "google_service_account" "service_account_2" {
display_name = "Service Account 2"
account_id = "service-account-2"
}
现在,首先,您只想应用“1st.tf”中的代码,因此您需要注释掉“2nd.tf”中的代码:
1st.tf:
provider "google" {
credentials = file("myCredentials.json")
project = "myproject-113738"
region = "asia-northeast1"
}
resource "google_project_service" "project" {
service = "iam.googleapis.com"
disable_dependent_services = true
}
2nd.tf(注释掉):
# resource "google_service_account" "service_account_1" {
# display_name = "Service Account 1"
# account_id = "service-account-1"
# }
# resource "google_service_account" "service_account_2" {
# display_name = "Service Account 2"
# account_id = "service-account-2"
# }
然后,您申请:
terraform apply -auto-approve
接下来,另外,您要应用“2nd.tf”中的代码,因此您需要取消注释“2nd.tf”中的代码:
1st.tf:
provider "google" {
credentials = file("myCredentials.json")
project = "myproject-113738"
region = "asia-northeast1"
}
resource "google_project_service" "project" {
service = "iam.googleapis.com"
disable_dependent_services = true
}
2nd.tf(取消注释):
resource "google_service_account" "service_account_1" {
display_name = "Service Account 1"
account_id = "service-account-1"
}
resource "google_service_account" "service_account_2" {
display_name = "Service Account 2"
account_id = "service-account-2"
}
然后,您申请:
terraform apply -auto-approve
这样,您可以应用带有注释和取消注释的特定代码。