Skip to content
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

refactor(cognito): switch user groups from count to for_each #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

catrielg
Copy link
Contributor

@catrielg catrielg commented Jan 8, 2025

Switch Cognito User Groups from count to for_each

Changes

  • Replaces count-based iteration with for_each in Cognito user group resources
  • Uses group name as the resource identifier instead of list index
  • Maintains existing lookup functionality for optional attributes

Motivation

The previous implementation used count with a list of groups, which made the resources sensitive to list order changes. Any reordering of the groups would cause Terraform to destroy and recreate the groups, even when only the order changed. By switching to for_each with the group name as the key, we ensure stable resource identifiers that are independent of list order.

⚠️ Breaking Change

This change modifies how Terraform identifies user groups, which will trigger recreation of existing groups. This requires careful handling during deployment.

Migration Steps

  1. Before applying:
    • Export existing user group memberships to a backup location
    • You can use AWS CLI or SDK:
      aws cognito-idp list-users-in-group --user-pool-id <pool_id> --group-name <group_name>
  2. Apply the changes
  3. After applying:
    • Restore user group memberships from backup
    • You can use AWS CLI or SDK:
      aws cognito-idp admin-add-user-to-group --user-pool-id <pool_id> --group-name <group_name> --username <username>

BREAKING CHANGE: Changes user group resource identifiers from index-based to name-based, requiring group recreation. Backup group memberships before applying.
@lgallard
Copy link
Owner

@catrielg thanks for the PR. I like the refactoring. My only concern is the breaking code part and wonder if we can use terraform move to have a flawless transition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants