Open hobbyhorse opened 5 years ago
This is on version 0.12.3. Everything works as expected on version 0.11.14.
Seeing the same on 0.12.6. This is something that also happened long ago with block_device.
I see this on instances that were imported. I haven't tried with a new instance.
Items I have not set in my aws_instance resource, but must include in ignore_changes to prevent recreation: ipv6_addresses, ebs_optimized, security_groups, ephemeral_block_device,
Update: only on provider.aws 2.11. On provider.aws 2.22, only ebs_optimized still causes an issue.
+1
Seeing the same on
Terraform v0.12.24
+ provider.aws v2.58.0
MacBook Pro (13-inch, 2017, Two Thunderbolt 3 ports)
macos Mojave
resource "aws_instance" "test1" {
ami = "ami-0ea0e838b9153f3a9"
instance_type = "t3.micro"
security_groups = ["sg-0846a52c36953458e"]
subnet_id = "subnet-0068193f3002512c7"
}
penzaideMacBook-Pro:aws_test penzai$ terraform import aws_instance.test1 i-0ec30eb274f4f98be
aws_instance.test1: Importing from ID "i-0ec30eb274f4f98be"...
aws_instance.test1: Import prepared!
Prepared aws_instance for import
aws_instance.test1: Refreshing state... [id=i-0ec30eb274f4f98be]
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.
penzaideMacBook-Pro:aws_test penzai$ 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.test1: Refreshing state... [id=i-0ec30eb274f4f98be]
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
-/+ destroy and then create replacement
Terraform will perform the following actions:
# aws_instance.test1 must be replaced
I just test on CentOS Linux release 7.5.1804 (Core)
.Recreate aws_instance which same with macos
Is this solved? I am having exactly the same issue now, with terraform 0.12.26
It seemed to be solved for me later on.
If it is still happening to you after deleting the cached provider files (where they are stored I am not always 100% sure, but https://www.terraform.io/docs/commands/cli-config.html#:~:text=exclusions%20out%20yourself.)-,Provider%20Plugin%20Cache,be%20downloaded%20for%20each%20configuration. ) then come back and post the TF and provider versions you are having the problem with.
It's happening for me for aws resources such as S3 bucket etc. I posted my question on SO: Terraform (0.12.29) import not working as expected; import succeeded but plan shows destroy & recreate
My Env:
Local machine: macOS v10.14.6
Terraform v0.12.29
+ provider.aws v3.14.1
+ provider.null v2.1.2
+ provider.random v2.3.1
+ provider.template v2.1.2
Same behavior when creating a new instance
Workaround is to ignore the changes that are forcing the replacement. Terraform v0.12.24 points out the changes that cause the EC2 instance to be replaced. In my case it was the security_groups
attributes that was forcing the replacement.
Following is the code snippet that I had to add
lifecycle {
ignore_changes = [security_groups]
}
Similar solutions have been proposed earlier
Environment
Terraform v0.12.24
+ provider.aws v2.70.0
+ provider.local v1.4.0
+ provider.null v2.1.2
+ provider.template v2.2.0
+ provider.tls v2.2.0
I found that by replacing security_groups
with vpc_security_group_ids
it doesn't cycle.
From the Terraform documentation:
security_groups
- (Optional, EC2-Classic and default VPC only) A list of security group names (EC2-Classic) or IDs (default VPC) to associate with.NOTE: If you are creating Instances in a VPC, use
vpc_security_group_ids
instead.
Therefore, if your EC2 instance is not a classic EC2 instance and not in the default VPC, use vpc_security_group_ids
and the Terraform plan will not force a replacement of the imported instance.
For me, the user_data is forcing a replacement of the instance. Anyone knows a way around this ? I don't want to replace the instance. Thanks
@kochsecurity - I had the same issue and used the lifecycle feature for the instance
lifecycle { ignore_changes = [ami] }
I am trying to figure out how to import an EC2 instance into Terraform. Whenever I import the instance, if I run "terraform plan" it always wants to replace the instance.
I have tried an extremely minimal import that just has the AMI id and instance size, and I have tried a complete import where every possible value is specified. Neither seem to work, with the same results.
The initial state:
Plan:
Apply:
Post creation, lets check plan:
Now we delete the state file and import the resource:
Another plan: