Best Python code snippet using localstack_python
test_iam_integration.py
Source:test_iam_integration.py  
...100            "Name": instance_profile_name,101        },102        InstanceId=instance_id,103    )104    associations = client.describe_iam_instance_profile_associations()105    associations["IamInstanceProfileAssociations"].should.have.length_of(1)106    associations["IamInstanceProfileAssociations"][0]["InstanceId"].should.equal(107        instance_id108    )109    associations["IamInstanceProfileAssociations"][0]["IamInstanceProfile"][110        "Arn"111    ].should.equal(instance_profile_arn)112    associations["IamInstanceProfileAssociations"][0]["State"].should.equal(113        "associated"114    )115    instance_id = quick_instance_creation()116    instance_profile_arn, instance_profile_name = quick_instance_profile_creation(117        "test_profile1"118    )119    client.associate_iam_instance_profile(120        IamInstanceProfile={121            "Arn": instance_profile_arn,122            "Name": instance_profile_name,123        },124        InstanceId=instance_id,125    )126    next_test_associations = client.describe_iam_instance_profile_associations()127    next_test_associations["IamInstanceProfileAssociations"].should.have.length_of(2)128    associations = client.describe_iam_instance_profile_associations(129        AssociationIds=[130            next_test_associations["IamInstanceProfileAssociations"][0][131                "AssociationId"132            ],133        ]134    )135    associations["IamInstanceProfileAssociations"].should.have.length_of(1)136    associations["IamInstanceProfileAssociations"][0]["IamInstanceProfile"][137        "Arn"138    ].should.equal(139        next_test_associations["IamInstanceProfileAssociations"][0][140            "IamInstanceProfile"141        ]["Arn"]142    )143    associations = client.describe_iam_instance_profile_associations(144        Filters=[145            {146                "Name": "instance-id",147                "Values": [148                    next_test_associations["IamInstanceProfileAssociations"][0][149                        "InstanceId"150                    ],151                ],152            },153            {"Name": "state", "Values": ["associated"]},154        ]155    )156    associations["IamInstanceProfileAssociations"].should.have.length_of(1)157    associations["IamInstanceProfileAssociations"][0]["IamInstanceProfile"][158        "Arn"159    ].should.equal(160        next_test_associations["IamInstanceProfileAssociations"][0][161            "IamInstanceProfile"162        ]["Arn"]163    )164@mock_ec2165@mock_iam166def test_replace():167    client = boto3.client("ec2", region_name="us-east-1")168    instance_id1 = quick_instance_creation()169    instance_profile_arn1, instance_profile_name1 = quick_instance_profile_creation(170        "test_profile1"171    )172    instance_profile_arn2, instance_profile_name2 = quick_instance_profile_creation(173        "test_profile2"174    )175    association = client.associate_iam_instance_profile(176        IamInstanceProfile={177            "Arn": instance_profile_arn1,178            "Name": instance_profile_name1,179        },180        InstanceId=instance_id1,181    )182    association = client.replace_iam_instance_profile_association(183        IamInstanceProfile={184            "Arn": instance_profile_arn2,185            "Name": instance_profile_name2,186        },187        AssociationId=association["IamInstanceProfileAssociation"]["AssociationId"],188    )189    association["IamInstanceProfileAssociation"]["IamInstanceProfile"][190        "Arn"191    ].should.equal(instance_profile_arn2)192    association["IamInstanceProfileAssociation"]["State"].should.equal("associating")193@mock_ec2194@mock_iam195def test_invalid_replace():196    client = boto3.client("ec2", region_name="us-east-1")197    instance_id = quick_instance_creation()198    instance_profile_arn, instance_profile_name = quick_instance_profile_creation(199        "test_profile"200    )201    instance_profile_arn2, instance_profile_name2 = quick_instance_profile_creation(202        "test_profile2"203    )204    association = client.associate_iam_instance_profile(205        IamInstanceProfile={206            "Arn": instance_profile_arn,207            "Name": instance_profile_name,208        },209        InstanceId=instance_id,210    )211    # Wrong id212    with pytest.raises(ClientError) as ex:213        client.replace_iam_instance_profile_association(214            IamInstanceProfile={215                "Arn": instance_profile_arn2,216                "Name": instance_profile_name2,217            },218            AssociationId="fake",219        )220    ex.value.response["Error"]["Code"].should.equal("InvalidAssociationID.NotFound")221    ex.value.response["Error"]["Message"].should.contain("An invalid association-id of")222    # Wrong instance profile223    with pytest.raises(ClientError) as ex:224        client.replace_iam_instance_profile_association(225            IamInstanceProfile={"Arn": "fake", "Name": "fake",},226            AssociationId=association["IamInstanceProfileAssociation"]["AssociationId"],227        )228    ex.value.response["Error"]["Code"].should.equal("NoSuchEntity")229    ex.value.response["Error"]["Message"].should.contain("not found")230@mock_ec2231@mock_iam232def test_disassociate():233    client = boto3.client("ec2", region_name="us-east-1")234    instance_id = quick_instance_creation()235    instance_profile_arn, instance_profile_name = quick_instance_profile_creation(236        "test_profile"237    )238    association = client.associate_iam_instance_profile(239        IamInstanceProfile={240            "Arn": instance_profile_arn,241            "Name": instance_profile_name,242        },243        InstanceId=instance_id,244    )245    associations = client.describe_iam_instance_profile_associations()246    associations["IamInstanceProfileAssociations"].should.have.length_of(1)247    disassociation = client.disassociate_iam_instance_profile(248        AssociationId=association["IamInstanceProfileAssociation"]["AssociationId"],249    )250    disassociation["IamInstanceProfileAssociation"]["IamInstanceProfile"][251        "Arn"252    ].should.equal(instance_profile_arn)253    disassociation["IamInstanceProfileAssociation"]["State"].should.equal(254        "disassociating"255    )256    associations = client.describe_iam_instance_profile_associations()257    associations["IamInstanceProfileAssociations"].should.have.length_of(0)258@mock_ec2259@mock_iam260def test_invalid_disassociate():261    client = boto3.client("ec2", region_name="us-east-1")262    # Wrong id263    with pytest.raises(ClientError) as ex:264        client.disassociate_iam_instance_profile(AssociationId="fake",)265    ex.value.response["Error"]["Code"].should.equal("InvalidAssociationID.NotFound")...inventory-instances-sg.py
Source:inventory-instances-sg.py  
...93        resource_item['errors']                         = {}94        save_resource_to_s3(SG_RESOURCE_PATH, resource_item['resourceId'], resource_item)95def get_instance_profiles(ec2_client):96    assoc = []97    response = ec2_client.describe_iam_instance_profile_associations()98    while 'NextToken' in response:99        assoc += response['IamInstanceProfileAssociations']100        response = ec2_client.describe_iam_instance_profile_associations(NextToken=response['NextToken'])101    assoc += response['IamInstanceProfileAssociations']102    output = {}103    for a in assoc:104        output[a['InstanceId']] = a105    return(output)106def get_all_instances(ec2_client):107    output = []108    response = ec2_client.describe_instances()109    while 'NextToken' in response:110        output += response['Reservations']111        response = ec2_client.describe_instances(NextToken=response['NextToken'])112    output += response['Reservations']113    return(output)114def get_all_securitygroups(ec2_client):...describe_iam_instance_profile_associations.py
Source:describe_iam_instance_profile_associations.py  
1import boto32client = boto3.client('ec2', region_name='us-west-2')3response = client.describe_iam_instance_profile_associations(4	Filters=[5		{6			'Name': 'instance-id',7			'Values': [8				'i-0ae3f63791af9e181',9				'i-02e0f7f9046df500d',10				'i-0b1c7e5a2cd51d518',11				'i-018d27468e5bd8cd0',12				'i-0d8deed45eb72c59d',13				'i-0bd92ca0b8fe0c577',14				'i-0c525be5cdef298c4',15				'i-03eaaed9a0f11a20e',16				'i-032f67f88d605953e',17				'i-0ba503eca0c009f5b',...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!!
