为何Terraform import命令不自动生成配置文件?
import Doesn't Generate Configuration Terraform's import command only registers existing infrastructure into the Terraform state file—it won't generate corresponding configuration code. Here are the core reasons for this design:
Ambiguity in resource attributes
Cloud provider APIs return dozens of attributes for any resource, including user-specified parameters, auto-generated metadata, computed values, and default settings. Terraform can't reliably tell which attributes need to be explicitly defined in configuration (likeamifor an EC2 instance) and which are meant to be managed automatically by the state (likeprivate_ip). Auto-generating config would likely include unnecessary or incorrect fields, causing confusion down the line.Configuration reflects intent, not just current state
Terraform configuration represents your desired state for infrastructure, not just a snapshot of what's currently running. Manually created resources often have default settings or one-off changes you don't want to lock into managed infrastructure. For example, a manually provisioned VM might use a default security group, but you might plan to replace it with a custom one later. Auto-generated config would preserve those accidental defaults, conflicting with your long-term management goals.Provider compatibility hurdles
Each Terraform provider interacts with a unique API, and attribute mappings between API responses and Terraform resource arguments vary widely. Building logic to automatically translate API data into valid, idiomatic Terraform config would require massive effort across all providers, and would still be prone to errors from edge cases and API updates.Enforcing explicit user oversight
Terraform's core design separates configuration (your intent) from state (current reality). Requiring you to write config after import ensures you fully understand the resources you're bringing under management. This prevents "black box" operations where you runimportand apply changes without reviewing what's in the generated config, leading to unintended infrastructure modifications.
官方说明:您可以使用import命令将现有资源迁移至Terraform状态文件中。目前import命令不会为导入的资源生成配置,因此您必须编写对应的配置块来映射导入的资源。
内容的提问来源于stack exchange,提问作者samsina

