简体   繁体   中英

How to launch an EC2 instance into a VPC with a public IP address in PowerShell?

I have been trying to use the following script, but haven't been successful:

$Ami=Get-EC2ImageByName WINDOWS_2012_BASE

New-EC2Instance -ImageId $Ami[0].ImageId -MinCount 1 -MaxCount 1 -KeyName uckey -InstanceType `
t1.micro -SubnetId subnet-56738b33 -AssociatePublicIp $true

The error is:

New-EC2Instance : Object reference not set to an instance of an object.
At line:1 char:1
+ New-EC2Instance -ImageId $Ami[0].ImageId -MinCount 1 -MaxCount 1 -KeyName uckey  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...2InstanceCmdlet:NewEC2InstanceCmdlet)  
   [New-EC2Instance], InvalidOperationException
    + FullyQualifiedErrorId : System.NullReferenceException,Amazon.PowerShell.Cmdlets.EC2.NewEC2InstanceC 
   mdlet

The problem is about the parameter -AssociatePublicIp without it, the script works.

Thanks for reading

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 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, ie [--associate-public-ip-address | --no-associate-public-ip-address] [--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.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM