This Python script automates the process of converting inline IAM policies attached to roles into managed IAM policies. It ensures efficient policy reuse by identifying identical inline policies and linking them to a single managed policy. Additionally, it resolves conflicts in policy naming by appending unique timestamps when necessary.
- Inline to Managed Policy Conversion:
- Migrates inline policies attached to roles into managed policies.
- Policy Deduplication:
- Ensures identical inline policies are converted to a single managed policy.
- Conflict Resolution:
- Handles duplicate managed policy names by appending a unique timestamp.
- Final Validation:
- Verifies that all inline policies are removed from the specified roles.
- AWS CLI:
- Ensure you have the AWS CLI installed and configured with appropriate permissions.
- Install AWS CLI
- Boto3:
- Install the AWS SDK for Python using pip:
pip install boto3
- Install the AWS SDK for Python using pip:
- Permissions:
- The script requires the following IAM permissions:
iam:ListRolePolicies
iam:GetRolePolicy
iam:DeleteRolePolicy
iam:ListPolicies
iam:GetPolicy
iam:GetPolicyVersion
iam:CreatePolicy
iam:AttachRolePolicy
- The script requires the following IAM permissions:
-
Clone the Script:
- Copy the script into a local
.py
file, e.g.,role-inline-to-managed.py
.
- Copy the script into a local
-
Run the Script:
- Provide a comma-separated list of role ARNs when prompted.
- Example:
python role-inline-to-managed.py Enter comma-separated role ARNs: arn:aws:iam::123456789012:role/MyRole1,arn:aws:iam::123456789012:role/MyRole2
-
Script Workflow:
- The script performs the following steps for each role:
- Validation: Ensures role ARNs are in the correct format.
- Fetch Inline Policies: Retrieves all inline policies attached to the role.
- Policy Deduplication: Computes a hash of each policy document to check for existing identical managed policies.
- Managed Policy Creation or Reuse:
- If no existing policy matches, a new managed policy is created.
- Handles name conflicts by appending a unique timestamp.
- Attachment and Removal:
- Attaches the managed policy to the role.
- Removes the original inline policy.
- Final Validation: Ensures the role has no remaining inline policies.
- The script performs the following steps for each role:
Processing role: MyRole1
Found 2 inline policies for role MyRole1
Processing inline policy: Policy1
Created managed policy: arn:aws:iam::123456789012:policy/Policy1
Attached managed policy arn:aws:iam::123456789012:policy/Policy1 to role MyRole1
Removed inline policy: Policy1
All inline policies removed for role MyRole1
Processing inline policy: Policy2
Reusing existing managed policy: arn:aws:iam::123456789012:policy/Policy2
Attached managed policy arn:aws:iam::123456789012:policy/Policy2 to role MyRole2
Removed inline policy: Policy2
-
IAM Policy Limits:
- Managed policies have limits on the number of characters, statements, and conditions. Ensure inline policies are within these limits.
- IAM Policy Quotas
-
Error Handling:
- The script gracefully handles duplicate managed policy names and ensures unique naming.
- In case of permission issues or other errors, the script stops execution and displays the error message.
-
Testing:
- It is recommended to test the script in a non-production environment to ensure it works as expected before running on production roles.
This script is provided under the MIT License. Feel free to use, modify, and distribute it.