LoginSignup
0

More than 5 years have passed since last update.

[JAWS-UG CLI] Amazon Redshift 入門 (2) クラスターの作成

Last updated at Posted at 2016-05-23

このハンズオンについて

  • このハンズオンでは、Redshiftのクラスターとそのクラスタに対してクエリを発行するインスタンスの作成を実施します。
  • クエリの発行には、「psql」を利用します。本手順では、Amazon Linux上へのインストールと利用方法を説明します。

前提条件

バージョン確認

このハンズオンは以下のバージョンで動作確認を行いました。

コマンド
aws --version
結果
aws-cli/1.10.23 Python/2.7.10 Linux/4.4.5-15.26.amzn1.x86_64 botocore/1.4.14

必要な権限

作業にあたっては、以下の権限を有したIAMユーザもしくはIAMロールを利用してください。

  • EC2に対するフルコントロール権限
  • RedShiftに関するフルコントロール権限
  • IAMに関するフルコントロール権限
  • S3に関するフルコントロール権限
  • STSに関するフルコントロール権限

0. 準備

リージョンを指定

コマンド
export AWS_DEFAULT_REGION='ap-northeast-1'

資格情報を確認

コマンド
aws configure list
結果
Name                    Value             Type    Location
----                    -----             ----    --------
profile                <not set>             None    None
access_key     ****************RDPA         iam-role
secret_key     ****************9GA8         iam-role
region           ap-northeast-1              env    AWS_DEFAULT_REGION

1.Subnet Groupの作成

Redshiftの作成先となるSubnetを予めSubnet Groupとして指定しておく必要があります。

サブネットグループ名の指定

コマンド
CLUSTER_SUBNET_GROUP_NAME="redshift-subnet"
CLUSTER_SUBNET_GROUP_DESCRIPTION="Subnet Group for Redshift"

同名のサブネットグループが存在しないことを確認

コマンド
aws redshift describe-cluster-subnet-groups --cluster-subnet-group-name ${CLUSTER_SUBNET_GROUP_NAME}
結果
A client error (ClusterSubnetGroupNotFoundFault) occurred when calling the DescribeClusterSubnetGroups operation: Cluster Subnet group 'redshift-subnet' not found.

パラメータの確認

コマンド
cat << ETX

    CLUSTER_SUBNET_GROUP_NAME:${CLUSTER_SUBNET_GROUP_NAME}
    CLUSTER_SUBNET_GROUP_DESCRIPTION:${CLUSTER_SUBNET_GROUP_DESCRIPTION}
    SUBNET_A_ID:${SUBNET_A_ID}

ETX
結果

    CLUSTER_SUBNET_GROUP_NAME:redshift-subnet
    CLUSTER_SUBNET_GROUP_DESCRIPTION:Subnet Group for Redshift
    SUBNET_A_ID:subnet-********

サブネットグループの作成

コマンド
aws redshift create-cluster-subnet-group \
  --cluster-subnet-group-name ${CLUSTER_SUBNET_GROUP_NAME}  \
  --description "${CLUSTER_SUBNET_GROUP_DESCRIPTION}" \
  --subnet-ids ${SUBNET_A_ID}
結果
{
    "ClusterSubnetGroup": {
        "Subnets": [
            {
                "SubnetStatus": "Active",
                "SubnetIdentifier": "subnet-********",
                "SubnetAvailabilityZone": {
                    "Name": "ap-northeast-1a"
                }
            }
        ],
        "VpcId": "vpc-********",
        "Description": "Subnet Group for Redshift",
        "Tags": [],
        "SubnetGroupStatus": "Complete",
        "ClusterSubnetGroupName": "redshift-subnet"
    }
}

2. Security Groupの作成

Redshiftクラスターへのアクセス制限はVPCのSecurity Groupで行います。
(VPC-Classicな環境では専用のSecurity Groupを作成する必要があるようです。)

Security Group名の指定(Redshiftクラスタ用)

コマンド
SG_GROUP_NAME_REDSHIFT='Redshift'
SG_DESCRIPTION_REDSHIFT='JAWS-UG CLI at Co-Edo'

パラメータの確認

コマンド
cat << ETX

   SG_GROUP_NAME: ${SG_GROUP_NAME_REDSHIFT}
   SG_DESCRIPTION: ${SG_DESCRIPTION_REDSHIFT}

ETX
結果

   SG_GROUP_NAME: Redshift
   SG_DESCRIPTION: JAWS-UG CLI at Co-Edo

Redshiftのクラスターに設定するSecurity Groupを作成

コマンド
SG_ID_REDSHIFT=`aws ec2 create-security-group --group-name ${SG_GROUP_NAME_REDSHIFT} \
  --description "${SG_DESCRIPTION_REDSHIFT}" --vpc-id ${VPC_ID} \
  --query GroupId \
  --output text` \
  && echo ${SG_ID_REDSHIFT}
結果
sg-********

作成したSecurity Groupを確認

(アウトバウンドの通信のみ全て許可する設定のみ、がデフォルト)

コマンド
aws ec2 describe-security-groups --group-ids ${SG_ID_REDSHIFT}
結果
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "PrefixListIds": []
                }
            ],
            "Description": "JAWS-UG CLI at Co-Edo",
            "IpPermissions": [],
            "GroupName": "Redshift",
            "VpcId": "vpc-********",
            "OwnerId": "************",
            "GroupId": "sg-********"
        }
    ]
}

