1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
這里正確的做法是告訴Terraform,對(duì)資源的更改不能就地完成,而是需要重新創(chuàng)建資源(通常先銷(xiāo)毀,然后創(chuàng)建,但你可以用lifecycle.create_before_destroy
來(lái)逆轉(zhuǎn))。
創(chuàng)建提供程序時(shí),可以使用架構(gòu)屬性上的 ForceNew
參數(shù)執(zhí)行此操作。
例如,從 AWS 的 API 端來(lái)看,資源被認(rèn)為是不可變的,因此架構(gòu)中的每個(gè)非計(jì)算屬性都標(biāo)記為 ForceNew: true
。aws_launch_configuration
func 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的計(jì)劃將顯示它需要替換資源,并且在應(yīng)用時(shí),只要用戶(hù)接受該計(jì)劃,它就會(huì)自動(dòng)執(zhí)行此操作。ForceNew: true
對(duì)于更復(fù)雜的示例,該資源允許就地進(jìn)行版本更改,但僅適用于特定的版本升級(jí)路徑(因此您無(wú)法直接從轉(zhuǎn)到,而必須轉(zhuǎn)到。這是通過(guò)在架構(gòu)上使用 CustomizeDiff
屬性來(lái)完成的,該屬性允許您在計(jì)劃時(shí)使用邏輯來(lái)提供與靜態(tài)配置通常不同的結(jié)果。aws_elasticsearch_domain
5.4
7.8
5.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,
),
嘗試在已接受的升級(jí)路徑上升級(jí) 的 (例如 )將顯示它是計(jì)劃中的就地升級(jí),并在應(yīng)用時(shí)應(yīng)用。另一方面,如果您嘗試通過(guò)不允許的路徑進(jìn)行升級(jí)(例如直接),那么Terraform的計(jì)劃將顯示它需要銷(xiāo)毀現(xiàn)有的彈性搜索域并創(chuàng)建一個(gè)新域。aws_elasticsearch_domainelasticsearch_version7.4 -> 7.85.4 -> 7.8
- 1 回答
- 0 關(guān)注
- 116 瀏覽
添加回答
舉報(bào)