In this AWS with Python & Boto3 lesson we want to learn how to Update AWS IAM User with Python & Boto3, AWS IAM users represent individuals or entities with access to AWS services and resources. If you want to update an IAM user with Python, then you can modify attributes such as usernames, groups, permissions, login profiles, and many more.
Make sure that you have already read these two article 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
Prerequisites: Before we start updating IAM users with Python and Boto3, ensure you have the 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).
Update AWS IAM User with Python & Boto3
Let’s learn step by step updating IAM users programmatically using Python and Boto3:
Step 1: Import Boto3
Start by importing the Boto3 library in your Python script:
1 |
import boto3 |
Step 2: Initialize Boto3 IAM Client
Next, initialize the Boto3 IAM client to interact with AWS IAM:
1 |
iam = boto3.client('iam') |
Step 3: Define Update Operations
Identify the updates you want to perform on IAM users, such as modifying usernames, adding/removing users from groups, updating permissions, or resetting passwords. Execute update operations using appropriate Boto3 methods.
This is an example of updating an IAM user’s username:
1 2 3 4 |
iam.update_user( UserName='existing_username', NewUserName='new_username' ) |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import boto3 # Initialize Boto3 IAM client iam = boto3.client('iam') # Define existing and new usernames existing_username = 'codeloop' new_username = 'codeloop-updated' # Update IAM user's username response = iam.update_user( UserName=existing_username, NewUserName=new_username ) # Print response if needed print("IAM user's username updated successfully:", response) |
Run the code you will see this in the console
And if you check AWS Management console, then you will see updated use in the list of uses.
FAQs
Q: What types of updates can I perform on IAM users using Python and Boto3?
A: You can perform different updates on IAM users, for example you can modify usernames, adding/removing users from groups, updating permissions (through policies), managing access keys, resetting passwords and updating user details.
Q: Can I update multiple IAM users simultaneously?
A: Yes, you can iterate over a list of IAM users and apply updates to multiple users programmatically using Python and Boto3.
Q: Are there any permissions required to update IAM users?
A: Yes, you need appropriate permissions granted by an IAM user, group or role inside your AWS account. Permissions for IAM user management actions such as iam:UpdateUser and iam:UpdateLoginProfile are required.
Learn More on AWS IAM:
Subscribe and Get Free Video Courses & Articles in your Email