terraform:如何在降低aws_实例数时销毁最旧的实例
给定一对使用部署的aws实例terraform:如何在降低aws_实例数时销毁最旧的实例,terraform,Terraform,给定一对使用部署的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将销毁最后部署的实
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]
Terraform通过其状态跟踪哪个实例是哪个实例。当您减少
aws\u实例上的count
时,资源地形将简单地删除后面的实例。虽然这不是什么大问题,因为我只建议您部署一组同质实例,这些实例可以处理被中断的负载(并且将位于某种形式的负载平衡器机制之后)如果确实需要,可以在减少实例数量之前编辑状态文件以重新排序实例
状态文件被序列化为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.
...
Terraform通过其状态跟踪哪个实例是哪个实例。当您减少aws\u实例上的count
时,资源地形将简单地删除后面的实例。虽然这不是什么大问题,因为我只建议您部署一组同质实例,这些实例可以处理被中断的负载(并且将位于某种形式的负载平衡器机制之后)如果确实需要,可以在减少实例数量之前编辑状态文件以重新排序实例
状态文件被序列化为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.
...
出于好奇,为什么要这样做?我希望能够扩展实例,而ELB不适合我们的设置。理想情况下,当我缩小规模时,我希望取出最旧的实例。出于好奇,为什么要这样做?我希望能够缩放实例,而ELB不适合我们的设置。理想情况下,当我缩小规模时,我希望取出最旧的实例。在没有任何选项来控制从列表中删除哪个实例的情况下,这可能是最好的选择。很遗憾,没有一些生命周期选项可以指定fifo或lifo。Terraform真的不在乎。我强烈建议您考虑一下您的用例,如果您真的无法通过使用自动缩放组等方式更好地处理此问题。在没有任何选项来控制从列表中删除哪个实例的情况下,这可能是最好的选择。很遗憾,没有一些生命周期选项可以指定fifo或lifo。Terraform真的不在乎。我强烈建议您考虑一下您的用例,如果您真的无法通过使用诸如自动缩放组之类的方法更好地处理这个问题,那么请考虑一下。