

# AWS CLI の Amazon EC2 セキュリティグループの作成、設定、および削除
<a name="cli-services-ec2-sg"></a>

本質的にファイアウォールとして動作する Amazon Elastic Compute Cloud (Amazon EC2) インスタンス用のセキュリティグループを作成し、入退出が可能なネットワークトラフィックを決めるルールと共に作成することができます。

AWS Command Line Interface (AWS CLI) を使用して新しいセキュリティグループを作成し、既存のセキュリティグループにルールを追加して、セキュリティグループを削除します。

**注記**  
その他のコマンドの例については、「[AWS CLI リファレンスガイド](https://docs.aws.amazon.com/cli/latest/reference/index.html)」を参照してください。

**Topics**
+ [前提条件](#cli-services-ec2-sg-prereqs)
+ [セキュリティグループの作成](#creating-a-security-group)
+ [ルールをセキュリティグループに追加する](#configuring-a-security-group)
+ [セキュリティグループを削除する](#deleting-a-security-group)
+ [リファレンス](#cli-services-ec2-sg-references)

## 前提条件
<a name="cli-services-ec2-sg-prereqs"></a>

`ec2` コマンドを実行するには、次のことが必要です。
+ AWS CLI をインストールして設定します。詳細については、「[AWS CLI の最新バージョンのインストールまたは更新](getting-started-install.md)」および「」を参照してください。[Authentication and access credentials for the AWS CLI](cli-chap-authentication.md)
+ Amazon EC2 アクセスできるように IAM アクセス許可を設定します。Amazon EC2 に対する IAM アクセス許可の詳細については、「*Amazon EC2 ユーザーガイド*」の「[Amazon EC2 の IAM ポリシー](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-for-amazon-ec2.html)」を参照してください。

## セキュリティグループの作成
<a name="creating-a-security-group"></a>

仮想プライベートクラウド (VPC) に関連するセキュリティグループを作成できます。

次の `[aws ec2 create-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-security-group.html)` の例は、指定された VPC のセキュリティグループを作成する方法を示しています。

```
$ aws ec2 create-security-group --group-name my-sg --description "My security group" --vpc-id vpc-1a2b3c4d
{
    "GroupId": "sg-903004f8"
}
```

セキュリティグループの初期情報を表示するには、`[aws ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)` コマンドを実行します。EC2-VPC セキュリティグループは、名前ではなく `vpc-id` によってのみ参照することができます。

```
$ aws ec2 describe-security-groups --group-ids sg-903004f8
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": []
                }
            ],
            "Description": "My security group"
            "IpPermissions": [],
            "GroupName": "my-sg",
            "VpcId": "vpc-1a2b3c4d",
            "OwnerId": "123456789012",
            "GroupId": "sg-903004f8"
        }
    ]
}
```

## ルールをセキュリティグループに追加する
<a name="configuring-a-security-group"></a>

Amazon EC2 インスタンスを実行するときには、セキュリティグループのルールを有効にして、イメージに接続する手段としての着信ネットワークトラフィックを有効にする必要があります。

例えば、Windows インスタンスを起動する場合、Remote Desktop Protocol (RDP) をサポートするには、一般に TCP ポート 3389 へのインバウンドトラフィックを許可するルールを追加します。Linux インスタンスを起動する場合、SSH 接続をサポートするには、一般に TCP ポート 22 へのインバウンドトラフィックを許可するルールを追加します。

セキュリティグループにルールを追加するには、`[aws ec2 authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html)` コマンドを使用します。このコマンドの必須パラメータは、コンピュータのパブリック IP アドレス、またはコンピュータが接続しているネットワーク (アドレス範囲の形式で) の [CIDR](https://wikipedia.org/wiki/Classless_Inter-Domain_Routing) 表記です。

**注記**  
当社では、以下のサービス[ https://checkip.global.api.aws/ ](https://checkip.global.api.aws/)を提供し、お客様のパブリック IP アドレスを特定できます。IP アドレスの識別に役立つその他のサービスを見つけるには、ブラウザを使用して「*what is my IP address*」を検索します。ISP 経由、またはファイアウォールの内側から動的な IP アドレスを使用して接続している場合 (プライベートネットワークの NAT ゲートウェイ経由)、アドレスは定期的に変更される場合があります。その場合、クライアントコンピュータによって使用される IP アドレスの範囲を見つける必要があります。

次の例は、IP アドレスを使用して ID `sg-903004f8` を持つ EC2-VPC セキュリティグループに RDP (TCP ポート 3389) のルールを追加する方法を示しています。

まず、あなたの IP アドレスを探してください。

```
$ curl https://checkip.amazonaws.com
x.x.x.x
```

その後、`[aws ec2 authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html)` コマンドを実行して、セキュリティグループに IP アドレスを追加できます。

```
$ aws ec2 authorize-security-group-ingress --group-id sg-903004f8 --protocol tcp --port 3389 --cidr x.x.x.x/x
```

次のコマンドは、同じセキュリティグループのインスタンスに SSH を有効にする別のルールを追加します。

```
$ aws ec2 authorize-security-group-ingress --group-id sg-903004f8 --protocol tcp --port 22 --cidr x.x.x.x/x
```

セキュリティグループに加えられた変更を表示するには、`[aws ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)` コマンドを実行します。

```
$ aws ec2 describe-security-groups --group-ids sg-903004f8
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": []
                }
            ],
            "Description": "My security group"
            "IpPermissions": [
                {
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "IpRanges": [
                        {
                            "CidrIp": "x.x.x.x/x"
                        }
                    ]
                    "UserIdGroupPairs": [],
                    "FromPort": 22
                }
            ],
            "GroupName": "my-sg",
            "OwnerId": "123456789012",
            "GroupId": "sg-903004f8"
        }
    ]
}
```

## セキュリティグループを削除する
<a name="deleting-a-security-group"></a>

セキュリティグループを削除するには、`[aws ec2 delete-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-security-group.html)` コマンドを実行します。

**注記**  
環境に現在アタッチされているセキュリティグループは削除できません。

次のコマンド例は、EC2-VPC セキュリティグループを削除します。

```
$ aws ec2 delete-security-group --group-id sg-903004f8
```

## リファレンス
<a name="cli-services-ec2-sg-references"></a>

**AWS CLI リファレンス:**
+ `[aws ec2](https://docs.aws.amazon.com/cli/latest/reference/ec2/index.html)`
+ `[aws ec2 authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html)`
+ `[aws ec2 create-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-security-group.html)`
+ `[aws ec2 delete-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-security-group.html)`
+ `[aws ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)`

**その他のリファレンス:**
+ [Amazon Elastic Compute Cloud のドキュメント](https://docs.aws.amazon.com/ec2/)
+ AWS SDK および AWS CLI のコード例を表示したり、提供したりするには、*GitHub* の [AWS コード例のリポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/)を参照してください。