前提条件
EC2への権限
EC2に対してフル権限があること。
AWS CLI
以下のバージョンで動作確認済
- AWS CLI 1.11.14
 
コマンド
aws --version
結果(例)
      aws-cli/1.11.14 Python/2.7.10 Darwin/15.6.0 botocore/1.4.71
バージョンが古い場合は最新版に更新しましょう。
コマンド
sudo -H pip install -U awscli
AWSアカウントの属性
AWSアカウントがEC2-Classicに対応していないこと。
コマンド
AWS_SUPPORT_PLATFORMS=$( \
       aws ec2 describe-account-attributes \
         --query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
         --output text \
) \
      && echo ${AWS_SUPPORT_PLATFORMS}
結果:
VPC
注釈
'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは利用してください。
- 準備
======= 
0.1. リージョンの決定
変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'
0.2. 変数の確認
プロファイルが想定のものになっていることを確認します。
変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       rdsFull-prjz-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region        ap-northeast-1        env    AWS_DEFAULT_REGION
0.3. VPCの指定
既存のVPCに割り当てられているCIDRブロックを確認します。
コマンド
aws ec2 describe-vpcs \
      --query 'Vpcs[].CidrBlock'
結果(例):
[
  "172.31.0.0/16",
  "10.192.0.0/16"
]
ここでは、10.192.0.0/16を範囲とするVPCを選択します。
変数の設定
VPC_CIDR='10.192.0.0/16'
VPC IDを取得します。
コマンド
VPC_ID=$( \
      aws ec2 describe-vpcs \
        --filters Name=cidr,Values=${VPC_CIDR} \
        --query 'Vpcs[].VpcId' \
        --output text \
) \
      && echo ${VPC_ID}
結果(例):
vpc-xxxxxxxx
- 事前作業
=========== 
1.1. Security Groupの名称の決定
変数の設定
VPC_SG_NAME='rds-postgresql-inbound'
1.2. Security Groupの説明の決定
変数の設定
VPC_SG_DESC='RDS PostgreSQL inbound'
1.3. Security Groupの不存在確認
コマンド
aws ec2 describe-security-groups \
  --query "SecurityGroups[?VpcId == \`${VPC_ID}\` \
    && GroupName == \`${VPC_SG_NAME}\`]"
結果(例)
[]
1.4. Security Groupの設定内容決定
コマンド
VPC_SG_PROTOCOL='tcp'
VPC_SG_PORT='5432'
VPC_SG_CIDR='0.0.0.0/0'
- Security Groupの作成
======================= 
2.1. Security Groupの作成
変数の確認
cat << ETX
        VPC_SG_NAME: ${VPC_SG_NAME}
        VPC_SG_DESC: "${VPC_SG_DESC}"
        VPC_ID:      "${VPC_ID}"
ETX
コマンド
aws ec2 create-security-group \
        --group-name ${VPC_SG_NAME} \
        --description "${VPC_SG_DESC}" \
        --vpc-id ${VPC_ID}
結果(例)
      {
          "GroupId": "sg-xxxxxxxx"
      }
2.2. Security Groupの確認
コマンド
aws ec2 describe-security-groups \
  --query "SecurityGroups[?VpcId == \`${VPC_ID}\` \
    && GroupName == \`${VPC_SG_NAME}\`]"
結果(例)
      {
        "SecurityGroups": [
          {
              "IpPermissionsEgress": [
                  {
                      "IpProtocol": "-1",
                      "IpRanges": [
                          {
                              "CidrIp": "0.0.0.0/0"
                          }
                      ],
                      "UserIdGroupPairs": [],
                      "PrefixListIds": []
                  }
              ],
              "Description": "RDS PosgreSQL inbound",
              "IpPermissions": [],
              "GroupName": "rds-posgre-inbound",
              "VpcId": "vpc-xxxxxxxx",
              "OwnerId": "XXXXXXXXXXXX",
              "GroupId": "sg-xxxxxxxx"
          }
        ]
      }
2.3. VPC Security Group IDの取得
コマンド
VPC_SG_ID=$( \
  aws ec2 describe-security-groups \
    --filter Name=group-name,Values=${VPC_SG_NAME} \
    --query "SecurityGroups[?VpcId == \` ${VPC_ID}\`].GroupId" \
    --output text \
) \
        && echo ${VPC_SG_ID}
結果(例)
      sg-xxxxxxxx
2.4. アクセス制御の設定
変数の確認
cat << ETX
        VPC_SG_ID:       ${VPC_SG_ID}
        VPC_SG_PROTOCOL: ${VPC_SG_PROTOCOL}
        VPC_SG_PORT:     ${VPC_SG_PORT}
        VPC_SG_CIDR:     ${VPC_SG_CIDR}
ETX
コマンド
aws ec2 authorize-security-group-ingress \
        --group-id ${VPC_SG_ID} \
        --protocol ${VPC_SG_PROTOCOL} \
        --port ${VPC_SG_PORT} \
        --cidr ${VPC_SG_CIDR}
結果(例)
      (戻り値なし)
- 事後確認
=========== 
セキュリティグループの内容確認
コマンド
aws ec2 describe-security-groups \
  --query "SecurityGroups[?VpcId == \`${VPC_ID}\` \
    && GroupName == \`${VPC_SG_NAME}\`]"
結果(例)
      {
        "SecurityGroups": [
          {
              "IpPermissionsEgress": [
                  {
                      "IpProtocol": "-1",
                      "IpRanges": [
                          {
                              "CidrIp": "0.0.0.0/0"
                          }
                      ],
                      "UserIdGroupPairs": [],
                      "PrefixListIds": []
                  }
              ],
              "Description": "RDS PosgreSQL inbound",
              "IpPermissions": [
                  {
                      "PrefixListIds": [],
                      "FromPort": 5432,
                      "IpRanges": [
                          {
                              "CidrIp": "0.0.0.0/0"
                          }
                      ],
                      "ToPort": 5432,
                      "IpProtocol": "tcp",
                      "UserIdGroupPairs": []
                  }
              ],
              "GroupName": "rds-posgre-inbound",
              "VpcId": "vpc-xxxxxxxx",
              "OwnerId": "XXXXXXXXXXXX",
              "GroupId": "sg-xxxxxxxx"
          }
        ]
      }