Skip to content

Commit 3bbbf5a

Browse files
authored
Merge pull request #36961 from robmoss2k/b-amplify-domain-association-verification
Fix issue #29791
2 parents b64416a + 98404fb commit 3bbbf5a

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

.changelog/36961.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_amplify_domain_association: Prevents unexpected state error when creating with multiple `sub_domain`
3+
```

internal/service/amplify/amplify_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestAccAmplify_serial(t *testing.T) {
5151
"certificateSettings_Custom": testAccDomainAssociation_certificateSettings_Custom,
5252
acctest.CtDisappears: testAccDomainAssociation_disappears,
5353
"update": testAccDomainAssociation_update,
54+
"createWithSubdomain": testAccDomainAssociation_CreateWithSubdomain,
5455
},
5556
"Webhook": {
5657
acctest.CtBasic: testAccWebhook_basic,

internal/service/amplify/domain_association.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ func waitDomainAssociationCreated(ctx context.Context, conn *amplify.Client, app
327327
types.DomainStatusPendingVerification,
328328
types.DomainStatusPendingDeployment,
329329
types.DomainStatusAvailable,
330+
types.DomainStatusAwaitingAppCname,
330331
),
331332
Refresh: statusDomainAssociation(ctx, conn, appID, domainName),
332333
Timeout: timeout,
@@ -354,6 +355,8 @@ func waitDomainAssociationVerified(ctx context.Context, conn *amplify.Client, ap
354355
types.DomainStatusUpdating,
355356
types.DomainStatusInProgress,
356357
types.DomainStatusPendingVerification,
358+
types.DomainStatusAwaitingAppCname,
359+
types.DomainStatusImportingCustomCertificate,
357360
),
358361
Target: enum.Slice(
359362
types.DomainStatusPendingDeployment,

internal/service/amplify/domain_association_test.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func testAccDomainAssociation_update(t *testing.T) {
159159
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sub_domain.*", map[string]string{
160160
"branch_name": rName,
161161
names.AttrPrefix: "",
162-
// "verified": acctest.CtTrue, // Even though we're waiting for verification, this isn't getting verified
162+
"verified": acctest.CtTrue,
163163
}),
164164
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sub_domain.*", map[string]string{
165165
"branch_name": fmt.Sprintf("%s-2", rName),
@@ -309,6 +309,59 @@ func testAccDomainAssociation_certificateSettings_Custom(t *testing.T) {
309309
})
310310
}
311311

312+
func testAccDomainAssociation_CreateWithSubdomain(t *testing.T) {
313+
ctx := acctest.Context(t)
314+
key := "AMPLIFY_DOMAIN_NAME"
315+
domainName := os.Getenv(key)
316+
if domainName == "" {
317+
t.Skipf("Environment variable %s is not set", key)
318+
}
319+
320+
var domain types.DomainAssociation
321+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
322+
resourceName := "aws_amplify_domain_association.test"
323+
324+
resource.Test(t, resource.TestCase{
325+
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
326+
ErrorCheck: acctest.ErrorCheck(t, names.AmplifyServiceID),
327+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
328+
CheckDestroy: testAccCheckDomainAssociationDestroy(ctx),
329+
Steps: []resource.TestStep{
330+
{
331+
Config: testAccDomainAssociationConfig_WithSubdomain(rName, domainName, true),
332+
Check: resource.ComposeAggregateTestCheckFunc(
333+
testAccCheckDomainAssociationExists(ctx, resourceName, &domain),
334+
acctest.MatchResourceAttrRegionalARN(ctx, resourceName, names.AttrARN, "amplify", regexache.MustCompile(fmt.Sprintf(`apps/.+/domains/%s$`, domainName))),
335+
resource.TestCheckResourceAttr(resourceName, "certificate_settings.#", "1"),
336+
resource.TestCheckResourceAttrSet(resourceName, "certificate_settings.0.certificate_verification_dns_record"),
337+
resource.TestCheckResourceAttr(resourceName, "certificate_settings.0.custom_certificate_arn", ""),
338+
resource.TestCheckResourceAttr(resourceName, "certificate_settings.0.type", "AMPLIFY_MANAGED"),
339+
resource.TestCheckResourceAttr(resourceName, names.AttrDomainName, domainName),
340+
resource.TestCheckResourceAttr(resourceName, "enable_auto_sub_domain", acctest.CtFalse),
341+
resource.TestCheckResourceAttr(resourceName, "sub_domain.#", "2"),
342+
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sub_domain.*", map[string]string{
343+
"branch_name": rName,
344+
names.AttrPrefix: "",
345+
"verified": acctest.CtTrue, // Even though we're waiting for verification, this isn't getting verified
346+
}),
347+
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "sub_domain.*", map[string]string{
348+
"branch_name": rName,
349+
names.AttrPrefix: "www",
350+
// "verified": acctest.CtTrue, // Even though we're waiting for verification, this isn't getting verified
351+
}),
352+
resource.TestCheckResourceAttr(resourceName, "wait_for_verification", acctest.CtTrue),
353+
),
354+
},
355+
{
356+
ResourceName: resourceName,
357+
ImportState: true,
358+
ImportStateVerify: true,
359+
ImportStateVerifyIgnore: []string{"wait_for_verification"},
360+
},
361+
},
362+
})
363+
}
364+
312365
func testAccCheckDomainAssociationExists(ctx context.Context, n string, v *types.DomainAssociation) resource.TestCheckFunc {
313366
return func(s *terraform.State) error {
314367
rs, ok := s.RootModule().Resources[n]
@@ -513,6 +566,36 @@ resource "aws_acm_certificate_validation" "test" {
513566
`, rName, domainName, enableAutoSubDomain, waitForVerification))
514567
}
515568

569+
func testAccDomainAssociationConfig_WithSubdomain(rName, domainName string, waitForVerification bool) string {
570+
return fmt.Sprintf(`
571+
resource "aws_amplify_domain_association" "test" {
572+
app_id = aws_amplify_app.test.id
573+
domain_name = %[2]q
574+
575+
sub_domain {
576+
branch_name = aws_amplify_branch.test.branch_name
577+
prefix = ""
578+
}
579+
580+
sub_domain {
581+
branch_name = aws_amplify_branch.test.branch_name
582+
prefix = "www"
583+
}
584+
585+
wait_for_verification = %[3]t
586+
}
587+
588+
resource "aws_amplify_app" "test" {
589+
name = %[1]q
590+
}
591+
592+
resource "aws_amplify_branch" "test" {
593+
app_id = aws_amplify_app.test.id
594+
branch_name = %[1]q
595+
}
596+
`, rName, domainName, waitForVerification)
597+
}
598+
516599
// In practice, we don't seem to need to wait for the Domain Association to be `AVAILABLE` for the purposes of deploying infrastructure.
517600
// Since subsequent modifications to a Domain Association cannot occur until it is `AVAILABLE`, wait during tests.
518601
func domainAssociationStatusAvailablePreConfig(ctx context.Context, t *testing.T, app *types.App, domain *types.DomainAssociation) func() {

0 commit comments

Comments
 (0)