2022-01-27 16:41:51 -08:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to use AWS Identity and Access Management (IAM) groups.
"""
# snippet-start:[python.example_code.iam.group_wrapper.imports]
import logging
import boto3
from botocore . exceptions import ClientError
logger = logging . getLogger ( __name__ )
2023-10-18 10:35:05 -07:00
iam = boto3 . resource ( " iam " )
2022-01-27 16:41:51 -08:00
# snippet-end:[python.example_code.iam.group_wrapper.imports]
# snippet-start:[python.example_code.iam.ListGroups]
def list_groups ( count ) :
"""
Lists the specified number of groups for the account.
:param count: The number of groups to list.
"""
try :
for group in iam . groups . limit ( count ) :
logger . info ( " Group: %s " , group . name )
except ClientError :
logger . exception ( " Couldn ' t list groups for the account. " )
raise
2023-10-18 10:35:05 -07:00
2022-01-27 16:41:51 -08:00
# snippet-end:[python.example_code.iam.ListGroups]
def usage_demo ( ) :
2023-10-18 10:35:05 -07:00
print ( " - " * 88 )
2022-01-27 16:41:51 -08:00
print ( " Welcome to the IAM groups demo! " )
2023-10-18 10:35:05 -07:00
print ( " - " * 88 )
2022-01-27 16:41:51 -08:00
2023-10-18 10:35:05 -07:00
logging . basicConfig ( level = logging . INFO , format = " %(levelname)s : %(message)s " )
2022-01-27 16:41:51 -08:00
print ( " Listing up to 10 groups for the account. " )
list_groups ( 10 )
print ( " \n Thanks for watching! " )
2023-10-18 10:35:05 -07:00
print ( " - " * 88 )
2022-01-27 16:41:51 -08:00
2023-10-18 10:35:05 -07:00
if __name__ == " __main__ " :
2022-01-27 16:41:51 -08:00
usage_demo ( )