An implementation of the encoding required to suport AWS's domain name
    rules defined here__:

    .. __: http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/DomainNameFormat.html

    While AWS's documentation specifies individual ASCII characters which need
    to be encoded, we instead just try to force the string to one of
    escaped unicode or idna depending on whether there are non-ASCII characters
    present.

    This means that we support things like ドメイン.テスト as a domain name string.

    More information about IDNA encoding in python is found here__:

    .. __: https://pypi.org/project/idna

def aws_encode(x):
    '''
    An implementation of the encoding required to suport AWS's domain name
    rules defined here__:

    .. __: http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/DomainNameFormat.html

    While AWS's documentation specifies individual ASCII characters which need
    to be encoded, we instead just try to force the string to one of
    escaped unicode or idna depending on whether there are non-ASCII characters
    present.

    This means that we support things like ドメイン.テスト as a domain name string.

    More information about IDNA encoding in python is found here__:

    .. __: https://pypi.org/project/idna

    '''
    ret = None
    try:
        x.encode('ascii')
        ret = re.sub(r'\\x([a-f0-8]{2})',
                      _hexReplace, x.encode('unicode_escape'))
    except UnicodeEncodeError:
        ret = x.encode('idna')
    except Exception as e:
        log.error("Couldn't encode %s using either 'unicode_escape' or 'idna' codecs", x)
        raise CommandExecutionError(e)
    log.debug('AWS-encoded result for %s: %s', x, ret)
    return ret