2016-12-06 30 views
0

我有一個terraform資源,看起來像下面如何刪除terraform覆蓋資源中的屬性。

resource "aws_instance" "web" { 
    ami = "ami-408c7f28" 
    tags = { Name = "hello World"} 
} 

我要重寫它來刪除標記,並讓它看起來像這樣

resource "aws_instance" "web" { 
    ami = "ami-408c7f28" 
} 

基本上去除標籤。

有沒有辦法在覆蓋文件中做到這一點,如此處所述? https://www.terraform.io/docs/providers/aws/r/instance.html

以上是一個例子。一般來說,我真的想知道我是否可以在覆蓋中移除一個屬性。

回答

1

是的,Terraform應該能夠從資源中刪除屬性。例如,假設我已經用下面的.tf文件運行terraform apply

resource "aws_instance" "web" { 
    ami = "ami-408c7f28" 
    instance_type = "m1.small" 
    tags = { Name = "hello World"} 
} 

現在,如果我改變.tf文件:

resource "aws_instance" "web" { 
    ami = "ami-408c7f28" 
    instance_type = "m1.small" 
} 

和運行terraform plan,我應該看到這樣的輸出這個:

~ aws_instance.web 
    tags.%: "1" => "0" 
    tags.Name: "hello World" => "" 

這表明terraform希望通過刪除Name標籤來修改實例。如果我運行terraform apply,標籤將被刪除。

如果你想刪除的標籤在override file(override.tf,例如),你會明確設置標記爲空地圖:

resource "aws_instance" "web" { 
    ami = "ami-408c7f28" 
    instance_type = "m1.small" 
    tags = {} 
} 

注意這些具體的例子只有當你的工作在我們東1帳戶仍支持EC2-Classic

+0

當我第一次看到你的問題時,我錯過了你正在詢問有關使用[覆蓋文件](https://www.terraform.io/docs/configuration/override.html),特別是(我認爲你粘貼了錯誤的鏈接)。您仍然可以使用覆蓋來做到這一點,但您需要明確清除tags屬性。例如,在override.tf中,您可以指定'tags = {}'來獲得相同的效果。 –

+0

我想我需要更精確地使用我的用例。這在技術上回答了我的問題,但並未解決我的問題。榮譽。 –