Skip to main content

More Info:

Role should not have inline policies attached to them.

Risk Level

Low

Address

Security

Compliance Standards

PCIDSS, NIST, GDPR

Triage and Remediation

  • Prevention
  • Cause
  • Remediation

How to Prevent

Using Console

To prevent roles from having inline policies in AWS IAM using the AWS Management Console, follow these steps:
  1. Navigate to IAM Dashboard:
    • Sign in to the AWS Management Console.
    • Open the IAM (Identity and Access Management) dashboard by selecting “IAM” from the services menu.
  2. Review Existing Roles:
    • In the IAM dashboard, select “Roles” from the left-hand navigation pane.
    • Review the list of roles to identify any roles that have inline policies attached.
  3. Remove Inline Policies:
    • Click on the role name to view its details.
    • In the “Permissions” tab, look for any inline policies listed under “Inline Policies.”
    • For each inline policy, click on the policy name and then select “Delete” to remove the inline policy from the role.
  4. Attach Managed Policies:
    • Instead of using inline policies, attach managed policies to the role.
    • In the role details, go to the “Permissions” tab and click “Attach policies.”
    • Select the appropriate managed policies from the list and click “Attach policy” to apply them to the role.
By following these steps, you can ensure that roles do not have inline policies and instead use managed policies, which are easier to manage and audit.
To prevent roles from having inline policies in AWS IAM using the AWS CLI, you can follow these steps:
  1. List All Roles: First, list all the IAM roles in your AWS account to identify which roles might have inline policies.
    aws iam list-roles
    
  2. Check for Inline Policies: For each role, check if there are any inline policies attached. Replace <role-name> with the actual role name.
    aws iam list-role-policies --role-name <role-name>
    
  3. Create Managed Policies: If you need to attach policies to roles, create managed policies instead of inline policies. This ensures that policies are reusable and easier to manage. Here is an example of creating a managed policy:
    aws iam create-policy --policy-name MyManagedPolicy --policy-document file://policy.json
    
  4. Attach Managed Policies to Roles: Attach the newly created managed policy to the role. Replace <role-name> with the actual role name and <policy-arn> with the ARN of the managed policy.
    aws iam attach-role-policy --role-name <role-name> --policy-arn <policy-arn>
    
By following these steps, you can prevent roles from having inline policies and ensure that your IAM policies are managed more effectively.
To prevent IAM roles from having inline policies in AWS using Python scripts, you can use the Boto3 library, which is the AWS SDK for Python. Here are the steps to achieve this:

Step 1: Install Boto3

First, ensure you have Boto3 installed. You can install it using pip if you haven’t already:
pip install boto3

Step 2: Initialize Boto3 Client

Initialize the Boto3 client for IAM:
import boto3

iam_client = boto3.client('iam')

Step 3: List All Roles

Fetch all IAM roles in your AWS account:
def list_roles():
    roles = []
    paginator = iam_client.get_paginator('list_roles')
    for page in paginator.paginate():
        roles.extend(page['Roles'])
    return roles

Step 4: Check and Remove Inline Policies

For each role, check if there are any inline policies and remove them if they exist:
def remove_inline_policies(role_name):
    inline_policies = iam_client.list_role_policies(RoleName=role_name)['PolicyNames']
    for policy_name in inline_policies:
        iam_client.delete_role_policy(RoleName=role_name, PolicyName=policy_name)
        print(f"Removed inline policy {policy_name} from role {role_name}")

def main():
    roles = list_roles()
    for role in roles:
        remove_inline_policies(role['RoleName'])

if __name__ == "__main__":
    main()

Summary

  1. Install Boto3: Ensure Boto3 is installed in your Python environment.
  2. Initialize Boto3 Client: Set up the IAM client using Boto3.
  3. List All Roles: Retrieve all IAM roles in your AWS account.
  4. Check and Remove Inline Policies: For each role, check for inline policies and remove them if they exist.
This script will help you prevent IAM roles from having inline policies by removing any existing inline policies. You can run this script periodically or integrate it into your CI/CD pipeline to ensure compliance.

Additional Reading:

I