Amazon AWS – Deleting the role from instance profile throwing access denied error

I have below code which deletes the inline policy, managed policy and the role it self from the IAM.

For this I have created a role with following custom policies attached to it.

STS

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "sts:AssumeRole"
            ],
            "Resource": "arn:aws:iam::321750505551:role/MathRole-*"
        }
    ]
}

IAM –

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "iam:*",
            "Resource": [
                "arn:aws:iam::321750505551:role/MathRole-*",
                "arn:aws:iam::321750505551:policy/MathPolicy-*"
            ]
        }
    ]
}

My Code is as below

 self.iam_client = boto3.client('iam', verify=False)
    math_role_name = f"MathRole-{userId}-role"
        logger.info(f"Deleting the policies and math role {math_role_name} for test user ")
        # response = iam_client.delete_role(roleName=math_role_name)
        response = self.iam_client.list_role_policies(RoleName=math_role_name)
        for inline_policy in response['PolicyNames']:
            logger.info(f"deleting inline policy: {inline_policy}")
            delete_inline_policy_response = self.iam_client.delete_role_policy(
                RoleName=math_role_name,
                PolicyName=inline_policy
            )
            logger.info(f"deleted inline policy response {delete_inline_policy_response}")
        attached_policies = self.iam_client.list_attached_role_policies(RoleName=math_role_name)
        for items in attached_policies['AttachedPolicies']:
            logger.info(f"detaching the attached policy : {items['PolicyArn']}")
            detach_inline_policy_response = self.iam_client.detach_role_policy(
                RoleName=math_role_name,
                PolicyArn=items['PolicyArn']
            )
            logger.info(f"deleted managed policy response {detach_inline_policy_response}")
        logger.info(f"list instance profile for the role")
        instance_profile = self.iam_client.list_instance_profiles_for_role(
            RoleName=math_role_name
        )
        logger.info(
            f"deleting instance profile for role {math_role_name} :{instance_profile['InstanceProfiles'][0]['InstanceProfileName']}")
        delete_role_from_instance_profile = self.iam_client.remove_role_from_instance_profile(
            InstanceProfileName=instance_profile['InstanceProfiles'][0]['InstanceProfileName'],
            RoleName=math_role_name
        )
        delete_instance_profile = self.iam_client.delete_instance_profile(
            InstanceProfileName=instance_profile['InstanceProfiles'][0]['InstanceProfileName']
        )
        logger.info(f"deleting role : {math_role_name}")
        delete_role = self.iam_client.delete_role(
            RoleName=math_role_name
        )
        logger.info(f"deleted role response {delete_role}")

However When I run with with my role it throws following error

botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the RemoveRoleFromInstanceProfile operation: User: arn:aws:sts::321750505551:assumed-role/MathRole-devops-QA-GithubActions/GitHub_to_AWS_via_FederatedOIDC is not authorized to perform: iam:RemoveRoleFromInstanceProfile on resource: instance profile MathRole-64ed9e32086f90da1426bf51-role because no identity-based policy allows the iam:RemoveRoleFromInstanceProfile action

Any idea what policy I need to attach to the role so that it can delete the role from instance profile

How to remove the intance profile role while delete the role.
What policy I need to attach to my role to delete instance profile role.
Instance profile deletion throws an error.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật