AWS CLIでサービスの各種コマンドを動かしてみる(Redshift編1: クラスタ・スナップショット)

2013.08.31

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

IAM, EC2, S3と基本的なサービスに触れてきたこのシリーズですが一気にここで新しめのサービスであるRedshiftに行ってみたいと思います。まぁ最近良く触っているのと言うのもあるので。

クラスタ(cluster)に関する操作

クラスタに関する操作は以下の5つ。

create-cluster
describe-clusters
modify-cluster
reboot-cluster
delete-cluster

create-cluster

クラスタを作成します。VPC内で作成/VPCを使わずに作成,シングルノード/マルチノード等の違いで必要とするパラメータの内容に若干違いが出てきますが、コンソールでの処理同様、デフォルト値が設定されるものについては指定無しで行ける模様。

$ aws redshift create-cluster
  --cluster-identifier cmtestcluster
  --cluster-type single-node
  --node-type dw.hs1.xlarge
  --master-username cmtestuser
  --master-user-password cmtestpassABC123
  
{
    "Cluster": {
        "Encrypted": false, 
        "ClusterVersion": "1.0", 
        "NumberOfNodes": 1, 
        "VpcId": "vpc-0f5de367", 
        "NodeType": "dw.hs1.xlarge", 
        "PubliclyAccessible": true, 
        "MasterUsername": "cmtestuser", 
        "ClusterParameterGroups": [
            {
                "ParameterGroupName": "default.redshift-1.0", 
                "ParameterApplyStatus": "in-sync"
            }
        ], 
        "AutomatedSnapshotRetentionPeriod": 1, 
        "AllowVersionUpgrade": true, 
        "VpcSecurityGroups": [], 
        "ClusterSubnetGroupName": "default", 
        "ClusterSecurityGroups": [], 
        "ClusterStatus": "creating", 
        "ClusterIdentifier": "cmtestcluster", 
        "PreferredMaintenanceWindow": "wed:20:30-wed:21:00", 
        "PendingModifiedValues": {
            "MasterUserPassword": "****"
        }
    }
}
$ aws redshift create-cluster
  --cluster-identifier cmdevcluster
  --cluster-type multi-node
  --number-of-nodes 3
  --node-type dw.hs1.xlarge
  --master-username cmtestuser
  --master-user-password cmtestpassDEF456

{
    "Cluster": {
        "Encrypted": false, 
        "ClusterVersion": "1.0", 
        "NumberOfNodes": 3, 
        "VpcId": "vpc-0f5de367", 
        "NodeType": "dw.hs1.xlarge", 
        "PubliclyAccessible": true, 
        "MasterUsername": "cmtestuser", 
        "ClusterParameterGroups": [
            {
                "ParameterGroupName": "default.redshift-1.0", 
                "ParameterApplyStatus": "in-sync"
            }
        ], 
        "AutomatedSnapshotRetentionPeriod": 1, 
        "AllowVersionUpgrade": true, 
        "VpcSecurityGroups": [], 
        "ClusterSubnetGroupName": "default", 
        "ClusterSecurityGroups": [], 
        "ClusterStatus": "creating", 
        "ClusterIdentifier": "cmdevcluster", 
        "PreferredMaintenanceWindow": "wed:13:00-wed:13:30", 
        "PendingModifiedValues": {
            "MasterUserPassword": "****"
        }
    }
}
$

試しに2台クラスタを立ててみました。以下画像はコマンド実行直後のクラスタ一覧です。

awscli-create-1

VPC-IDが割り振られています。この操作を試したアカウントでのSupported Platformsが『EC2-VPC』のみのため、パラメータ指定無しで行ったこの操作は『VPC内にクラスタを作成する』と認識されたようです。

awscli-create-3

describe-clusters

クラスタの一覧/詳細を取得します。--cluster-identifierを指定すると任意のクラスタの情報を確認出来ます。

