

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 创建安全组
<a name="pstools-ec2-sg"></a>

您可以使用AWS Tools for PowerShell来创建和配置安全组。该操作以安全组 ID 作为响应。

如果您需要连接到您的实例，就必须配置安全组以允许 SSH 流量 (Linux) 或 RDP 流量 (Windows)。

**Topics**
+ [先决条件](#sg-prerequisites)
+ [为 EC2-VPC 创建安全组](#new-ec2securitygroup-vpc)

## 先决条件
<a name="sg-prerequisites"></a>

您需要您的计算机的公有 IP 地址，该地址使用 CIDR 表示法。您可以通过一项服务来获取本地计算机的公有 IP 地址。例如，Amazon 提供以下服务：[http://checkip.amazonaws.com/](http://checkip.amazonaws.com/) 或 [https://checkip.amazonaws.com/](https://checkip.amazonaws.com/)。要查找另一项可提供您的 IP 地址的服务，请使用搜索短语“what is my IP address”。如果您正通过 ISP 或从防火墙后面连接，没有静态 IP 地址，您需要找出客户端计算机可以使用的 IP 地址范围。

**警告**  
如果您指定 `0.0.0.0/0`，则会启用来自世界上任何 IP 地址的流量。对于 SSH 和 RDP 协议，您可能考虑这在测试环境下短时间内是可以接受的，但它对于生产环境并不安全。对于生产环境，请确保仅授权从适当的单个 IP 地址或地址范围进行访问。

## 为 EC2-VPC 创建安全组
<a name="new-ec2securitygroup-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/)。

以下 `New-EC2SecurityGroup` 示例将添加 `-VpcId` 参数为指定的 VPC 创建安全组。

```
PS > $groupid = New-EC2SecurityGroup `
    -VpcId "vpc-da0013b3" `
    -GroupName "myPSSecurityGroup" `
    -GroupDescription "EC2-VPC from PowerShell"
```

要查看安全组的初始配置，请使用 `Get-EC2SecurityGroup` cmdlet。默认情况下，VPC 的安全组中包含允许所有出站流量的规则。请注意，您不能通过名称引用 EC2-VPC 的安全组。

```
PS > Get-EC2SecurityGroup -GroupId sg-5d293231

OwnerId             : 123456789012
GroupName           : myPSSecurityGroup
GroupId             : sg-5d293231
Description         : EC2-VPC from PowerShell
IpPermissions       : {}
IpPermissionsEgress : {Amazon.EC2.Model.IpPermission}
VpcId               : vpc-da0013b3
Tags                : {}
```

要为 TCP 端口 22 (SSH) 和 TCP 端口 3389 上的入站流量定义权限，请使用 `New-Object` cmdlet。以下示例脚本为从来自单个 IP 地址 `203.0.113.25/32` 的 TCP 端口 22 和 3389 定义权限。

```
$ip1 = new-object Amazon.EC2.Model.IpPermission 
$ip1.IpProtocol = "tcp" 
$ip1.FromPort = 22 
$ip1.ToPort = 22 
$ip1.IpRanges.Add("203.0.113.25/32") 
$ip2 = new-object Amazon.EC2.Model.IpPermission 
$ip2.IpProtocol = "tcp" 
$ip2.FromPort = 3389 
$ip2.ToPort = 3389 
$ip2.IpRanges.Add("203.0.113.25/32") 
Grant-EC2SecurityGroupIngress -GroupId $groupid -IpPermissions @( $ip1, $ip2 )
```

要验证已更新安全组，请再次使用 `Get-EC2SecurityGroup` cmdlet。

```
PS > Get-EC2SecurityGroup -GroupIds sg-5d293231

OwnerId             : 123456789012
GroupName           : myPSSecurityGroup
GroupId             : sg-5d293231
Description         : EC2-VPC from PowerShell
IpPermissions       : {Amazon.EC2.Model.IpPermission}
IpPermissionsEgress : {Amazon.EC2.Model.IpPermission}
VpcId               : vpc-da0013b3
Tags                : {}
```

要查看入站规则，您可以从上一个命令返回的集合对象中检索 `IpPermissions` 属性。

```
PS > (Get-EC2SecurityGroup -GroupIds sg-5d293231).IpPermissions

IpProtocol       : tcp
FromPort         : 22
ToPort           : 22
UserIdGroupPairs : {}
IpRanges         : {203.0.113.25/32}

IpProtocol       : tcp
FromPort         : 3389
ToPort           : 3389
UserIdGroupPairs : {}
IpRanges         : {203.0.113.25/32}
```