Best Python code snippet using localstack_python
delete_iam_role.py
Source:delete_iam_role.py  
...88    except botocore.exceptions.ClientError as error:           89        return (f"policy {aws_manage_policy_5} is not attached to {role_name_3}")90                              91    try: 92       detach_role_policy(role_name_1,policy_name_1)93        94       try:95          delete__policy(policy_name_1)96       except botocore.exceptions.ClientError as error:97           98          return (f"policy {policy_name_1} is not attached to another role apart from {role_name_1}")99           100    except botocore.exceptions.ClientError as error:           101        return (f"policy {policy_name_1} is not attached to {role_name_1}")102        103    try: 104       detach_role_policy(role_name_2,policy_name_2)105       try:106          delete__policy(policy_name_2)107       except botocore.exceptions.ClientError as error:108           109          return (f"policy {policy_name_2} is not attached to another role apart from {role_name_2}")110           111    except botocore.exceptions.ClientError as error:           112        return (f"policy {policy_name_2} is not attached to {role_name_2}")113        114    try: 115       detach_role_policy(role_name_2,policy_name_3)116       try:117          delete__policy(policy_name_3)118       except botocore.exceptions.ClientError as error:119           120          return (f"policy {policy_name_3} is not attached to another role apart from {role_name_2}")121           122    except botocore.exceptions.ClientError as error:           123        return (f"policy {policy_name_3} is not attached to {role_name_2}")  124    try: 125       detach_role_policy(role_name_3,policy_name_4)126       try:127          delete__policy(policy_name_4)128       except botocore.exceptions.ClientError as error:129           130          return (f"policy {policy_name_4} is not attached to another role apart from {role_name_3}")131           132    except botocore.exceptions.ClientError as error:           133        return (f"policy {policy_name_4} is not attached to {role_name_3}")        134 135    try: 136       detach_role_policy(role_name_3,policy_name_5)137       try:138          delete__policy(policy_name_5)139       except botocore.exceptions.ClientError as error:140           141          return (f"policy {policy_name_5} is not attached to another role apart from {role_name_3}")142           143    except botocore.exceptions.ClientError as error:           144        return (f"policy {policy_name_5} is not attached to {role_name_3}")        145    try: 146       detach_role_policy(role_name_3,policy_name_6)147       try:148          delete__policy(policy_name_6)149       except botocore.exceptions.ClientError as error:150           151          return (f"policy {policy_name_6} is not attached to another role apart from {role_name_3}")152           153    except botocore.exceptions.ClientError as error:           154        return (f"policy {policy_name_6} is not attached to {role_name_3}")        155    try: 156       detach_role_policy(role_name_3,policy_name_7)157       try:158          delete__policy(policy_name_7)159       except botocore.exceptions.ClientError as error:160           161          return (f"policy {policy_name_7} is not attached to another role apart from {role_name_3}")162           163    except botocore.exceptions.ClientError as error:           164        return (f"policy {policy_name_7} is not attached to {role_name_3}")        165    166    try: 167       detach_role_policy(role_name_3,policy_name_8)168       try:169          delete__policy(policy_name_8)170       except botocore.exceptions.ClientError as error:171           172          return (f"policy {policy_name_8} is not attached to another role apart from {role_name_3}")173           174    except botocore.exceptions.ClientError as error:           175        return (f"policy {policy_name_8} is not attached to {role_name_3}")    176        177    try: 178       detach_role_policy(role_name_3,policy_name_9)179       try:180          delete__policy(policy_name_9)181       except botocore.exceptions.ClientError as error:182           183          return (f"policy {policy_name_9} is not attached to another role apart from {role_name_3}")184           185    except botocore.exceptions.ClientError as error:           186        return (f"policy {policy_name_9} is not attached to {role_name_3}")  187        188    try: 189       detach_role_policy(role_name_3,policy_name_10)190       try:191          delete__policy(policy_name_10)192       except botocore.exceptions.ClientError as error:193           194          return (f"policy {policy_name_10} is not attached to another role apart from {role_name_3}")195           196    except botocore.exceptions.ClientError as error:           197        return (f"policy {policy_name_10} is not attached to {role_name_3}")        198       199    try: 200        delete_role(role_name_1)201    except botocore.exceptions.ClientError as error:           202        return (f"policies are either still attached or  {role_name_1} does not exist")        203 204 205    try: 206        delete_role(role_name_2)207    except botocore.exceptions.ClientError as error:           208        return (f"policies are either still attached or  {role_name_2} does not exist")        209 210    try: 211        delete_role(role_name_3)212    except botocore.exceptions.ClientError as error:           213        return (f"policies are either still attached or  {role_name_3} does not exist")  214    215    res = {216        "accountData": {217            "accountId": account_num218        },219        "deleteData": {220            "service": aws_service221        },222        "status": f"{role_name_1},{role_name_2},{role_name_3} have successfully been deleted" 223    }224    return res        225            226def detach_role_policy(role_name,policy_name):227    sts = boto3.client("sts") 228    logger.info(f"Starting scan of new account {account_num}")229    logger.info(f"account_num: {account_num}")230    role_arn = f"arn:aws:iam::{account_num}:role/KB_assumed_role"231    sts_auth = sts.assume_role(RoleArn=role_arn, RoleSessionName="acquired_account_role")232    credentials = sts_auth["Credentials"]233    234    # ----------------------------- #235    # Place all service code below236    # ----------------------------- #237    # Section for boto3 connection with aws service238    sts_client = boto3.client(aws_service,239                              region_name=target_region,240                              aws_access_key_id=credentials["AccessKeyId"],241                              aws_secret_access_key=credentials["SecretAccessKey"],242                              aws_session_token=credentials["SessionToken"], )    243    244    response= sts_client.detach_role_policy(245    RoleName=role_name,246    PolicyArn=f"arn:aws:iam::{account_num}:policy/{policy_name}"247      )248def delete__policy(policy_name):249    sts = boto3.client("sts") 250    logger.info(f"Starting scan of new account {account_num}")251    logger.info(f"account_num: {account_num}")252    role_arn = f"arn:aws:iam::{account_num}:role/KB_assumed_role"253    sts_auth = sts.assume_role(RoleArn=role_arn, RoleSessionName="acquired_account_role")254    credentials = sts_auth["Credentials"]255    256    # ----------------------------- #257    # Place all service code below258    # ----------------------------- #259    # Section for boto3 connection with aws service260    sts_client = boto3.client(aws_service,261                              region_name=target_region,262                              aws_access_key_id=credentials["AccessKeyId"],263                              aws_secret_access_key=credentials["SecretAccessKey"],264                              aws_session_token=credentials["SessionToken"], )265                              266    response= sts_client.delete_policy(267            PolicyArn=f"arn:aws:iam::{account_num}:policy/{policy_name}"268        ) 269def delete_role(role_name):270    sts = boto3.client("sts") 271    logger.info(f"Starting scan of new account {account_num}")272    logger.info(f"account_num: {account_num}")273    role_arn = f"arn:aws:iam::{account_num}:role/KB_assumed_role"274    sts_auth = sts.assume_role(RoleArn=role_arn, RoleSessionName="acquired_account_role")275    credentials = sts_auth["Credentials"]276    277    # ----------------------------- #278    # Place all service code below279    # ----------------------------- #280    # Section for boto3 connection with aws service281    sts_client = boto3.client(aws_service,282                              region_name=target_region,283                              aws_access_key_id=credentials["AccessKeyId"],284                              aws_secret_access_key=credentials["SecretAccessKey"],285                              aws_session_token=credentials["SessionToken"], )286    287    response_= sts_client.delete_role(288        RoleName=role_name289    )  290def detach_AWS_managed_role_policy(role_name,policy_arn):291    sts = boto3.client("sts") 292    logger.info(f"Starting scan of new account {account_num}")293    logger.info(f"account_num: {account_num}")294    role_arn = f"arn:aws:iam::{account_num}:role/KB_assumed_role"295    sts_auth = sts.assume_role(RoleArn=role_arn, RoleSessionName="acquired_account_role")296    credentials = sts_auth["Credentials"]297    298    # ----------------------------- #299    # Place all service code below300    # ----------------------------- #301    # Section for boto3 connection with aws service302    sts_client = boto3.client(aws_service,303                              region_name=target_region,304                              aws_access_key_id=credentials["AccessKeyId"],305                              aws_secret_access_key=credentials["SecretAccessKey"],306                              aws_session_token=credentials["SessionToken"], )    307    308    response= sts_client.detach_role_policy(309    RoleName=role_name,310    PolicyArn=policy_arn311   )...iam_helper.py
Source:iam_helper.py  
...35        print(f"Using ARN from existing role: {role_name}")36        response = iam.get_role(RoleName=role_name)37        return response["Role"]["Arn"]38def delete_lambda_role(role_name):39    response = iam.detach_role_policy(40        RoleName=role_name, PolicyArn="arn:aws:iam::aws:policy/AWSLambda_FullAccess"41    )42    response = iam.detach_role_policy(43        RoleName=role_name,44        PolicyArn="arn:aws:iam::aws:policy/AmazonSageMakerFullAccess",45    )46    response = iam.detach_role_policy(47        RoleName=role_name, PolicyArn="arn:aws:iam::aws:policy/ComprehendFullAccess"48    )49    response = iam.delete_role(RoleName=role_name)...AwsIam.py
Source:AwsIam.py  
...34            RoleName=role_name,35            PolicyArn=policy_arn36        )37        return response38    def detach_role_policy(self, role_name, policy_arn):39        client = self.get_client()40        response = client.detach_role_policy(41            RoleName=role_name,42            PolicyArn=policy_arn43        )44        return response45    def list_role_policies(self, role_name):46        client = self.get_client()47        response = client.list_role_policies(48            RoleName=role_name49        )...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
