EC2实例的Terraform导入总是在临时设备和ipv6上替换
我正在试图弄清楚如何将EC2实例导入Terraform。每当我导入实例时,如果我运行“terraform plan”,它总是希望替换实例 我尝试了一个只有AMI id和实例大小的极小的导入,并且尝试了一个完整的导入,其中指定了所有可能的值。两者似乎都不起作用,结果相同 初始状态:EC2实例的Terraform导入总是在临时设备和ipv6上替换,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,我正在试图弄清楚如何将EC2实例导入Terraform。每当我导入实例时,如果我运行“terraform plan”,它总是希望替换实例 我尝试了一个只有AMI id和实例大小的极小的导入,并且尝试了一个完整的导入,其中指定了所有可能的值。两者似乎都不起作用,结果相同 初始状态: [ec2-user@ip-172-31-34-85 terraform]$ ls provider.aws.tf test1.tf [ec2-user@ip-172-31-34-85 terraform]$ cat
[ec2-user@ip-172-31-34-85 terraform]$ ls
provider.aws.tf test1.tf
[ec2-user@ip-172-31-34-85 terraform]$ cat provider.aws.tf
provider "aws" {
region = "eu-west-1"
}
[ec2-user@ip-172-31-34-85 terraform]$ cat test1.tf
resource "aws_instance" "web" {
ami = "ami-0bbc25e23a7640b9b"
instance_type = "t3.medium"
}
计划:
适用于:
----snip----
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_instance.web: Creating...
aws_instance.web: Still creating... [10s elapsed]
aws_instance.web: Creation complete after 11s [id=i-0875a3e74f53ed82c]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
创建后,让我们检查计划:
[ec2-user@ip-172-31-34-85 terraform]$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_instance.web: Refreshing state... [id=i-0875a3e74f53ed82c]
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
现在,我们删除状态文件并导入资源:
[ec2-user@ip-172-31-34-85 terraform]$ terraform import aws_instance.web i-0875a3e74f53ed82c
aws_instance.web: Importing from ID "i-0875a3e74f53ed82c"...
aws_instance.web: Import complete!
Imported aws_instance
aws_instance.web: Refreshing state... [id=i-0875a3e74f53ed82c]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
另一项计划:
[ec2-user@ip-172-31-34-85 terraform]$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_instance.web: Refreshing state... [id=i-0875a3e74f53ed82c]
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# aws_instance.web must be replaced
-/+ resource "aws_instance" "web" {
ami = "ami-0bbc25e23a7640b9b"
~ arn = "arn:aws:ec2:eu-west-1:341009493741:instance/i-0875a3e74f53ed82c" -> (known after apply)
~ associate_public_ip_address = true -> (known after apply)
~ availability_zone = "eu-west-1b" -> (known after apply)
~ cpu_core_count = 1 -> (known after apply)
~ cpu_threads_per_core = 2 -> (known after apply)
- disable_api_termination = false -> null
- ebs_optimized = false -> null
get_password_data = false
+ host_id = (known after apply)
~ id = "i-0875a3e74f53ed82c" -> (known after apply)
~ instance_state = "running" -> (known after apply)
instance_type = "t3.medium"
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply) # forces replacement
+ key_name = (known after apply)
- monitoring = false -> null
~ network_interface_id = "eni-077ae2820afc460a7" -> (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
~ primary_network_interface_id = "eni-077ae2820afc460a7" -> (known after apply)
~ private_dns = "ip-172-31-2-15.eu-west-1.compute.internal" -> (known after apply)
~ private_ip = "172.31.2.15" -> (known after apply)
~ public_dns = "ec2-34-255-30-73.eu-west-1.compute.amazonaws.com" -> (known after apply)
~ public_ip = "34.255.30.73" -> (known after apply)
~ security_groups = [
- "default",
] -> (known after apply)
source_dest_check = true
~ subnet_id = "subnet-25d0f941" -> (known after apply)
- tags = {
- "Backup" = "True"
} -> null
~ tenancy = "default" -> (known after apply)
+ volume_tags = (known after apply)
~ vpc_security_group_ids = [
- "sg-12b9c774",
] -> (known after apply)
- credit_specification {
- cpu_credits = "unlimited" -> null
}
+ ebs_block_device {
+ delete_on_termination = (known after apply)
+ device_name = (known after apply)
+ encrypted = (known after apply)
+ iops = (known after apply)
+ snapshot_id = (known after apply)
+ volume_id = (known after apply)
+ volume_size = (known after apply)
+ volume_type = (known after apply)
}
+ ephemeral_block_device { # forces replacement
+ device_name = (known after apply)
+ no_device = (known after apply)
+ virtual_name = (known after apply)
}
+ network_interface {
+ delete_on_termination = (known after apply)
+ device_index = (known after apply)
+ network_interface_id = (known after apply)
}
~ root_block_device {
~ delete_on_termination = true -> (known after apply)
~ iops = 100 -> (known after apply)
~ volume_id = "vol-0c46eb9d028b04d9a" -> (known after apply)
~ volume_size = 8 -> (known after apply)
~ volume_type = "gp2" -> (known after apply)
}
timeouts {}
}
Plan: 1 to add, 0 to change, 1 to destroy.
它看起来像是提供程序中的一个bug。您应该在问题追踪器上提出此问题:已提交错误报告。我正在使用Terraform 0.11.14,并进行了相同的测试。但我的情况没有问题。您使用的是什么版本?这是在0.12.3Came上,有相同的问题。对我来说,通过将
security\u组
替换为vpc\u security\u组ID
[ec2-user@ip-172-31-34-85 terraform]$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_instance.web: Refreshing state... [id=i-0875a3e74f53ed82c]
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# aws_instance.web must be replaced
-/+ resource "aws_instance" "web" {
ami = "ami-0bbc25e23a7640b9b"
~ arn = "arn:aws:ec2:eu-west-1:341009493741:instance/i-0875a3e74f53ed82c" -> (known after apply)
~ associate_public_ip_address = true -> (known after apply)
~ availability_zone = "eu-west-1b" -> (known after apply)
~ cpu_core_count = 1 -> (known after apply)
~ cpu_threads_per_core = 2 -> (known after apply)
- disable_api_termination = false -> null
- ebs_optimized = false -> null
get_password_data = false
+ host_id = (known after apply)
~ id = "i-0875a3e74f53ed82c" -> (known after apply)
~ instance_state = "running" -> (known after apply)
instance_type = "t3.medium"
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply) # forces replacement
+ key_name = (known after apply)
- monitoring = false -> null
~ network_interface_id = "eni-077ae2820afc460a7" -> (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
~ primary_network_interface_id = "eni-077ae2820afc460a7" -> (known after apply)
~ private_dns = "ip-172-31-2-15.eu-west-1.compute.internal" -> (known after apply)
~ private_ip = "172.31.2.15" -> (known after apply)
~ public_dns = "ec2-34-255-30-73.eu-west-1.compute.amazonaws.com" -> (known after apply)
~ public_ip = "34.255.30.73" -> (known after apply)
~ security_groups = [
- "default",
] -> (known after apply)
source_dest_check = true
~ subnet_id = "subnet-25d0f941" -> (known after apply)
- tags = {
- "Backup" = "True"
} -> null
~ tenancy = "default" -> (known after apply)
+ volume_tags = (known after apply)
~ vpc_security_group_ids = [
- "sg-12b9c774",
] -> (known after apply)
- credit_specification {
- cpu_credits = "unlimited" -> null
}
+ ebs_block_device {
+ delete_on_termination = (known after apply)
+ device_name = (known after apply)
+ encrypted = (known after apply)
+ iops = (known after apply)
+ snapshot_id = (known after apply)
+ volume_id = (known after apply)
+ volume_size = (known after apply)
+ volume_type = (known after apply)
}
+ ephemeral_block_device { # forces replacement
+ device_name = (known after apply)
+ no_device = (known after apply)
+ virtual_name = (known after apply)
}
+ network_interface {
+ delete_on_termination = (known after apply)
+ device_index = (known after apply)
+ network_interface_id = (known after apply)
}
~ root_block_device {
~ delete_on_termination = true -> (known after apply)
~ iops = 100 -> (known after apply)
~ volume_id = "vol-0c46eb9d028b04d9a" -> (known after apply)
~ volume_size = 8 -> (known after apply)
~ volume_type = "gp2" -> (known after apply)
}
timeouts {}
}
Plan: 1 to add, 0 to change, 1 to destroy.