How to launch an EC2 instance into a VPC with a public IP address in PowerShell?
As of AWS PowerShell version 2.1.3.0, this bug has been corrected.
I was able to execute this script:
New-EC2Instance -ImageId $Ami[0].ImageId -MinCount 1 -MaxCount 1 -KeyName uckey -InstanceType `t1.micro -SubnetId subnet-56738b33 -AssociatePublicIp $true
I ran into the same problem, and a possible workaround while still using PowerShell is to create the network interface first, and then associating it with the instance:
$subnetId = "subnet-56738b33"$keyName = "uckey"$instanceType = "t1.micro"$Ami = Get-EC2ImageByName WINDOWS_2012_BASE$ImageId = $Ami[0].ImageId$networkInterface = New-EC2NetworkInterface -SubnetId $subnetId -Description "Primary network interface"$interfaceSpec = New-Object Amazon.EC2.Model.InstanceNetworkInterfaceSpecification -property @{"NetworkInterfaceId"=$networkInterface.NetworkInterfaceId}$reservation = New-EC2Instance -ImageId $ImageId -MinCount 1 -MaxCount 1 -InstanceType $instanceType -KeyName $keyName -NetworkInterfaces $interfaceSpec
The InstanceNetworkInterfaceSpecification has a property to indicate if the interface needs a public IP address (see the docs)
I suspect this to be a bug in the AWS Tools for Windows PowerShell. As already commented, running the semantically identical command with the AWS Command Line Interface instead yields the desired result:
$ aws ec2 run-instances --image-id $ami.Imageid --count 1:1 --instance-type t1.micro ` --key-name uckey --subnet-id subnet-56738b33 --associate-public-ip-address
- Beware of the slight syntax difference for
--count
and--associate-public-ip-address
, the latter doesn't require a value, rather comprises the flag in itself, i.e.[--associate-public-ip-address | --no-associate-public-ip-address]
, see run-instances.
This is also confirmed by an (unfortunately unanswered) inquiry in the AWS Forum for PowerShell scripting, see Unable to get New-EC2Instance to honour -AssociatePublicIP.
Accordingly, your best bet to get this resolved might be to bump that thread and hope for a response from the AWS team. Meanwhile you can work around the issue by means of scripting the operation via the AWS CLI instead.