

AWS Tools for PowerShell V4 已进入维护模式。

我们建议您迁移到 [AWS Tools for PowerShell V5](https://docs.aws.amazon.com/powershell/v5/userguide/)。有关如何迁移的更多详细信息和信息，请参阅我们的[维护模式公告](https://aws.amazon.com/blogs/developer/aws-tools-for-powershell-v4-maintenance-mode-announcement/)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Windows PowerShell 启动 Amazon EC2 实例
<a name="pstools-ec2-launch"></a>

要启动 Amazon EC2 实例，您需要您在之前的章节中创建的密钥对和安全组。您还需要 Amazon Machine Image (AMI) 的 ID。有关更多信息，请参阅以下文档：
+  [创建密钥对](pstools-ec2-keypairs.md) 
+  [使用 Windows PowerShell 创建安全组](pstools-ec2-sg.md) 
+  [使用 Windows PowerShell 查找 Amazon Machine Image](pstools-ec2-get-amis.md) 

**重要**  
如果您启动不在免费套餐范围内的实例，那么在启动实例后您将需要付费，费用按实例的运行时间计算，即使实例处于闲置状态也需付费。

**Topics**
+ [在 VPC 中启动实例](#new-ec2instance-vpc)
+ [在 VPC 中启动 Spot 实例](#new-ec2instance-spot)

## 在 VPC 中启动实例
<a name="new-ec2instance-vpc"></a>

**警告**  
EC2-Classic 已于 2022 年 8 月 15 日停用。我们建议您从 EC2-Classic 迁移到 VPC。有关更多信息，请参阅博客文章 [EC2-Classic Networking is Retiring - Here's How to Prepare](https://aws.amazon.com/blogs/aws/ec2-classic-is-retiring-heres-how-to-prepare/)。

以下命令在指定的私有子网中创建一个 `m1.small` 实例。安全组必须对指定的子网有效。

```
PS > New-EC2Instance `
    -ImageId ami-c49c0dac `
    -MinCount 1 -MaxCount 1 `
    -KeyName myPSKeyPair `
    -SecurityGroupId sg-5d293231 `
    -InstanceType m1.small `
    -SubnetId subnet-d60013bf

ReservationId   : r-b70a0ef1
OwnerId         : 123456789012
RequesterId     :
Groups          : {}
GroupName       : {}
Instances       : {}
```

最初，您的实例处于 `pending` 状态，但在几分钟后将进入 `running` 状态。要查看有关您的实例的信息，请使用 `Get-EC2Instance` cmdlet。如果您有多个实例，则可以使用 `Filter` 参数按照预留 ID 筛选结果。首先，创建类型为 `Amazon.EC2.Model.Filter` 的对象。接下来，调用使用该过滤器的 `Get-EC2Instance`，然后显示 `Instances` 属性。

```
PS > $reservation = New-Object 'collections.generic.list[string]'
PS > $reservation.add("r-b70a0ef1")
PS > $filter_reservation = New-Object Amazon.EC2.Model.Filter -Property @{Name = "reservation-id"; Values = $reservation}
PS > (Get-EC2Instance -Filter $filter_reservation).Instances

AmiLaunchIndex        : 0
Architecture          : x86_64
BlockDeviceMappings   : {/dev/sda1}
ClientToken           :
EbsOptimized          : False
Hypervisor            : xen
IamInstanceProfile    :
ImageId               : ami-c49c0dac
InstanceId            : i-5203422c
InstanceLifecycle     :
InstanceType          : m1.small
KernelId              :
KeyName               : myPSKeyPair
LaunchTime            : 12/2/2018 3:38:52 PM
Monitoring            : Amazon.EC2.Model.Monitoring
NetworkInterfaces     : {}
Placement             : Amazon.EC2.Model.Placement
Platform              : Windows
PrivateDnsName        :
PrivateIpAddress      : 10.25.1.11
ProductCodes          : {}
PublicDnsName         :
PublicIpAddress       : 198.51.100.245
RamdiskId             :
RootDeviceName        : /dev/sda1
RootDeviceType        : ebs
SecurityGroups        : {myPSSecurityGroup}
SourceDestCheck       : True
SpotInstanceRequestId :
SriovNetSupport       :
State                 : Amazon.EC2.Model.InstanceState
StateReason           :
StateTransitionReason :
SubnetId              : subnet-d60013bf
Tags                  : {}
VirtualizationType    : hvm
VpcId                 : vpc-a01106c2
```

## 在 VPC 中启动 Spot 实例
<a name="new-ec2instance-spot"></a>

以下示例脚本在指定的子网中请求一个 Spot 实例。该安全组必须是您为包含指定子网的 VPC 创建的安全组。

```
$interface1 = New-Object Amazon.EC2.Model.InstanceNetworkInterfaceSpecification
$interface1.DeviceIndex = 0
$interface1.SubnetId = "subnet-b61f49f0"
$interface1.PrivateIpAddress = "10.0.1.5"
$interface1.Groups.Add("sg-5d293231")
Request-EC2SpotInstance `
    -SpotPrice 0.007 `
    -InstanceCount 1 `
    -Type one-time `
    -LaunchSpecification_ImageId ami-7527031c `
    -LaunchSpecification_InstanceType m1.small `
    -Region us-west-2 `
    -LaunchSpecification_NetworkInterfaces $interface1
```