1 回答

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