$ aws redshift describe-clusters --cluster-identifier cmtestcluster | jq '.'
{
  "Clusters": [
    {
      "PendingModifiedValues": {},
      "PreferredMaintenanceWindow": "wed:20:30-wed:21:00",
      "ClusterIdentifier": "cmtestcluster",
      "ClusterStatus": "available",
      "ClusterParameterGroups": [
        {
          "ParameterApplyStatus": "in-sync",
          "ParameterGroupName": "default.redshift-1.0"
        }
      ],
      "MasterUsername": "cmtestuser",
      "AvailabilityZone": "ap-northeast-1a",
      "PubliclyAccessible": true,
      "NodeType": "dw.hs1.xlarge",
      "Endpoint": {
        "Address": "cmtestcluster.xxxxxxxxxxx.ap-northeast-1.redshift.amazonaws.com",
        "Port": 5439
      },
      "NumberOfNodes": 1,
      "ClusterVersion": "1.0",
      "Encrypted": false,
      "ClusterSecurityGroups": [],
      "AllowVersionUpgrade": true,
      "VpcSecurityGroups": [],
      "VpcId": "vpc-0f5de367",
      "ClusterCreateTime": "2013-08-30T16:49:48.459Z",
      "ClusterSubnetGroupName": "default",
      "AutomatedSnapshotRetentionPeriod": 1
    }
  ]
}
$
$ aws redshift describe-clusters | jq '.Clusters[].ClusterIdentifier'
"cmdevcluster"
"cmtestcluster"
$

modify-cluster

クラスタの情報を更新します。helpコマンドで内容を確認してみる感じでは、ひと通り更新が行えそうです。あまり利用料金に影響の掛からなさそうな(笑)要素を更新する事にします。スナップショット自動化の間隔とパスワードを変えてみます。

現状はこちらの内容。

awscli-create-4

更新実施。

$ aws redshift modify-cluster
    --cluster-identifier cmtestcluster
    --automated-snapshot-retention-period 3
    --master-user-password Redshift20130831

更新完了後。ちゃんと更新されていますね。

awscli-create-5

reboot-cluster

所定クラスタの再起動を行います。

$ aws redshift reboot-cluster --cluster-identifier cmtestcluster
{
    "Cluster": {
        "ClusterVersion": "1.0", 
        "NumberOfNodes": 1, 
        "Endpoint": {
            "Port": 5439, 
            "Address": "cmtestcluster.cpawnbhuvpy0.ap-northeast-1.redshift.amazonaws.com"
        }, 
        "NodeType": "dw.hs1.xlarge", 
        "PubliclyAccessible": true, 
        "AvailabilityZone": "ap-northeast-1a", 
        "MasterUsername": "cmtestuser", 
        "ClusterParameterGroups": [
            {
                "ParameterGroupName": "default.redshift-1.0", 
                "ParameterApplyStatus": "in-sync"
            }
        ], 
        "Encrypted": false, 
        "ClusterSecurityGroups": [], 
        "AllowVersionUpgrade": true, 
        "VpcSecurityGroups": [], 
        "VpcId": "vpc-0f5de367", 
        "ClusterCreateTime": "2013-08-30T16:49:48.459Z", 
        "ClusterSubnetGroupName": "default", 
        "AutomatedSnapshotRetentionPeriod": 3, 
        "ClusterStatus": "rebooting", 
        "ClusterIdentifier": "cmtestcluster", 
        "PreferredMaintenanceWindow": "wed:20:30-wed:21:00", 
        "PendingModifiedValues": {}
    }
}
$

コマンド実行直後。確かにreboot処理が走っています。

awscli-reboot-1

delete-cluster

クラスタを削除します。シンプルにIDのみ指定して実行してみると、以下のようなメッセージが。SkipFinalClusterSnapshotが指定されていない場合はFinalClusterSnapshotIdentifierの指定が求められる、とあります。

$ aws redshift delete-cluster --cluster-identifier cmtestcluster
{
    "Cluster": {}, 
    "Errors": [
        {
            "Message": "FinalClusterSnapshotIdentifier is required unless SkipFinalClusterSnapshot is specified.", 
            "Code": "InvalidParameterCombination", 
            "Type": "Sender"
        }
    ], 
    "ResponseMetadata": {
        "RequestId": "f90b9c96-11d9-11e3-ae4e-d7eeeff925a2"
    }
}
A client error (InvalidParameterCombination) occurred: FinalClusterSnapshotIdentifier is required unless SkipFinalClusterSnapshot is specified.
$

