Skip to content

[Enhancement] aws_cognito_user_pool: Update name attribute without resource replacement #42639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/42639.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cognito_user_pool: The name attribute can now be updated in-place, instead of requiring resource replacement
```
6 changes: 5 additions & 1 deletion internal/service/cognitoidp/user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ func resourceUserPool() *schema.Resource {
names.AttrName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.Any(
validation.StringLenBetween(1, 128),
validation.StringMatch(regexache.MustCompile(`[\w\s+=,.@-]+`),
Expand Down Expand Up @@ -1069,6 +1068,7 @@ func resourceUserPoolUpdate(ctx context.Context, d *schema.ResourceData, meta an
"email_verification_message",
"email_verification_subject",
"lambda_config",
names.AttrName,
"password_policy",
"sign_in_policy",
"sms_authentication_message",
Expand Down Expand Up @@ -1148,6 +1148,10 @@ func resourceUserPoolUpdate(ctx context.Context, d *schema.ResourceData, meta an
}
}

if v, ok := d.GetOk(names.AttrName); ok {
input.PoolName = aws.String(v.(string))
}

if v, ok := d.GetOk("mfa_configuration"); ok {
input.MfaConfiguration = awstypes.UserPoolMfaType(v.(string))
}
Expand Down
44 changes: 44 additions & 0 deletions internal/service/cognitoidp/user_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,38 @@ func TestAccCognitoIDPUserPool_userPoolTier(t *testing.T) {
})
}

func TestAccCognitoIDPUserPool_nameUpdate(t *testing.T) {
ctx := acctest.Context(t)
var pool1, pool2 awstypes.UserPoolType
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_cognito_user_pool.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckIdentityProvider(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.CognitoIDPServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckUserPoolDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccUserPoolConfig_name(rName + "-test1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckUserPoolExists(ctx, resourceName, &pool1),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName+"-test1"),
),
},
{
Config: testAccUserPoolConfig_name(rName + "-test2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckUserPoolExists(ctx, resourceName, &pool2),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName+"-test2"),
// check that the user pool was not recreated
testAccCheckUserPoolIdEqual(&pool1, &pool2),
),
},
},
})
}

func testAccCheckUserPoolDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).CognitoIDPClient(ctx)
Expand Down Expand Up @@ -2099,6 +2131,18 @@ func testAccPreCheckIdentityProvider(ctx context.Context, t *testing.T) {
acctest.PreCheckCognitoIdentityProvider(ctx, t)
}

func testAccCheckUserPoolIdEqual(pool1 *awstypes.UserPoolType, pool2 *awstypes.UserPoolType) resource.TestCheckFunc {
return func(s *terraform.State) error {
if pool1.Id == nil || pool2.Id == nil {
return fmt.Errorf("user pool ID is nil")
}
if aws.ToString(pool1.Id) != aws.ToString(pool2.Id) {
return fmt.Errorf("user pool %s should not have been replaced with %s", aws.ToString(pool1.Id), aws.ToString(pool2.Id))
}
return nil
}
}

func testAccUserPoolSMSConfigurationConfig_base(rName string, externalID string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
Expand Down
Loading