インバウンドのルールを追加

Redshiftへの接続を許可します。

コマンド
aws ec2 authorize-security-group-ingress --group-id ${SG_ID_REDSHIFT} \
  --protocol 'tcp' --port 5439 --cidr ${PUBLIC_IP_ADDRESS}/32
結果
(返値無し)

追加されたルールを確認

コマンド
aws ec2 describe-security-groups --group-ids ${SG_ID_REDSHIFT}
結果
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "PrefixListIds": []
                }
            ],
            "Description": "JAWS-UG CLI at Co-Edo",
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 5439,
                    "IpRanges": [
                        {
                            "CidrIp": "**.**.**.**/32"
                        }
                    ],
                    "ToPort": 5439,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": []
                }
            ],
            "GroupName": "Redshift",
            "VpcId": "vpc-********",
            "OwnerId": "************",
            "GroupId": "sg-********"
        }
    ]
}

3.クラスタの作成

Redshiftクラスターを作成します。

パラメータの指定

コマンド
CLUSTER_NAME="mycluster"
DB_NAME="mydb"
PORT="5439"
MASTER_USER_NAME="awsuser"
MASTER_USER_PASSWORD="Pa55w0rd"
NODE_TYPE="dc1.large"
CLUSTER_TYPE="single-node"

その他のパラメータについて

その他はデフォルトの設定を利用します。

デフォルト設定は、公式リファレンスを確認してください。

パラメータの確認

コマンド
cat << ETX

    CLUSTER_NAME:${CLUSTER_NAME}
    DB_NAME:${DB_NAME}
    PORT:${PORT}
    MASTER_USER_NAME:${MASTER_USER_NAME}
    MASTER_USER_PASSWORD:${MASTER_USER_PASSWORD}
    NODE_TYPE:${NODE_TYPE}
    CLUSTER_TYPE:${CLUSTER_TYPE}

ETX
結果

クラスタの作成

コマンド
aws redshift create-cluster \
  --db-name ${DB_NAME} \
  --cluster-identifier ${CLUSTER_NAME} \
  --cluster-type ${CLUSTER_TYPE} \
  --node-type ${NODE_TYPE} \
  --master-username ${MASTER_USER_NAME} \
  --master-user-password ${MASTER_USER_PASSWORD} \
  --port ${PORT} \
  --cluster-subnet-group-name ${CLUSTER_SUBNET_GROUP_NAME} \
  --vpc-security-group-ids ${SG_ID_REDSHIFT}
結果
{
    "Cluster": {
        "IamRoles": [],
        "ClusterVersion": "1.0",
        "NumberOfNodes": 1,
        "VpcId": "vpc-********",
        "NodeType": "dc1.large",
        "PubliclyAccessible": true,
        "Tags": [],
        "MasterUsername": "awsuser",
        "ClusterParameterGroups": [
            {
                "ParameterGroupName": "default.redshift-1.0",
                "ParameterApplyStatus": "in-sync"
            }
        ],
        "Encrypted": false,
        "ClusterSecurityGroups": [],
        "AllowVersionUpgrade": true,
        "VpcSecurityGroups": [
            {
                "Status": "active",
                "VpcSecurityGroupId": "sg-********"
            }
        ],
        "ClusterSubnetGroupName": "redshift-sg",
        "AutomatedSnapshotRetentionPeriod": 1,
        "ClusterStatus": "creating",
        "ClusterIdentifier": "mycluster",
        "DBName": "mydb",
        "PreferredMaintenanceWindow": "sun:14:30-sun:15:00",
        "PendingModifiedValues": {
            "MasterUserPassword": "****"
        }
    }
}

作成したクラスタの確認

コマンド
aws redshift describe-clusters --cluster-identifier ${CLUSTER_NAME}
結果
{
    "Clusters": [
        {
            "PubliclyAccessible": true,
            "MasterUsername": "awsuser",
            "VpcSecurityGroups": [
                {
                    "Status": "active",
                    "VpcSecurityGroupId": "sg-********"
                }
            ],
            "NumberOfNodes": 1,
            "PendingModifiedValues": {
                "MasterUserPassword": "****"
            },
            "VpcId": "vpc-********",
            "ClusterVersion": "1.0",
            "Tags": [],
            "AutomatedSnapshotRetentionPeriod": 1,
            "ClusterParameterGroups": [
                {
                    "ParameterGroupName": "default.redshift-1.0",
                    "ParameterApplyStatus": "in-sync"
                }
            ],
            "DBName": "mydb",
            "PreferredMaintenanceWindow": "sun:14:30-sun:15:00",
            "IamRoles": [],
            "AllowVersionUpgrade": true,
            "ClusterSubnetGroupName": "redshift-sg",
            "ClusterSecurityGroups": [],
            "ClusterIdentifier": "mycluster",
            "ClusterNodes": [
                {
                    "NodeRole": "SHARED",
                    "PublicIPAddress": "**.**.**.**"
                }
            ],
            "AvailabilityZone": "ap-northeast-1a",
            "NodeType": "dc1.large",
            "Encrypted": false,
            "ClusterRevisionNumber": "1043",
            "ClusterStatus": "creating"
        }
    ]
}

以上

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0