给出一对使用
部署的aws实例provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
count = 2
ami = "ami-2757f631"
instance_type = "t2.micro"
tags = {
Name = "Test${count.index}"
}
}
降低count = 1
会破坏最后部署的实例:
Terraform will perform the following actions:
- aws_instance.example[1]
是否有可能让terraform破坏 第一个实例 。即
Terraform will perform the following actions:
- aws_instance.example[0]
答案 0 :(得分:2)
Terraform正在跟踪哪个实例通过其状态。当您减少count
资源上的aws_instance
时,Terraform将只删除后面的实例。虽然这不应该是一个很大的问题,因为我只是真的建议您部署一组可以处理负载被中断的同质实例(如果你真的需要的话,它会处于某种形式的负载平衡器机制之后)可以编辑状态文件以在减少实例数量之前重新排序实例。
状态文件被序列化为JSON,因此您可以直接编辑它(确保它上传到您用于远程状态的任何内容,如果您使用的是远程状态)或者更好的是您可以使用第一类工具进行编辑Terraform CLI提供的远程状态terraform state mv
。
举个例子,你可以这样做:
# Example from question has been applied already
# `count` is edited from 2 to 1
$ terraform plan
...
aws_instance.example[1]: Refreshing state... (ID: i-0c227dfbfc72fb0cd)
aws_instance.example: Refreshing state... (ID: i-095fd3fdf86ce8254)
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
- aws_instance.example[1]
Plan: 0 to add, 0 to change, 1 to destroy.
...
$
$
$
$ terraform state list
aws_instance.example[0]
aws_instance.example[1]
$
$
$
$ terraform state mv aws_instance.example[1] aws_instance.example[2]
Moved aws_instance.example[1] to aws_instance.example[2]
$ terraform state mv aws_instance.example[0] aws_instance.example[1]
Moved aws_instance.example[0] to aws_instance.example[1]
$ terraform state mv aws_instance.example[2] aws_instance.example[0]
Moved aws_instance.example[2] to aws_instance.example[0]
$
$
$
$ terraform plan
...
aws_instance.example[1]: Refreshing state... (ID: i-095fd3fdf86ce8254)
aws_instance.example: Refreshing state... (ID: i-0c227dfbfc72fb0cd)
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
~ update in-place
- destroy
Terraform will perform the following actions:
~ aws_instance.example
tags.Name: "Test1" => "Test0"
- aws_instance.example[1]
Plan: 0 to add, 1 to change, 1 to destroy.
...