1

Cmdlet:

New-EC2Instance -ImageId ami-abcdefg123 -MinCount 1 -MaxCount 1 -KeyName Keypair `
                -SecurityGroupId sg-abcdefg -InstanceType m1.small ` 
                -SubnetId subnet-01bd1e76

How can I change this to add a private IP address to this instance? Also, I don't want a public IP address assigned to it.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
minisch
  • 323
  • 5
  • 19
  • This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Web Apps Stack Exchange](http://webapps.stackexchange.com/) would be a better place to ask. – jww Mar 27 '15 at 01:25
  • The question's initial formatting may have made it ambiguous that this is on-topic, but the user is asking for assistance with AWS Tools For PowerShell. This is a [software tool commonly used by programmers](http://stackoverflow.com/help/on-topic), and this specific usage would be very unlikely to appear outside of a script. This isn't unlike asking for assistance with a library or SDK in other languages. I've edited the question, which will hopefully draw this distinction out. – Anthony Neace Mar 28 '15 at 01:40

1 Answers1

0

You should always be assigned a private IP address on instance launch. In EC2 classic, this is assigned from the EC2-Classic range every time your instance is started. In a VPC, this address is static (doesn't reset on stop/start) and comes from the address range of your subnet.

You can disable public IP assignment, but be aware of the default settings for assignment of Public IPs:

EC2-Classic: Your instance receives a public IP address. This behavior cannot be changed.

Default Subnet: Your instance receives a public IP address by default, unless you specify otherwise during launch, or you modify the subnet's public IP address attribute.

Nondefault Subnet: Your instance doesn't receive a public IP address by default, unless you specify otherwise during launch, or you modify the subnet's public IP address attribute.

Since you've specified a SubnetId in your New-EC2Instance I can safely assume you're working in a VPC, and since you're asking this question I can assume that the VPC is configured to set the public IP address by default.

In this case, toggling the assignment of a public IP address is as simple as specifying the -AssociatePublicIP parameter of New-EC2Instance.

Example 1: Simply toggle off on EC2 Launch

New-EC2Instance -ImageId ami-abcdefg123 -MinCount 1 -MaxCount 1 -KeyName Keypair `
                -SecurityGroupId sg-abcdefg -InstanceType m1.small ` 
                -SubnetId subnet-1a2b3c4d -AssociatePublicIP $false

Example 2: Disable default public IP on all new instances in your subnet. Use -Force when specifying this in a script to skip interactive confirmation.

Edit-EC2SubnetAttribute -MapPublicIpOnLaunch $false -SubnetId subnet-1a2b3c4d -Force

Documentation:

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • Thanks for detailed explanation. This helps me learning powershell. – minisch Mar 29 '15 at 00:44
  • @minisch No problem! If this resolved your question, you can click the checkmark to the left of this answer to mark it as 'accepted', which will allow other users to see that this has been resolved without checking the comments. – Anthony Neace Mar 29 '15 at 00:53