导入资源而不强制创建新资源
Import resources without forcing new ones being created
我正在尝试恢复配置了 Terraform 的 AWS 账户的状态。我的模块已经 运行 'terraform import' 并且能够进入状态 aws_subnets,但是当我 运行 'terraform plan' 它仍然尝试强制销毁现有子网并重新创建它们。
这是我的 terraform 的样子
resource "aws_subnet" "instance_subnets" {
for_each = { for idx, subnet in keys(var.subnets) : idx => { name = subnet
cidr = var.subnets[subnet] } }
cidr_block = each.value.cidr
vpc_id = aws_vpc.vpc.id
availability_zone = element(data.aws_availability_zones.available.names, each.key)
tags = {
Name = each.value.name
}
}
我的terraform.tf
variable "subnets" {
type = map(string)
default = {
"Public" : "10.10.0.0/24"
"Private" : "10.10.1.0/24"
}
terraform 计划输出
# module.mymodule.aws_subnet.subnets will be destroyed
- resource "aws_subnet" "subnets" {
- arn = "arn:aws:ec2:eu-west-2:0xxxxxxxxxxxxx:subnet/subnet-0000xxxxx" -> null
- assign_ipv6_address_on_creation = false -> null
- availability_zone = "eu-west-2a" -> null
- availability_zone_id = "euw2-az2" -> null
- cidr_block = "10.10.0.0/24" -> null
- id = "subnet-0000xxxxx" -> null
- map_customer_owned_ip_on_launch = false -> null
- map_public_ip_on_launch = false -> null
- owner_id = "0xxxxxxxxxxxxx" -> null
- tags = {
- "Name" = "Public"
} -> null
- tags_all = {
- "Name" = "Public"
} -> null
- vpc_id = "vpc-0000xxxxxxx" -> null
- timeouts {}
}
# module.mymodule.aws_subnet.subnets[1] will be destroyed
- resource "aws_subnet" "subnets" {
- arn = "arn:aws:ec2:eu-west-2:0xxxxxxxxxxxxx:subnet/subnet-0000xxxxx" -> null
- assign_ipv6_address_on_creation = false -> null
- availability_zone = "eu-west-2b" -> null
- availability_zone_id = "euw2-az3" -> null
- cidr_block = "10.10.1.0/24" -> null
- id = "subnet-0000xxxxx" -> null
- map_customer_owned_ip_on_launch = false -> null
- map_public_ip_on_launch = false -> null
- owner_id = "0xxxxxxxxxxxxx" -> null
- tags = {
- "Name" = "Private"
} -> null
- tags_all = {
- "Name" = "Private"
} -> null
- vpc_id = "vpc-0000xxxxxxx" -> null
- timeouts {}
}
# module.mymodule.aws_subnet.subnets["0"] will be created
+ resource "aws_subnet" "subnets" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "eu-west-2a"
+ availability_zone_id = (known after apply)
+ cidr_block = "10.10.0.0/24"
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ map_public_ip_on_launch = false
+ owner_id = (known after apply)
+ tags = {
+ "Name" = "Public"
}
+ tags_all = {
+ "Name" = "Public"
}
+ vpc_id = "vpc-0000xxxxxxx"
}
# module.mymodule.aws_subnet.subnets["1"] will be created
+ resource "aws_subnet" "subnets" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "eu-west-2b"
+ availability_zone_id = (known after apply)
+ cidr_block = "10.10.1.0/24"
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ map_public_ip_on_launch = false
+ owner_id = (known after apply)
+ tags = {
+ "Name" = "Private"
}
+ tags_all = {
+ "Name" = "Private"
}
+ vpc_id = "vpc-0000xxxxxxx"
}
我有无法在每个子网中终止的 ec2 实例,因此无法将其销毁并重新创建。是否可以以允许我按原样使用状态文件的方式导入它们?
for_each
使用的索引是子网长度变量的索引,在正常情况下for_each
你应该使用名称索引:
for_each = var.subnets
然后使用名称索引导入:
terraform import module.mymodule.aws_subnet.subnets["private"] SUBNET_ID
terraform import module.mymodule.aws_subnet.subnets["public"] SUBNET_ID
在此处阅读更多内容:
我正在尝试恢复配置了 Terraform 的 AWS 账户的状态。我的模块已经 运行 'terraform import' 并且能够进入状态 aws_subnets,但是当我 运行 'terraform plan' 它仍然尝试强制销毁现有子网并重新创建它们。
这是我的 terraform 的样子
resource "aws_subnet" "instance_subnets" {
for_each = { for idx, subnet in keys(var.subnets) : idx => { name = subnet
cidr = var.subnets[subnet] } }
cidr_block = each.value.cidr
vpc_id = aws_vpc.vpc.id
availability_zone = element(data.aws_availability_zones.available.names, each.key)
tags = {
Name = each.value.name
}
}
我的terraform.tf
variable "subnets" {
type = map(string)
default = {
"Public" : "10.10.0.0/24"
"Private" : "10.10.1.0/24"
}
terraform 计划输出
# module.mymodule.aws_subnet.subnets will be destroyed
- resource "aws_subnet" "subnets" {
- arn = "arn:aws:ec2:eu-west-2:0xxxxxxxxxxxxx:subnet/subnet-0000xxxxx" -> null
- assign_ipv6_address_on_creation = false -> null
- availability_zone = "eu-west-2a" -> null
- availability_zone_id = "euw2-az2" -> null
- cidr_block = "10.10.0.0/24" -> null
- id = "subnet-0000xxxxx" -> null
- map_customer_owned_ip_on_launch = false -> null
- map_public_ip_on_launch = false -> null
- owner_id = "0xxxxxxxxxxxxx" -> null
- tags = {
- "Name" = "Public"
} -> null
- tags_all = {
- "Name" = "Public"
} -> null
- vpc_id = "vpc-0000xxxxxxx" -> null
- timeouts {}
}
# module.mymodule.aws_subnet.subnets[1] will be destroyed
- resource "aws_subnet" "subnets" {
- arn = "arn:aws:ec2:eu-west-2:0xxxxxxxxxxxxx:subnet/subnet-0000xxxxx" -> null
- assign_ipv6_address_on_creation = false -> null
- availability_zone = "eu-west-2b" -> null
- availability_zone_id = "euw2-az3" -> null
- cidr_block = "10.10.1.0/24" -> null
- id = "subnet-0000xxxxx" -> null
- map_customer_owned_ip_on_launch = false -> null
- map_public_ip_on_launch = false -> null
- owner_id = "0xxxxxxxxxxxxx" -> null
- tags = {
- "Name" = "Private"
} -> null
- tags_all = {
- "Name" = "Private"
} -> null
- vpc_id = "vpc-0000xxxxxxx" -> null
- timeouts {}
}
# module.mymodule.aws_subnet.subnets["0"] will be created
+ resource "aws_subnet" "subnets" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "eu-west-2a"
+ availability_zone_id = (known after apply)
+ cidr_block = "10.10.0.0/24"
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ map_public_ip_on_launch = false
+ owner_id = (known after apply)
+ tags = {
+ "Name" = "Public"
}
+ tags_all = {
+ "Name" = "Public"
}
+ vpc_id = "vpc-0000xxxxxxx"
}
# module.mymodule.aws_subnet.subnets["1"] will be created
+ resource "aws_subnet" "subnets" {
+ arn = (known after apply)
+ assign_ipv6_address_on_creation = false
+ availability_zone = "eu-west-2b"
+ availability_zone_id = (known after apply)
+ cidr_block = "10.10.1.0/24"
+ id = (known after apply)
+ ipv6_cidr_block_association_id = (known after apply)
+ map_public_ip_on_launch = false
+ owner_id = (known after apply)
+ tags = {
+ "Name" = "Private"
}
+ tags_all = {
+ "Name" = "Private"
}
+ vpc_id = "vpc-0000xxxxxxx"
}
我有无法在每个子网中终止的 ec2 实例,因此无法将其销毁并重新创建。是否可以以允许我按原样使用状态文件的方式导入它们?
for_each
使用的索引是子网长度变量的索引,在正常情况下for_each
你应该使用名称索引:
for_each = var.subnets
然后使用名称索引导入:
terraform import module.mymodule.aws_subnet.subnets["private"] SUBNET_ID
terraform import module.mymodule.aws_subnet.subnets["public"] SUBNET_ID
在此处阅读更多内容: