简体   繁体   中英

Can I use AWS Tools for Powershell to create a new EC2 instance and set the private IP?

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.

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:

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