代码之家  ›  专栏  ›  技术社区  ›  Yuri Astrakhan

导出google云配置

  •  0
  • Yuri Astrakhan  · 技术社区  · 6 年前

    是否有一种方法来为对象导出谷歌云配置,例如对于负载均衡器,与通过API来设置它的方式相同吗?

    我可以很快地在控制台网站上配置我需要的东西,但是我花了很多时间试图用Terraform来复制。如果我能从我已经配置好的系统中生成terraform文件,或者至少生成google api输出,那就太好了。

    1 回复  |  直到 6 年前
        1
  •  2
  •   ydaetskcoR    6 年前

    如果您已经在terraform之外创建了一些东西,并且希望terraform管理它,或者希望找出如何使用terraform对其进行最佳配置 Terraform's import command 任何支持它的资源。

    所以如果你创造了一个 forwarding rule 打电话 terraform-test 通过google云控制台,想知道它是如何映射到terraform的 google_compute_forwarding_rule 资源,然后您可以运行 terraform import google_compute_forwarding_rule.default terraform-test 将其导入terraform的状态文件。

    如果你运行一个计划,terraform会告诉你它有 google_compute_forwarding_rule.default 处于其状态,但代码中未定义资源,因此它将要删除该资源。

    如果添加使计划生效所需的最小配置:

    resource "google_compute_forwarding_rule" "default" {
      name = "terraform-test"
    }
    

    然后再次运行计划terraform将告诉您需要更改什么,以使导入的转发规则看起来像您定义的配置。假设你已经做了一些事情,比如在负载均衡器上设置描述,Terraform的计划会显示出这样的情况:

    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
      ~ update in-place
    -/+ destroy and then create replacement
    
    Terraform will perform the following actions:
    
      ~ google_compute_forwarding_rule.default
          description:                              "This is the description I added via the console" => ""
    
    Plan: 5 to add, 5 to change, 3 to destroy.
    

    这告诉您terraform想要删除转发规则的描述,使其与配置匹配。

    如果随后将资源定义更新为:

    resource "google_compute_forwarding_rule" "default" {
      name        = "terraform-test"
      description = "This is the description I added via the console"
    }
    

    TerraForm的计划将显示一个空的变更集:

    No changes. Infrastructure is up-to-date.
    
    This means that Terraform did not detect any differences between your
    configuration and real physical resources that exist. As a result, no
    actions need to be performed.
    

    此时,您已经将terraform代码与google云中资源的实际情况对齐,并且应该能够轻松地看到需要在terraform端设置什么,以便在google云中控制台中按预期发生事情。