def test_get_instance_details_aws_api_exception(ec2_mock, instance_id): # https://github.com/surbas/pg2kinesis/blob/master/tests/test_stream.py#L20 error_response = {"Error": {"Code": "ResourceInUseException"}} ec2_mock.configure_mock( **{"instances.filter.side_effect": ClientError(error_response, "random_ec2_op")} ) with pytest.raises(AWSConnectionError): get_instance_details(instance_id, ec2=ec2_mock)
def test_get_instance_details_aws_api_exception(self): ec2_mock = MagicMock() # https://github.com/surbas/pg2kinesis/blob/master/tests/test_stream.py#L20 error_response = {'Error': {'Code': 'ResourceInUseException'}} ec2_mock.configure_mock(**{'instances.filter.side_effect': ClientError(error_response, 'random_ec2_op')}) with self.assertRaises(AWSConnectionError): get_instance_details(self.instance_id, ec2=ec2_mock)
def ssh( config, instance_name, user=DEFAULT_OS_USER, port=DEFAULT_SSH_PORT, key_type=DEFAULT_KEY_ALGORITHM, key_size=DEFAULT_KEY_SIZE, profile_name=AWS_DEFAULT_PROFILE, region_name=AWS_DEFAULT_REGION, command=None, forwarding=None, ): instance, profile, region = fetch_instance_details_from_config( config, instance_name, profile_name, region_name) ssm = get_aws_client("ssm", region_name=region, profile_name=profile) ec2 = get_aws_resource("ec2", region_name=region, profile_name=profile) ec2_ic = get_aws_client("ec2-instance-connect", region_name=region, profile_name=profile) instance_id = query_instance(name=instance, ec2=ec2) if instance_id is None: raise ValueError( "No instance could be found for name: {}".format(instance)) az = get_instance_details(instance_id=instance_id, ec2=ec2)["availability_zone"] logger.info( "Opening SSH session on instance %s (%s) via profile %s", instance_id, region, profile, ) with SshKey(key_type=key_type, key_size=key_size) as ssh_key: with SshKeyUploader(instance_id=instance_id, az=az, user=user, ssh_key=ssh_key, ec2_ic=ec2_ic): with SshSession( instance_id, region_name=region, profile_name=profile, ssm=ssm, port=port, user=user, command=command, forwarding=forwarding, ) as ssh_session: ssh_session.open()
def test_get_instance_details(instance_id, ec2): expected_details = { "instance_id": "i-0c32153096cd68a6d", "vpc_id": "vpc-1981f29759da4a354", "private_dns_name": "ip-10-69-104-49.eu-west-1.compute.internal", "private_ip_address": "10.69.104.49", "public_dns_name": "ec2-18-201-115-108.eu-west-1.compute.amazonaws.com", "public_ip_address": "18.201.115.108", "availability_zone": "eu-west-1a", "instance_name": "dummy-instance", } details = get_instance_details(instance_id, ec2=ec2) assert details == expected_details
def test_get_instance_details(self): expected_details = { 'instance_id': 'i-0c32153096cd68a6d', 'vpc_id': 'vpc-1981f29759da4a354', 'private_dns_name': 'ip-10-69-104-49.eu-west-1.compute.internal', 'private_ip_address': '10.69.104.49', 'public_dns_name': 'ec2-18-201-115-108.eu-west-1.compute.amazonaws.com', 'public_ip_addess': '18.201.115.108', 'availability_zone': 'eu-west-1a', 'instance_name': 'dummy-instance' } details = get_instance_details(self.instance_id, ec2=self.ec2) self.assertEqual(details, expected_details)
def ssh_proxy(config, instance_name, user=DEFAULT_OS_USER, port=DEFAULT_SSH_PORT, key_type=DEFAULT_KEY_ALGORITHM, key_size=DEFAULT_KEY_SIZE, profile_name=AWS_DEFAULT_PROFILE, region_name=AWS_DEFAULT_REGION): instance, profile, region = fetch_instance_details_from_config(config, instance_name, profile_name, region_name) ssm = get_aws_client('ssm', region_name=region, profile_name=profile) ec2 = get_aws_resource('ec2', region_name=region, profile_name=profile) ec2_ic = get_aws_client('ec2-instance-connect', region_name=region, profile_name=profile) instance_id = query_instance(name=instance, ec2=ec2) if instance_id is None: raise ValueError('No instance could be found for name: {}'.format(instance)) az = get_instance_details(instance_id=instance_id, ec2=ec2)['availability_zone'] logger.info('Opening SSH proxy session on instance %s (%s) via profile %s', instance_id, region, profile) with SshKey(key_type=key_type, key_size=key_size) as ssh_key: with SshKeyUploader(instance_id=instance_id, az=az, user=user, ssh_key=ssh_key, ec2_ic=ec2_ic): with SshProxySession(instance_id, region_name=region, profile_name=profile, ssm=ssm, port=port, user=user) as ssh_proxy_session: ssh_proxy_session.open()
def list_instances( profile_name=AWS_DEFAULT_PROFILE, region_name=AWS_DEFAULT_REGION, output_format=DEFAULT_LIST_OUTPUT, fields=DEFAULT_LIST_HUMAN_FIELDS, ): invalid_fields = list(set(fields) - set(DEFAULT_LIST_OUTPUT_FIELDS)) if invalid_fields: raise ValueError( 'Invalid fields provided: "{}". Valid fields: "{}"'.format( " ".join(invalid_fields), " ".join(DEFAULT_LIST_OUTPUT_FIELDS))) ssm = get_aws_client("ssm", region_name=region_name, profile_name=profile_name) ec2 = get_aws_resource("ec2", region_name=region_name, profile_name=profile_name) instances_ssm_paginator = ssm.get_paginator( "describe_instance_information") instances_ssm_response_iterator = instances_ssm_paginator.paginate() instance_ids = [] for response in instances_ssm_response_iterator: for instance in response["InstanceInformationList"]: instance_ids.append(instance["InstanceId"]) instance_details = [] for instance_id in instance_ids: instance_details.append( get_instance_details(instance_id=instance_id, ec2=ec2)) print( serialize(instance_details, output_format=output_format, fields=fields).rstrip())