Skip to content

Using terraform with codepipeline

0

I use codepipeline to test out whether I can deploy resource by terraform. EC2 is provisioned. I then import the resource into terraform, then run terraform plan. I see that it will replaced the one on aws. Can anyone explain why this happen ?

asked 2 years ago246 views

1 Answer
0

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:

  1. 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.

  2. 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.

  3. 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.

  4. Instance metadata: Certain instance metadata or tags that were set outside of Terraform might not be recognized, leading to a planned replacement.

  5. 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

answered 2 years ago

EXPERT

reviewed 2 years ago

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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.