ヘルプ内のオプション説明文。『クラスタの最終スナップショットをクラスタ削除する前に取得しておく処理をスキップするかどうかを決定。もしtrueなら、クラスタの最終スナップショット作成はスキップされます。デフォルト値はfalseです。』と書かれています。

       --skip-final-cluster-snapshot (boolean)
          Determines whether a final snapshot of the cluster is created before
          Amazon Redshift deletes the cluster. If true , a final cluster snap-
          shot is not created. If false , a final cluster snapshot is  created
          before the cluster is deleted.

          NOTE:
              The --final-cluster-snapshot-identifier parameter must be speci-
              fied if --skip-final-cluster-snapshot is false .

          Default: false

そしてこちらが上記オプションでfalseを指定した際に求められる内容。試した内容ではデフォルトfalseになるので、スキップしないならスナップショットのIDを指定しましょう、と指摘された事になるんですね。

       --final-cluster-snapshot-identifier (string)
          The identifier of the final snapshot that is to be  created  immedi-
          ately  before  deleting  the cluster. If this parameter is provided,
          --skip-final-cluster-snapshot must be false .

          Constraints:

              o Must be 1 to 255 alphanumeric characters.

              o First character must be a letter.

              o Cannot end with a hyphen or contain two consecutive hyphens.

trueを指定して実行。……ん〜またboolean値が上手く認識されてない。(この辺、何をどうやればboolean値が上手く認識されるんだろう?)

$ aws redshift delete-cluster --cluster-identifier cmtestcluster --skip-final-cluster-snapshot true
Unknown options: true
$ aws redshift delete-cluster --cluster-identifier cmtestcluster --skip-final-cluster-snapshot true
Unknown options: true
$ aws redshift delete-cluster --cluster-identifier cmtestcluster --skip-final-cluster-snapshot True
Unknown options: True
$ aws redshift delete-cluster --cluster-identifier cmtestcluster --skip-final-cluster-snapshot "true"
Unknown options: true
$

削除時の最終スナップショットスキップせずに、ちゃんと指定するオプションで試してみます。こちらは上手く行ったようです。

$ aws redshift delete-cluster --cluster-identifier cmtestcluster --final-cluster-snapshot-identifier cmtestcluster-snapshot-final
{
    "Cluster": {
        "ClusterVersion": "1.0", 
        "NumberOfNodes": 1, 
        "Endpoint": {
            "Port": 5439, 
            "Address": "cmtestcluster.cpawnbhuvpy0.ap-northeast-1.redshift.amazonaws.com"
        }, 
        "NodeType": "dw.hs1.xlarge", 
        "PubliclyAccessible": true, 
        "AvailabilityZone": "ap-northeast-1a", 
        "MasterUsername": "cmtestuser", 
        "ClusterParameterGroups": [
            {
                "ParameterGroupName": "default.redshift-1.0", 
                "ParameterApplyStatus": "in-sync"
            }
        ], 
        "Encrypted": false, 
        "ClusterSecurityGroups": [], 
        "AllowVersionUpgrade": true, 
        "VpcSecurityGroups": [], 
        "VpcId": "vpc-0f5de367", 
        "ClusterCreateTime": "2013-08-30T16:49:48.459Z", 
        "ClusterSubnetGroupName": "default", 
        "AutomatedSnapshotRetentionPeriod": 3, 
        "ClusterStatus": "final-snapshot", 
        "ClusterIdentifier": "cmtestcluster", 
        "PreferredMaintenanceWindow": "wed:20:30-wed:21:00", 
        "PendingModifiedValues": {}
    }
}
$

所定の名前でスナップショットも取られ、削除処理も走りました。

awscli-delete-2

awscli-delete-1

スナップショット(cluster-snapshot)に関する操作

クラスタのスナップショットに関する操作は以下の4つ。

