是否有一种方法可以导出对象(例如负载均衡器)的Google Cloud配置,就像通过API进行设置一样?
我可以在控制台站点中快速配置所需的内容,但是我花了大量的时间尝试使用Terraform复制这些内容。如果我可以从已经配置的系统中生成Terraform文件或至少Google API输出,那就太好了。
答案 0 :(得分:1)
如果您已经在Terraform之外创建了某些东西,并希望让Terraform对其进行管理,或者想知道如何使用Terraform对其进行最佳配置,则可以将Terraform's import
command用于支持它的任何资源。
因此,如果您通过Google Cloud控制台创建了名为<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Test"
DataContext="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}}"
Command="{Binding PlacementTarget.Tag.DataContext.YourCommand}"
CommandParameter="{Binding PlacementTarget.Tag.SelectedItems}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ItemContainerStyle>
的{{3}},并且想知道它如何映射到Terraform的forwarding rule资源,则可以运行terraform-test
将其导入Terraform的状态文件。
如果您随后运行了一个计划,Terraform会告诉您其状态为terraform import google_compute_forwarding_rule.default terraform-test
,但是资源未在代码中定义,因此它将希望删除它。
如果您添加了使计划生效所需的最小配置:
google_compute_forwarding_rule.default
然后再次运行该计划,然后Terraform会告诉您需要进行哪些更改以使导入的转发规则看起来像您定义的配置。假设您已经完成了类似在负载平衡器Terraform的计划上设置说明的操作,则将显示以下内容:
resource "google_compute_forwarding_rule" "default" {
name = "terraform-test"
}
这告诉您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代码与Google Cloud中资源的实际情况保持一致,并且应该能够轻松查看需要在Terraform方面进行设置以使事情在Google Cloud控制台中按预期进行。