Posts Tagged ‘sdk’

Amazon PHP SDK v1.4.7 set_region bug for EC2: DNS names not properly resolved

Saturday, December 3rd, 2011

Version 1.4.7 of the Amazon PHP SDK added the new Oregon region (along with the United States government private GovCloud region) but introduced a bug rendering the whole library useless as it cannot resolve the correct DNS names.

Prior to v1.4.7, the AmazonEC2 library used these constants (in /usr/share/php/AWSSDKforPHP/services/ec2.class.php):

    /*%******************************************************************************************%*/
    // CLASS CONSTANTS

    /**
     * Specify the default queue URL.
     */
    const DEFAULT_URL = 'ec2.amazonaws.com';

    /**
     * Specify the queue URL for the US-East (Northern Virginia) Region.
     */
    const REGION_US_E1 = 'us-east-1';

    /**
     * Specify the queue URL for the US-West (Northern California) Region.
     */
    const REGION_US_W1 = 'us-west-1';

    /**
     * Specify the queue URL for the EU (Ireland) Region.
     */
    const REGION_EU_W1 = 'eu-west-1';

    /**
     * Specify the queue URL for the Asia Pacific (Singapore) Region.
     */
    const REGION_APAC_SE1 = 'ap-southeast-1';

    /**
     * Specify the queue URL for the Asia Pacific (Japan) Region.
     */
    const REGION_APAC_NE1 = 'ap-northeast-1';

And the code used to reconstruct the DNS entry was

    /**
     * This allows you to explicitly sets the region for the service to use.
     *
     * @param string $region (Required) The region to explicitly set. Available options are <REGION_US_E1>, <REGION_US_W1>, <REGION_EU_W1>, or <REGION_APAC_SE1>.
     * @return $this A reference to the current instance.
     */
    public function set_region($region)
    {
        $this->set_hostname('http://ec2.'. $region .'.amazonaws.com');
        return $this;
    }

With the upgrade to v1.4.7, those constants were changed to a FQDN, while the set_region() function was not changed at all:

    /*%******************************************************************************************%*/
    // CLASS CONSTANTS

    /**
     * Specify the queue URL for the United States East (Northern Virginia) Region.
     */
    const REGION_US_E1 = 'ec2.us-east-1.amazonaws.com';

    /**
     * Specify the queue URL for the United States West (Northern California) Region.
     */
    const REGION_US_W1 = 'ec2.us-west-1.amazonaws.com';

    /**
     * Specify the queue URL for the United States West (Oregon) Region.
     */
    const REGION_US_W2 = 'ec2.us-west-2.amazonaws.com';

    /**
     * Specify the queue URL for the Europe West (Ireland) Region.
     */
    const REGION_EU_W1 = 'ec2.eu-west-1.amazonaws.com';

    /**
     * Specify the queue URL for the Asia Pacific Southeast (Singapore) Region.
     */
    const REGION_APAC_SE1 = 'ec2.ap-southeast-1.amazonaws.com';

    /**
     * Specify the queue URL for the Asia Pacific Northeast (Tokyo) Region.
     */
    const REGION_APAC_NE1 = 'ec2.ap-northeast-1.amazonaws.com';

    /**
     * Specify the queue URL for the United States GovCloud Region.
     */
    const REGION_US_GOV1 = 'ec2.us-gov-west-1.amazonaws.com';

So, for instance, if you were using REGION_US_W1 before,
the old SDK would resolve the url to ‘http://ec2.us-west-1.amazonaws.com’ using set_region()

As of v1.4.7, REGION_US_W1 was changed to
‘ec2.us-west-1.amazonaws.com’
so now set_region() builds the url as the non very pretty
‘http://ec2.ec2.us-west-1.amazonaws.com.amazonaws.com’

and triggers this PHP fatal error

PHP Fatal error:  Uncaught exception 'RequestCore_Exception' with message 'cURL resource: Resource id #62; cURL error: Couldn't resolve host 'ec2.ec2.us-west-1.amazonaws.com.amazonaws.com' (6)' in /usr/share/php/AWSSDKforPHP/lib/requestcore/requestcore.class.php:824
Stack trace:
#0/usr/share/php/AWSSDKforPHP/sdk.class.php(1185): RequestCore->send_request()

To fix the error, modify set_region to:

    public function set_region($region)
    {
        //$this->set_hostname('http://ec2.'. $region .'.amazonaws.com');
        $this->set_hostname('http://'.$region);
        return $this;
    }

——————————————————————
Update 12/07/2011:
Version 1.4.8 of the PHP SDK corrects the bug.