create-cluster-snapshot
copy-cluster-snapshot
describe-cluster-snapshots
delete-cluster-snapshot

create-snapshot

所定のクラスタのスナップショットを作成します。

$ aws redshift create-cluster-snapshot 
    --cluster-identifier cmtestcluster
    --snapshot-identifier cmtestcluster-ss
{
    "Snapshot": {
        "Status": "creating", 
        "SnapshotCreateTime": "2013-08-31T00:16:33.094Z", 
        "AvailabilityZone": "ap-northeast-1a", 
        "ClusterVersion": "1.0", 
        "MasterUsername": "cmtestuser", 
        "Encrypted": false, 
        "OwnerAccount": "014656228046", 
        "BackupProgressInMegaBytes": 0.0, 
        "ElapsedTimeInSeconds": 0, 
        "CurrentBackupRateInMegaBytesPerSecond": 0.0, 
        "EstimatedSecondsToCompletion": -1, 
        "VpcId": "vpc-0f5de367", 
        "ClusterCreateTime": "2013-08-30T16:49:48.459Z", 
        "ActualIncrementalBackupSizeInMegaBytes": -1.0, 
        "SnapshotType": "manual", 
        "NodeType": "dw.hs1.xlarge", 
        "ClusterIdentifier": "cmtestcluster", 
        "TotalBackupSizeInMegaBytes": -1.0, 
        "Port": 5439, 
        "NumberOfNodes": 1, 
        "SnapshotIdentifier": "cmtestcluster-ss"
    }
}
$

copy-snapshot

スナップショットのコピーを行います。試しに上記処理で作成したスナップショットをコピーしてみます…と思ったらエラーが出ちゃいました。『自動生成されたスナップショットだけがコピー出来ますよ』とあります。

$ aws redshift copy-cluster-snapshot
  --source-snapshot-identifier cmtestcluster-ss
  --target-snapshot-identifier cmtestcluster-target
{
    "Errors": [
        {
            "Message": "Only automated snapshots can be copied.", 
            "Code": "InvalidParameterValue", 
            "Type": "Sender"
        }
    ], 
    "Snapshot": {}, 
    "ResponseMetadata": {
        "RequestId": "e7758600-11d2-11e3-b03a-6d67f9a2b4e8"
    }
}
A client error (InvalidParameterValue) occurred: Only automated snapshots can be copied.
$

管理コンソールで内容を確認してみましょう。"Type"欄及び詳細内容にはこのような情報が記載されていました。

awscli-copy-snapshot-2

自動生成されたスナップショットをコピー元に指定して再度コマンド実行。今度は上手く行きました。

$ aws redshift copy-cluster-snapshot
  --source-snapshot-identifier rs:cmtestcluster-2013-08-30-16-54-35
  --target-snapshot-identifier cmtestcluster-target
{
    "Snapshot": {
        "Status": "available", 
        "SnapshotCreateTime": "2013-08-30T16:54:35.090Z", 
        "AvailabilityZone": "ap-northeast-1a", 
        "ClusterVersion": "1.0", 
        "MasterUsername": "cmtestuser", 
        "Encrypted": false, 
        "OwnerAccount": "014656228046", 
        "BackupProgressInMegaBytes": 15.0, 
        "ElapsedTimeInSeconds": 1, 
        "CurrentBackupRateInMegaBytesPerSecond": 0.0, 
        "EstimatedSecondsToCompletion": -1, 
        "VpcId": "vpc-0f5de367", 
        "ClusterCreateTime": "2013-08-30T16:49:48.459Z", 
        "ActualIncrementalBackupSizeInMegaBytes": 15.0, 
        "SnapshotType": "manual", 
        "NodeType": "dw.hs1.xlarge", 
        "ClusterIdentifier": "cmtestcluster", 
        "TotalBackupSizeInMegaBytes": 15.0, 
        "Port": 5439, 
        "NumberOfNodes": 1, 
        "SnapshotIdentifier": "cmtestcluster-target"
    }
}
$

管理コンソール上でも複製内容を確認出来ました。

awscli-copy-snapshot-3

describe-cluster-snapshots

