Any AWS Powershell command fails with message "Invalid URI: The hostname could not be parsed."
amazon-ec2, amazon-web-services, aws-powershell, powershell
Solution
This error `Invalid URI: The hostname could not be parsed.` can occur when you specify your region incorrectly. In your case, you appear to be specifying "EU West" incorrectly -- it should be `eu-west-1` instead.
For example, if I use "EU West" as my region for `Get-EC2Instances` in a session that is otherwise correctly configured, I will get the same error:
PS C:\> get-ec2instance -Region "EU West"
get-ec2instance : Invalid URI: The hostname could not be parsed.
At line:1 char:1
+ get-ec2instance -Region "EU West"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...2InstanceCmdlet:GetEC2InstanceCmdlet) [Get-EC2Insta
nce], InvalidOperationException
+ FullyQualifiedErrorId : System.UriFormatException,Amazon.PowerShell.Cmdlets.EC2.GetEC2InstanceCmdlet
To list available AWS regions from Powershell, you can use `Get-AWSRegion`. Here's an example where my shell default is `us-east-1`:
PS C:\> Get-AWSRegion
Region Name IsShellDefault
------ ---- --------------
us-east-1 US East (Virginia) True
us-west-1 US West (N. California) False
us-west-2 US West (Oregon) False
eu-west-1 EU West (Ireland) False
eu-central-1 EU Central (Frankfurt) False
ap-northeast-1 Asia Pacific (Tokyo) False
ap-southeast-1 Asia Pacific (Singapore) False
ap-southeast-2 Asia Pacific (Sydney) False
sa-east-1 South America (Sao Paulo) False
So here we see that EU West is infact a region, but it is specified as `eu-west-1` instead of `EU West`. So just refer to EU West as `eu-west-1` and you should be able to set your defaults.
You can explicitly set which region you want to use in Initialize-AWSDefaults:
Initialize-AWSDefaults -ProfileName MyProfileName -Region eu-west-1
More info on specifying AWS regions can be found in the documentation.
Problem
I am using powershell to create a new EC2 KeyPair using the script below: ``` Import-Module AWSPowerShell $Region = "EU West" $ProfileName = "AWS Default" Initialize-AWSDefaults -ProfileName $ProfileName $KeyName = "SecurityEssentials" $KeyPair = New-EC2KeyPair -KeyName $KeyName ``` However it fails on the last line with the following message: New-EC2KeyPair : Invalid URI: The hostname could not be parsed. At C:\Users\John\Documents\Cloud\AWS Setup.ps1:12 char:12 + $KeyPair = New-EC2KeyPair -KeyName $KeyName + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Amazon.PowerShe...C2KeyPairCmdlet:NewEC2KeyPairCmdlet) [New-EC2KeyPair], InvalidOperationException + FullyQualifiedErrorId : System.UriFormatException,Amazon.PowerShell.Cmdlets.EC2.NewEC2KeyPairCmdlet Using the debug flag does not provide any new information. I am using Windows 8.1 with the latest AWS SDK and AWS powershell cmdlet Edit: The problem appears to be with the way the AWSDefaults are set up. It turns out that any command issued after this gives the same error.