- Newest
- Most votes
- Most comments
When you import an existing EC2 instance into Terraform and then run terraform plan, you may see that Terraform plans to replace the resource rather than simply manage it as-is. This behavior can occur for a few reasons:
-
Configuration mismatch: The Terraform configuration you've written for the EC2 instance might not exactly match the current state of the instance in AWS. Even small differences in attributes can cause Terraform to plan a replacement.
-
Default values: Terraform may be applying default values for certain attributes that weren't explicitly set when the instance was created manually. These defaults might differ from the actual instance configuration.
-
Computed attributes: Some attributes of EC2 instances are computed by AWS and may not be known until after creation. Terraform might not be able to accurately compare these attributes during the plan phase.
-
Instance metadata: Certain instance metadata or tags that were set outside of Terraform might not be recognized, leading to a planned replacement.
-
Terraform state inconsistency: If the Terraform state doesn't accurately reflect the current state of the resource in AWS, it may lead to unexpected plan results.
To resolve this issue, you should carefully compare your Terraform configuration with the actual EC2 instance settings in the AWS console. Ensure that all relevant attributes are correctly specified in your Terraform code. You may need to use the terraform show command to see the current state and adjust your configuration accordingly.
Remember that Terraform's behavior is based on declarative configurations, and it aims to make the infrastructure match the desired state described in your code. If there are discrepancies between the code and the actual resource, Terraform will plan changes to reconcile them.
Sources
Understanding Terraform modules - AWS Prescriptive Guidance
Understanding Terraform providers - AWS Prescriptive Guidance
Relevant content
- AWS OFFICIALUpdated 2 years ago

Agree. Check in the plan which variable is triggering the REPLACE.