Helenr
这里正确的做法是告诉Terraform,对资源的更改不能就地完成,而是需要重新创建资源(通常先销毁,然后创建,但你可以用lifecycle.create_before_destroy来逆转)。创建提供程序时,可以使用架构属性上的 ForceNew 参数执行此操作。例如,从 AWS 的 API 端来看,资源被认为是不可变的,因此架构中的每个非计算属性都标记为 ForceNew: true。aws_launch_configurationfunc resourceAwsLaunchConfiguration() *schema.Resource { return &schema.Resource{ Create: resourceAwsLaunchConfigurationCreate, Read: resourceAwsLaunchConfigurationRead, Delete: resourceAwsLaunchConfigurationDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ "arn": { Type: schema.TypeString, Computed: true, }, "name": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, ConflictsWith: []string{"name_prefix"}, ValidateFunc: validation.StringLenBetween(1, 255), },// ...如果您随后尝试修改任何字段,则Terraform的计划将显示它需要替换资源,并且在应用时,只要用户接受该计划,它就会自动执行此操作。ForceNew: true对于更复杂的示例,该资源允许就地进行版本更改,但仅适用于特定的版本升级路径(因此您无法直接从转到,而必须转到。这是通过在架构上使用 CustomizeDiff 属性来完成的,该属性允许您在计划时使用逻辑来提供与静态配置通常不同的结果。aws_elasticsearch_domain5.47.85.4 -> 5.6 -> 6.8 -> 7.8“aws_elasticsearch_domain elasticsearch_version”属性的自定义Diff 如下所示:func resourceAwsElasticSearchDomain() *schema.Resource { return &schema.Resource{ Create: resourceAwsElasticSearchDomainCreate, Read: resourceAwsElasticSearchDomainRead, Update: resourceAwsElasticSearchDomainUpdate, Delete: resourceAwsElasticSearchDomainDelete, Importer: &schema.ResourceImporter{ State: resourceAwsElasticSearchDomainImport, }, Timeouts: &schema.ResourceTimeout{ Update: schema.DefaultTimeout(60 * time.Minute), }, CustomizeDiff: customdiff.Sequence( customdiff.ForceNewIf("elasticsearch_version", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool { newVersion := d.Get("elasticsearch_version").(string) domainName := d.Get("domain_name").(string) conn := meta.(*AWSClient).esconn resp, err := conn.GetCompatibleElasticsearchVersions(&elasticsearch.GetCompatibleElasticsearchVersionsInput{ DomainName: aws.String(domainName), }) if err != nil { log.Printf("[ERROR] Failed to get compatible ElasticSearch versions %s", domainName) return false } if len(resp.CompatibleElasticsearchVersions) != 1 { return true } for _, targetVersion := range resp.CompatibleElasticsearchVersions[0].TargetVersions { if aws.StringValue(targetVersion) == newVersion { return false } } return true }), SetTagsDiff, ),尝试在已接受的升级路径上升级 的 (例如 )将显示它是计划中的就地升级,并在应用时应用。另一方面,如果您尝试通过不允许的路径进行升级(例如直接),那么Terraform的计划将显示它需要销毁现有的弹性搜索域并创建一个新域。aws_elasticsearch_domainelasticsearch_version7.4 -> 7.85.4 -> 7.8