diff --git a/.changelog/39728.txt b/.changelog/39728.txt new file mode 100644 index 00000000000..4358f04248b --- /dev/null +++ b/.changelog/39728.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_route53_record: Allow creation of records with `ttl=0` +``` diff --git a/internal/service/route53/record.go b/internal/service/route53/record.go index aeef59b958c..e845687f067 100644 --- a/internal/service/route53/record.go +++ b/internal/service/route53/record.go @@ -616,14 +616,16 @@ func resourceRecordUpdate(ctx context.Context, d *schema.ResourceData, meta inte } } - if v, _ := d.GetChange("ttl"); v.(int) != 0 { - oldRec.TTL = aws.Int64(int64(v.(int))) - } - // Resource records if v, _ := d.GetChange("records"); v != nil { if v.(*schema.Set).Len() > 0 { oldRec.ResourceRecords = expandResourceRecords(flex.ExpandStringValueSet(v.(*schema.Set)), awstypes.RRType(oldRRType.(string))) + + // TTL and ResourceRecords + if v := d.GetRawState().GetAttr("ttl"); !v.IsNull() { + v, _ := v.AsBigFloat().Int64() + oldRec.TTL = aws.Int64(v) + } } } @@ -904,13 +906,15 @@ func expandResourceRecordSet(d *schema.ResourceData, zoneName string) *awstypes. Type: rrType, } - if v, ok := d.GetOk("ttl"); ok { - apiObject.TTL = aws.Int64(int64(v.(int))) - } - // Resource records if v, ok := d.GetOk("records"); ok { apiObject.ResourceRecords = expandResourceRecords(flex.ExpandStringValueSet(v.(*schema.Set)), rrType) + + // TTL and ResourceRecords + if v := d.GetRawPlan().GetAttr("ttl"); v.IsKnown() && !v.IsNull() { + v, _ := v.AsBigFloat().Int64() + apiObject.TTL = aws.Int64(v) + } } // Alias record diff --git a/internal/service/route53/record_test.go b/internal/service/route53/record_test.go index d562235f666..a660d1c30c4 100644 --- a/internal/service/route53/record_test.go +++ b/internal/service/route53/record_test.go @@ -1530,6 +1530,50 @@ func TestAccRoute53Record_Allow_overwrite(t *testing.T) { }) } +func TestAccRoute53Record_ttl0(t *testing.T) { + ctx := acctest.Context(t) + var v awstypes.ResourceRecordSet + resourceName := "aws_route53_record.test" + zoneName := acctest.RandomDomain() + recordName := zoneName.RandomSubdomain() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.Route53ServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckRecordDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccRecordConfig_ttl(zoneName.String(), strings.ToUpper(recordName.String()), 0), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRecordExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "ttl", acctest.Ct0), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"allow_overwrite", names.AttrWeight}, + }, + { + Config: testAccRecordConfig_ttl(zoneName.String(), strings.ToUpper(recordName.String()), 45), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRecordExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "ttl", "45"), + ), + }, + { + Config: testAccRecordConfig_ttl(zoneName.String(), strings.ToUpper(recordName.String()), 0), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRecordExists(ctx, resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "ttl", acctest.Ct0), + ), + }, + }, + }) +} + // testAccErrorCheckSkip skips Route53 tests that have error messages indicating unsupported features func testAccErrorCheckSkip(t *testing.T) resource.ErrorCheckFunc { return acctest.ErrorCheckSkipMessagesContaining(t, @@ -3195,3 +3239,19 @@ resource "aws_route53_record" "www-server1" { records = ["127.0.0.1"] } ` + +func testAccRecordConfig_ttl(zoneName, recordName string, ttl int) string { + return fmt.Sprintf(` +resource "aws_route53_zone" "test" { + name = %[1]q +} + +resource "aws_route53_record" "test" { + zone_id = aws_route53_zone.test.zone_id + name = %[2]q + type = "A" + ttl = %[3]d + records = ["127.0.0.1", "127.0.0.27"] +} +`, zoneName, recordName, ttl) +}