def connect_to_ec2(region='<STR_LIT>', access_key=None, secret_key=None):
if access_key:<EOL><INDENT>logger.info('<STR_LIT>'.format(region))<EOL>connection = ec2.connect_to_region(<EOL>region,<EOL>aws_access_key_id=access_key,<EOL>aws_secret_access_key=secret_key)<EOL><DEDENT>else:<EOL><INDENT>metadata = get_instance_metadata(timeout=<NUM_LIT:1>, num_retries=<NUM_LIT:1>)<EOL>if metadata:<EOL><INDENT>try:<EOL><INDENT>region = metadata['<STR_LIT>']['<STR_LIT>'][:-<NUM_LIT:1>]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>logger.info('<STR_LIT>'.format(region))<EOL>connection = ec2.connect_to_region(region)<EOL><DEDENT>if not connection:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>return connection<EOL>
Connect to AWS ec2

    :type region: str
    :param region: AWS region to connect to
    :type access_key: str
    :param access_key: AWS access key id
    :type secret_key: str
    :param secret_key: AWS secret access key
    :returns: boto.ec2.connection.EC2Connection -- EC2 connection