クラスタスナップショットの内容を確認します。所定のID指定で個別内容を、また指定無しの場合はアカウントに紐づく全てのクラスタスナップショットを表示します。

$ aws redshift describe-cluster-snapshots --snapshot-identifier rs:cmtestcluster-2013-08-30-16-54-35
{
    "Snapshots": [
        {
            "Status": "available", 
            "SnapshotCreateTime": "2013-08-30T16:54:35.090Z", 
            "AvailabilityZone": "ap-northeast-1a", 
            "ClusterVersion": "1.0", 
            "MasterUsername": "cmtestuser", 
            "Encrypted": false, 
            "OwnerAccount": "014656228046", 
            "BackupProgressInMegaBytes": 15.0, 
            "ElapsedTimeInSeconds": 1, 
            "CurrentBackupRateInMegaBytesPerSecond": 0.0, 
            "EstimatedSecondsToCompletion": -1, 
            "VpcId": "vpc-0f5de367", 
            "ClusterCreateTime": "2013-08-30T16:49:48.459Z", 
            "ActualIncrementalBackupSizeInMegaBytes": 15.0, 
            "SnapshotType": "automated", 
            "NodeType": "dw.hs1.xlarge", 
            "ClusterIdentifier": "cmtestcluster", 
            "TotalBackupSizeInMegaBytes": 15.0, 
            "Port": 5439, 
            "NumberOfNodes": 1, 
            "SnapshotIdentifier": "rs:cmtestcluster-2013-08-30-16-54-35"
        }
    ]
}
$

delete-cluster-snapshot

所定のクラスタスナップショットを削除します。これも上記に倣い自動生成されたものの削除を試みてみましたが、今度は逆に『マニュアルコピーされたものしか削除出来ません』と返されてしまいました。

$ aws redshift delete-cluster-snapshot --snapshot-identifier rs:cmdevcluster-2013-08-30-17-14-07
{
    "Errors": [
        {
            "Message": "Only manual snapshots may be deleted.", 
            "Code": "InvalidClusterSnapshotState", 
            "Type": "Sender"
        }
    ], 
    "Snapshot": {}, 
    "ResponseMetadata": {
        "RequestId": "4cdb982f-11d6-11e3-9c44-b5868b3956aa"
    }
}
A client error (InvalidClusterSnapshotState) occurred: Only manual snapshots may be deleted.
$

自動生成されたものから手動コピーしたクラスタスナップショットを削除対象に指定。今度はさっくり削除出来ました。

$ aws redshift delete-cluster-snapshot --snapshot-identifier cmtestcluster-target
{
    "Snapshot": {
        "Status": "deleted", 
        "SnapshotCreateTime": "2013-08-30T16:54:35.090Z", 
        "AvailabilityZone": "ap-northeast-1a", 
        "ClusterVersion": "1.0", 
        "MasterUsername": "cmtestuser", 
        "Encrypted": false, 
        "OwnerAccount": "014656228046", 
        "BackupProgressInMegaBytes": 15.0, 
        "ElapsedTimeInSeconds": 1, 
        "CurrentBackupRateInMegaBytesPerSecond": 0.0, 
        "EstimatedSecondsToCompletion": -1, 
        "VpcId": "vpc-0f5de367", 
        "ClusterCreateTime": "2013-08-30T16:49:48.459Z", 
        "ActualIncrementalBackupSizeInMegaBytes": 15.0, 
        "SnapshotType": "manual", 
        "NodeType": "dw.hs1.xlarge", 
        "ClusterIdentifier": "cmtestcluster", 
        "TotalBackupSizeInMegaBytes": 15.0, 
        "Port": 5439, 
        "NumberOfNodes": 1, 
        "SnapshotIdentifier": "cmtestcluster-target"
    }
}
$

(ちなみに以下の3つはスナップショットのリストアに関するもの。動作検証を行ったアカウントが制限によりこの辺りの操作を行えなかったので、スナップショットのリストアに関しては後日別エントリにて試みてみる方向で。

authorize-snapshot-access
revoke-snapshot-access
restore-from-cluster-snapshot