Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

Add AWS::Cognito::UserPoolGroup CloudFormation support (#1127)

ryan-bennettfdb3f7bb52bf
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 04a871e2..e42bd062 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -2529,6 +2529,46 @@ def _cognito_user_pool_client_delete(physical_id, props):
pool["_clients"].pop(physical_id, None)
+# --- Cognito UserPoolGroup ---
+
+def _cognito_user_pool_group_create(logical_id, props, stack_name):
+ pid = props.get("UserPoolId", "")
+ pool = _cognito._user_pools.get(pid)
+ if not pool:
+ raise ValueError(f"UserPool {pid} not found for UserPoolGroup")
+
+ name = props.get("GroupName") or _physical_name(stack_name, logical_id, max_len=128)
+ now = _cognito._now_epoch()
+ group = {
+ "GroupName": name,
+ "UserPoolId": pid,
+ "Description": props.get("Description", ""),
+ "RoleArn": props.get("RoleArn", ""),
+ "Precedence": props.get("Precedence", 0),
+ "CreationDate": now,
+ "LastModifiedDate": now,
+ "_members": [],
+ }
+ pool["_groups"][name] = group
+ # Ref on this resource type returns the GroupName (matches real AWS —
+ # see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolgroup.html#aws-resource-cognito-userpoolgroup-return-values).
+ return name, {}
+
+
+def _cognito_user_pool_group_delete(physical_id, props):
+ pid = props.get("UserPoolId", "")
+ pool = _cognito._user_pools.get(pid)
+ if not pool:
+ return
+ group = pool["_groups"].pop(physical_id, None)
+ if not group:
+ return
+ for username in group.get("_members", []):
+ user = pool["_users"].get(username)
+ if user and physical_id in user.get("_groups", []):
+ user["_groups"].remove(physical_id)
+
+
# --- Cognito IdentityPool ---
def _cognito_identity_pool_create(logical_id, props, stack_name):
@@ -4633,6 +4673,7 @@ _RESOURCE_HANDLERS = {
"AWS::SecretsManager::Secret": {"create": _sm_secret_create, "delete": _sm_secret_delete},
"AWS::Cognito::UserPool": {"create": _cognito_user_pool_create, "delete": _cognito_user_pool_delete},
"AWS::Cognito::UserPoolClient": {"create": _cognito_user_pool_client_create, "delete": _cognito_user_pool_client_delete},
+ "AWS::Cognito::UserPoolGroup": {"create": _cognito_user_pool_group_create, "delete": _cognito_user_pool_group_delete},
"AWS::Cognito::IdentityPool": {"create": _cognito_identity_pool_create, "delete": _cognito_identity_pool_delete},
"AWS::Cognito::UserPoolDomain": {"create": _cognito_user_pool_domain_create, "delete": _cognito_user_pool_domain_delete},
"AWS::ECR::Repository": {"create": _ecr_repo_create, "delete": _ecr_repo_delete},
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 3b1fbbf7..709bd79d 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -3157,6 +3157,57 @@ Outputs:
assert not without_resp["UserPoolClient"].get("ClientSecret"), "GenerateSecret=false should leave ClientSecret empty"
+def test_cfn_cognito_user_pool_group(cfn, cognito_idp):
+ """CFN AWS::Cognito::UserPoolGroup creates a group whose Ref resolves to
+ its GroupName, matching real AWS, and admin_add_user_to_group can then
+ reference it."""
+ template = """
+AWSTemplateFormatVersion: '2010-09-09'
+Resources:
+ Pool:
+ Type: AWS::Cognito::UserPool
+ Properties:
+ UserPoolName: cfn-group-pool
+ AdminGroup:
+ Type: AWS::Cognito::UserPoolGroup
+ Properties:
+ UserPoolId: !Ref Pool
+ GroupName: admins
+ Description: Administrators
+ Precedence: 1
+Outputs:
+ PoolId:
+ Value: !Ref Pool
+ GroupRef:
+ Value: !Ref AdminGroup
+"""
+ stack_name = "cfn-cognito-group"
+ try:
+ cfn.delete_stack(StackName=stack_name)
+ except Exception:
+ pass
+ cfn.create_stack(StackName=stack_name, TemplateBody=template)
+ _wait_stack(cfn, stack_name)
+
+ stack = cfn.describe_stacks(StackName=stack_name)["Stacks"][0]
+ outputs = {o["OutputKey"]: o["OutputValue"] for o in stack.get("Outputs", [])}
+ assert outputs["GroupRef"] == "admins"
+
+ group = cognito_idp.get_group(UserPoolId=outputs["PoolId"], GroupName="admins")["Group"]
+ assert group["Description"] == "Administrators"
+ assert group["Precedence"] == 1
+
+ cognito_idp.admin_create_user(UserPoolId=outputs["PoolId"], Username="alice")
+ cognito_idp.admin_add_user_to_group(UserPoolId=outputs["PoolId"], Username="alice", GroupName="admins")
+ groups = cognito_idp.admin_list_groups_for_user(UserPoolId=outputs["PoolId"], Username="alice")["Groups"]
+ assert any(g["GroupName"] == "admins" for g in groups)
+
+ cfn.delete_stack(StackName=stack_name)
+ with pytest.raises(ClientError) as exc:
+ cognito_idp.get_group(UserPoolId=outputs["PoolId"], GroupName="admins")
+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
+
+
# ---------------------------------------------------------------------------
# ApiGatewayV2 Integration + Route provisioners
# ---------------------------------------------------------------------------