Best Python code snippet using localstack_python
test_iam_integration.py
Source:test_iam_integration.py
...27 instance_id = quick_instance_creation()28 instance_profile_arn, instance_profile_name = quick_instance_profile_creation(29 "test_profile"30 )31 association = client.associate_iam_instance_profile(32 IamInstanceProfile={33 "Arn": instance_profile_arn,34 "Name": instance_profile_name,35 },36 InstanceId=instance_id,37 )38 association["IamInstanceProfileAssociation"]["InstanceId"].should.equal(instance_id)39 association["IamInstanceProfileAssociation"]["IamInstanceProfile"][40 "Arn"41 ].should.equal(instance_profile_arn)42 association["IamInstanceProfileAssociation"]["State"].should.equal("associating")43@mock_ec244@mock_iam45def test_invalid_associate():46 client = boto3.client("ec2", region_name="us-east-1")47 instance_id = quick_instance_creation()48 instance_profile_arn, instance_profile_name = quick_instance_profile_creation(49 "test_profile"50 )51 client.associate_iam_instance_profile(52 IamInstanceProfile={53 "Arn": instance_profile_arn,54 "Name": instance_profile_name,55 },56 InstanceId=instance_id,57 )58 # Duplicate59 with pytest.raises(ClientError) as ex:60 client.associate_iam_instance_profile(61 IamInstanceProfile={62 "Arn": instance_profile_arn,63 "Name": instance_profile_name,64 },65 InstanceId=instance_id,66 )67 ex.value.response["Error"]["Code"].should.equal("IncorrectState")68 ex.value.response["Error"]["Message"].should.contain(69 "There is an existing association for"70 )71 # Wrong instance profile72 with pytest.raises(ClientError) as ex:73 client.associate_iam_instance_profile(74 IamInstanceProfile={"Arn": "fake", "Name": "fake"}, InstanceId=instance_id,75 )76 ex.value.response["Error"]["Code"].should.equal("NoSuchEntity")77 ex.value.response["Error"]["Message"].should.contain("not found")78 # Wrong instance id79 with pytest.raises(ClientError) as ex:80 client.associate_iam_instance_profile(81 IamInstanceProfile={82 "Arn": instance_profile_arn,83 "Name": instance_profile_name,84 },85 InstanceId="fake",86 )87 ex.value.response["Error"]["Code"].should.equal("InvalidInstanceID.NotFound")88 ex.value.response["Error"]["Message"].should.contain("does not exist")89@mock_ec290@mock_iam91def test_describe():92 client = boto3.client("ec2", region_name="us-east-1")93 instance_id = quick_instance_creation()94 instance_profile_arn, instance_profile_name = quick_instance_profile_creation(95 "test_profile"96 )97 client.associate_iam_instance_profile(98 IamInstanceProfile={99 "Arn": instance_profile_arn,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")...
aws_assign_instance_profile.py
Source:aws_assign_instance_profile.py
...46 instance_id = _get_local_instance_id()47 ec2_client = boto3.client("ec2", 'us-east-1')48 #https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.associate_iam_instance_profile49 try:50 response = ec2_client.associate_iam_instance_profile(51 IamInstanceProfile={52 'Arn' : iam_instance_arn,53 },54 InstanceId = instance_id)55 print(response)56 # Wait for the instance profile to be assigned by polling the local instance metadata service57 _wait_instance_profile()58 except botocore.exceptions.ClientError as ce:59 if ce.response["Error"]["Code"] == "RequestLimitExceeded":60 print("WARNING: RequestLimitExceeded, exiting with error code 2")61 sys.exit(2)62 raise63def main() -> None:64 """Execute Main entry point."""...
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!!