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

I am trying to use the following script, but was not 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

Error:

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 the parameter -AssociatePublicIpwithout it, the script is running.

Thanks for reading

+4
source share
3 answers

I ran into the same problem, and a possible workaround when using PowerShell is to first create a network interface and then associate 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

InstanceNetworkInterfaceSpecification has the property to indicate whether the interface needs a public IP address (see documents )

+3
source

AWS PowerShell 2.1.3.0, .

script:

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

, AWS Tools Windows PowerShell. , AWS :

$ 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
  • --count --associate-public-ip-address, , , .. [--associate-public-ip-address | --no-associate-public-ip-address], . run-instances.

( , ) AWS Forum PowerShell, . New-EC2Instance -AssociatePublicIP.

Accordingly, the best choice to solve this problem may be a blow to this topic and hope for a response from the AWS team. In the meantime, you can work around the problem using the operation script through the AWS CLI.

+3
source

Source: https://habr.com/ru/post/1541775/


All Articles