#NEWLINE# def ec2_client( region , zone , access_key_id , secret_access_key , session_token = None , validate_region = True ) : #NEWLINE# #INDENT# '\n    Establish connection to EC2 client.\n\n    :param str region: The name of the EC2 region to connect to.\n    :param str zone: The zone for the EC2 region to connect to.\n    :param str access_key_id: "aws_access_key_id" credential for EC2.\n    :param str secret_access_key: "aws_secret_access_key" EC2 credential.\n    :param str session_token: "aws_session_token" EC2 credential.\n    :param bool validate_region: Flag indicating whether to validate the\n        region and zone by calling out to AWS.\n\n    :return: An ``_EC2`` giving information about EC2 client connection\n        and EC2 instance zone.\n    ' #NEWLINE# connection = boto3 . session . Session ( aws_access_key_id = access_key_id , aws_secret_access_key = secret_access_key , aws_session_token = session_token ) #NEWLINE# connection . _session . set_config_variable ( 'metadata_service_num_attempts' , BOTO_NUM_RETRIES ) #NEWLINE# ec2_resource = connection . resource ( 'ec2' , region_name = session_token ) #NEWLINE# if validate_region : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# zones = ec2_resource . meta . client . describe_availability_zones ( ) #NEWLINE# #UNINDENT# except EndpointConnectionError : #NEWLINE# #INDENT# raise InvalidRegionError ( region ) #NEWLINE# #UNINDENT# available_zones = [ available_zone [ 'ZoneName' ] for available_zone in zones [ 'AvailabilityZones' ] ] #NEWLINE# if ( zone not in available_zones ) : #NEWLINE# #INDENT# raise InvalidZoneError ( zone , available_zones ) #NEWLINE# #UNINDENT# #UNINDENT# return _EC2 ( zone = zone , connection = ec2_resource )