In this Python AWS lesson we want to learn How to Add Users to AWS Groups with Python, we already have talked about AWS Groups, that IAM user groups are collections of IAM users with similar permissions requirements. By assigning IAM policies to user groups, you can grant permissions to all users in the group at once, for example you work for a large software development company that uses AWS for its infrastructure and development needs. Your company follows best practices for access management by implementing IAM user groups to organize users and enforce least privilege access.
Prerequisites:
Before we start creating IAM user groups with Python and Boto3, we need to have following prerequisites:
-
- Python installed on your system.
- Boto3 library installed (pip install boto3).
- AWS credentials configured on your system (either through AWS CLI or environment variables).
Also make sure that you have already read these three articles, because they are related to this article.
- How to install Boto3 and AWS CLI for Python
- How to Configure AWS CLI to Use Boto3
- How to Create AWS IAM User with Python & Boto3
Adding Users to IAM Groups with Python & Boto3
Let’s talk about the steps steps involved in adding users to IAM groups programmatically using Python and Boto3:
Step 1: Import Boto3
Start by importing the Boto3 library in your Python code:
1 |
import boto3 |
Step 2: Initialize Boto3 IAM Client
After that initialize the Boto3 IAM client to interact with AWS IAM:
1 |
iam = boto3.client('iam') |
Step 3: Define IAM Group Name and User Name
Specify the name of the IAM group and also the users that you want to add.
1 2 3 4 |
group_name = 'MyIAMGroup' # Add the names of the users you want to add to the group user_names = ['user1', 'user2', 'user3'] |
Step 4: Add Users to IAM Group
Use the add_user_to_group method to add each user to the IAM group:
1 2 3 4 5 6 |
for user_name in user_names: response = iam.add_user_to_group( GroupName=group_name, UserName=user_name ) print("User '{}' added to group '{}' successfully.".format(user_name, group_name)) |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import boto3 # Initialize Boto3 IAM client iam = boto3.client('iam') # Define IAM group name and user names group_name = 'MyUserMGroup' # Add the names of the users you # want to add to the group user_names = ['codeloop-updated'] # Add users to IAM group for user_name in user_names: response = iam.add_user_to_group( GroupName=group_name, UserName=user_name ) print("User '{}' added to group '{}' successfully.".format(user_name, group_name)) |
Also check these articles
Now if you check AWS Group, you will see the user in the group, make sure to change the group name and username.
FAQs:
How do I add a user to an AWS group?
For adding a user to an AWS group, you can use add_user_to_group method in Boto3, Boto3 is official AWS SDK for Python. This method allows you to specify the name of the IAM group and the name of the user you want to add.
How do I list users in AWS Python?
For listing users in AWS using Python, you can use list_users method in Boto3, Boto3 is official AWS SDK for Python. This method allows you to retrieve information about all IAM users in your AWS account.
How to connect AWS using Python?
For connecting AWS with Python, you can use Boto3 library, which is the official AWS SDK for Python. First, you need to install the Boto3 library (pip install boto3). After that you can initialize the Boto3 client or resource for the AWS service you want to interact.
Subscribe and Get Free Video Courses & Articles in your Email