【问题标题】:How to give a .tf file as input in Terraform Apply command?如何在 Terraform Apply 命令中提供 .tf 文件作为输入?
【发布时间】:2018-05-22 08:15:32
【问题描述】:

我是 Terraform 的初学者。

我有一个包含 2 个 .tf 文件的目录。

现在我想在选定的 .tf 文件上运行 Terraform Apply 而忽略另一个。

我可以这样做吗?如果是,如何?如果不是,为什么以及最佳做法是什么?

【问题讨论】:

  • 你能详细说明你为什么要这样做吗?

标签: terraform devops open-source infrastructure-as-code hashicorp


【解决方案1】:

您不能有选择地应用一个文件,然后再应用另一个文件。 (也许)实现您的目标的两种方法:

  • 使用-target 标志来定位一个文件中的资源,然后是另一个。
  • 将每个文件(或更广泛地说,一组资源,可能是多个文件)放在单独的“模块”(文件夹)中。然后你可以分别apply他们。

【讨论】:

  • 谢谢,采用第二种方法。
【解决方案2】:

您可以使用terraform -target 标志。要么 您可以在单独的目录中拥有多个 terraform 模块。然后你可以在那里terraform apply。 例如,假设您分别有 3 个 .tf 文件。但是你需要同时运行不止一个。如果您也需要更频繁地运行它们,最好有一个 terraform 模块。

terraform
    |--frontend
    |  └──main.tf
    |--backend-1
    |  └──main.tf
    |--backend-2
    |  └──main.tf
    |--modules-1
    |  └──module.tf

module.tf 中,您可以定义需要应用的文件。

module "frontend" {
  source = "terraform/frontend"
}

module "backend-1" {
  source = "terraform/backend-1"
} 

然后在模块目录下发出terraform apply。它会自动在这些路径中导入实例并应用它。

【讨论】:

    【解决方案3】:

    将每个 terraform 配置文件放入单独的目录中可以正确完成工作。 所以这里是我的结构

    ├── aws
    │   └── aws_terraform.tf
    ├── trash
    │   └── main.tf
    

    所有你需要做的:

    1. 进入每个文件夹
    2. terraform init && terraform plan && terraform apply
    3. 输入“是”以确认 terraform 应用

    PS:“-target”键对我没有帮助。

    【讨论】:

      【解决方案4】:

      使用--target 选项来指定要通过以下命令运行的模块

      terraform apply -target=module.<module_name>
      

      或者另一种解决方法是重命名其他带有*.tf.disable 扩展名的 terraform 文件,以通过 Terraform 加载来跳过它。目前是Code加载*.tf文件

      【讨论】:

        【解决方案5】:

        不,很遗憾,Terraform 没有应用选定的 .tf 文件的功能。 Terraform 应用同一目录中的所有 .tf 文件

        但您可以通过注释掉取消注释应用所选代码。例如,您在同一目录中有 2 个 .tf 文件 "1st.tf""2nd.tf" 来为 GCP(Google Cloud平台)

        那么,"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
        

        这样,您可以通过注释掉取消注释应用所选代码

        【讨论】:

          【解决方案6】:

          如果您不能像其他答案所述的那样在不同的文件夹中拥有 terraform 文件。你可以试试我的脚本GitHub repo for script

          这是一个在特定 terraform 文件中运行并输出的脚本将“-target=”添加到所有模块名称。

          【讨论】:

            【解决方案7】:
            terraform apply -target nginx-docker.tf
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-12-18
              • 2019-06-03
              • 1970-01-01
              • 2020-05-18
              • 1970-01-01
              • 1970-01-01
              • 2021-07-17
              • 1970-01-01
              相关资源
              最近更新 更多