[JAWS-UG CLI] VPC:#5 セキュリティグループの作成 (SMTP許可)

ELBの動作検証用に、SMTPを許可するセキュリティグループを作成してみます。

前提条件
========


VPCへの権限
-----------

* EC2に対してフル権限があること。


デフォルトVPCの存在
-------------------

* デフォルトVPCが存在すること。

```bash:コマンド:
VPC_ID=$( \
        aws ec2 describe-vpcs \
          --filters Name=isDefault,Values=true \
          --query 'Vpcs[].VpcId' \
          --output text \
) \
        && echo ${VPC_ID}
```

```text:結果(例):
      vpc-xxxxxxxx
```

AWS CLIのバージョン
-------------------

* 以下のバージョンで動作確認済

  * AWS CLI 1.7.45

```bash:コマンド:
aws --version
```

```text:結果(例):
aws-cli/1.7.45 Python/2.7.5 Darwin/13.4.0
```


0. 準備
=======


0.1. リージョンの決定
---------------------

```bash:変数の設定:
export AWS_DEFAULT_REGION='ap-northeast-1'
```


0.2. 変数の確認
---------------

```bash:変数の確認:
aws configure list
```

```text:結果(例):
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile      ec2Full-prjZ-mbp13               env    AWS_DEFAULT_PROFILE
      access_key     ****************LOAQ shared-credentials-file
      secret_key     ****************I1O1 shared-credentials-file
          region           ap-northeast-1              env    AWS_DEFAULT_REGION
```


1. 事前作業
===========


1.1. VPC Security Groupの名称と説明の決定
-----------------------------------------

```bash:変数の設定:
VPC_SG_NAME='ec2-smtp-global-inbound'
VPC_SG_DESC='EC2 SMTP Global inbound'
```


1.2. VPC Security Groupの不存在確認
-----------------------------------

```bash:コマンド:
aws ec2 describe-security-groups \
  --group-names ${VPC_SG_NAME}
```

```text:結果(例):
      A client error (InvalidGroup.NotFound) occurred when calling the DescribeSecurityGroups operation: The security group 'ec2-smtp-global-inbound' does not exist in default VPC 'vpc-xxxxxxxx'
```


2. VPC Security Groupの作成
===========================


2.1. VPC Security Groupの作成
-----------------------------

```bash:変数の確認:
cat << ETX

        VPC_SG_NAME: ${VPC_SG_NAME}
        VPC_SG_DESC: "${VPC_SG_DESC}"

ETX
```

```bash:コマンド:
aws ec2 create-security-group \
  --group-name ${VPC_SG_NAME} \
  --description "${VPC_SG_DESC}"
```

```json:結果(例):
      {
          "GroupId": "sg-xxxxxxxx"
      }
```


2.2. VPC Security Groupの存在確認
---------------------------------

```bash:コマンド:
aws ec2 describe-security-groups \
  --group-names ${VPC_SG_NAME}
```

```json:結果(例):
      {
        "SecurityGroups": [
          {
              "IpPermissionsEgress": [
                  {
                      "IpProtocol": "-1",
                      "IpRanges": [
                          {
                              "CidrIp": "0.0.0.0/0"
                          }
                      ],
                      "UserIdGroupPairs": [],
                      "PrefixListIds": []
                  }
              ],
              "Description": "EC2 SMTP Global inbound",
              "IpPermissions": [],
              "GroupName": "EC2 SMTP Global inbound",
              "VpcId": "vpc-xxxxxxxx",
              "OwnerId": "XXXXXXXXXXXX",
              "GroupId": "sg-xxxxxxxx"
          }
        ]
      }
```


2.3. VPC Security Group IDの取得
--------------------------------

```bash:コマンド:
VPC_SG_ID=$( \
  aws ec2 describe-security-groups \
    --filter Name=group-name,Values=${VPC_SG_NAME} \
    --query 'SecurityGroups[].GroupId' \
    --output text \
) \
  && echo ${VPC_SG_ID}
```

```text:結果(例):
sg-xxxxxxxx
```


3. アクセス制御の設定
=====================

VPC Security Groupの設定追加 (SMTP)
-------------------------------------------

```bash:変数の設定:
VPC_SG_PROTOCOL='tcp'
VPC_SG_PORT='25'
VPC_SG_CIDR='0.0.0.0/0'
```

```bash:変数の確認:
cat << ETX

        VPC_SG_PROTOCOL: ${VPC_SG_PROTOCOL}
        VPC_SG_PORT:     ${VPC_SG_PORT}
        VPC_SG_CIDR:     ${VPC_SG_CIDR}

ETX
```

```bash:コマンド:
aws ec2 authorize-security-group-ingress \
  --group-id ${VPC_SG_ID} \
  --protocol ${VPC_SG_PROTOCOL} \
  --port ${VPC_SG_PORT} \
  --cidr ${VPC_SG_CIDR}
```

```text:結果:
      (戻り値なし)
```


4. 事後作業
===========


セキュリティグループの内容確認
------------------------------

```bash:コマンド:
aws ec2 describe-security-groups \
  --group-ids ${VPC_SG_ID}
```

```json:結果(例):
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1", 
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ], 
                    "UserIdGroupPairs": [], 
                    "PrefixListIds": []
                }
            ], 
            "Description": "EC2 SMTP Global inbound", 
            "IpPermissions": [
                {
                    "PrefixListIds": [], 
                    "FromPort": 25, 
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ], 
                    "ToPort": 25, 
                    "IpProtocol": "tcp", 
                    "UserIdGroupPairs": []
                }
            ], 
            "GroupName": "ec2-smtp-global-inbound", 
            "VpcId": "vpc-xxxxxxxx", 
            "OwnerId": "XXXXXXXXXXXX", 
            "GroupId": "sg-xxxxxxxx"
        }
    ]
}
```

完了
====