Amazon Elasticsearch ServiceをAWS CLIで使う

はじめに

AWS re:Invent 2015直前に発表されたAmazon Elasticsearch Service(Amazon ES)。EAmazon ESにも当然CLIが用意されています。今回はCLIを使ったAmazon ESの操作を調べてみました。

やってみた

実行環境は以下の通りです。

$ aws --version
aws-cli/1.8.13 Python/2.7.10 Linux/4.1.7-15.23.amzn1.x86_64

create-elasticsearch-domain

Elasticserachドメインを作成します。ドメイン作成時のオプションは全て指定可能です。以前のブログ記事でやったのと同じような構成でドメインを作成するには以下のような形で指定します。

$ aws es create-elasticsearch-domain --domain-name myes --elasticsearch-cluster-config InstanceType=m3.medium.elasticsearch,InstanceCount=1,DedicatedMasterEnabled=false,ZoneAwarenessEnabled=false --access-policies '{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:*",
      "Condition": {
        "IpAddress": {
          "aws:SourceIp": [
            "X.X.X.X"
          ]
        }
      },
      "Resource": "arn:aws:es:ap-northeast-1:123456789012:domain/myes/*"
    }
  ]
}'

戻り値は以下のようになります。

{
    "DomainStatus": {
        "ElasticsearchClusterConfig": {
            "DedicatedMasterEnabled": false,
            "InstanceCount": 1,
            "ZoneAwarenessEnabled": false,
            "InstanceType": "m3.medium.elasticsearch"
        },
        "DomainId": "123456789012/myes",
        "Created": true,
        "Deleted": false,
        "EBSOptions": {
            "EBSEnabled": false
        },
        "Processing": true,
        "DomainName": "myes",
        "SnapshotOptions": {
            "AutomatedSnapshotStartHour": 0
        },
        "AccessPolicies": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:ap-northeast-1:123456789012:domain/myes/*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"X.X.X.X\"}}}]}",
        "AdvancedOptions": {
            "rest.action.multi.allow_explicit_index": "true"
        },
        "ARN": "arn:aws:es:ap-northeast-1:123456789012:domain/myes"
    }
}

list-domain-names

Elasticserachドメインの一覧を取得します。

$ aws es list-domain-names

{
    "DomainNames": [
        {
            "DomainName": "myes"
        }
    ]
}

describe-elasticsearch-domaindescribe-elasticsearch-domains

Elasticsearchドメインのドメイン情報を取得します。ドメイン作成時の戻り値と同じものが戻ってきます。

$ aws es describe-elasticsearch-domain --domain-name myes

{
    "DomainStatus": {
        "ElasticsearchClusterConfig": {
            "DedicatedMasterEnabled": false,
            "InstanceCount": 1,
            "ZoneAwarenessEnabled": false,
            "InstanceType": "m3.medium.elasticsearch"
        },
        "DomainId": "123456789012/myes",
        "Created": true,
        "Deleted": false,
        "EBSOptions": {
            "EBSEnabled": false
        },
        "Processing": false,
        "DomainName": "myes",
        "SnapshotOptions": {
            "AutomatedSnapshotStartHour": 0
        },
        "AccessPolicies": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:ap-northeast-1:123456789012:domain/myes/*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"X.X.X.X\"}}}]}",
        "AdvancedOptions": {
            "rest.action.multi.allow_explicit_index": "true"
        },
        "ARN": "arn:aws:es:ap-northeast-1:123456789012:domain/myes"
    }
}

describe-elasticsearch-domainsでは、--domain-nameの引数をリストとして指定できます。

$ aws es describe-elasticsearch-domain --domain-name myes myes2 myes3

describe-elasticsearch-domain-config

Elsaticsearchドメインの詳細な設定情報を取得します。

$ aws es describe-elasticsearch-domain-config --domain-name myes
{
    "DomainConfig": {
        "ElasticsearchClusterConfig": {
            "Status": {
                "PendingDeletion": false,
                "State": "Active",
                "CreationDate": 1444954226.793,
                "UpdateVersion": 6,
                "UpdateDate": 1444954811.186
            },
            "Options": {
                "DedicatedMasterEnabled": false,
                "InstanceCount": 1,
                "ZoneAwarenessEnabled": false,
                "InstanceType": "m3.medium.elasticsearch"
            }
        },
        "AdvancedOptions": {
            "Status": {
                "PendingDeletion": false,
                "State": "Active",
                "CreationDate": 1444955568.628,
                "UpdateVersion": 8,
                "UpdateDate": 1444955568.628
            },
            "Options": {
                "rest.action.multi.allow_explicit_index": "true"
            }
        },
        "EBSOptions": {
            "Status": {
                "PendingDeletion": false,
                "State": "Active",
                "CreationDate": 1444954226.793,
                "UpdateVersion": 6,
                "UpdateDate": 1444954811.186
            },
            "Options": {
                "EBSEnabled": false
            }
        },
        "AccessPolicies": {
            "Status": {
                "PendingDeletion": false,
                "State": "Active",
                "CreationDate": 1444954226.793,
                "UpdateVersion": 6,
                "UpdateDate": 1444954811.186
            },
            "Options": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:ap-northeast-1:123456789012:domain/myes/*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"X.X.X.X\"}}}]}"
        },
        "SnapshotOptions": {
            "Status": {
                "PendingDeletion": false,
                "State": "Active",
                "CreationDate": 1444954226.793,
                "UpdateVersion": 6,
                "UpdateDate": 1444954811.186
            },
            "Options": {
                "AutomatedSnapshotStartHour": 0
            }
        }
    }
}

update-elasticsearch-domain-config

Elasticsearchドメインの設定を更新します。例えばスナップショットの取得時間を更新する場合には以下のようにします。

$ aws es update-elasticsearch-domain-config --domain-name myes --snapshot-options AutomatedSnapshotStartHour=1

...snip...
            "Options": {
                "AutomatedSnapshotStartHour": 1
            }
...snip...

add-tags

Elasticsearchドメインにタグを付与します。タグ関連についてはドメイン名ではなくARNで指定します。

$ aws es add-tags --arn arn:aws:es:ap-northeast-1:123456789012:domain/myes --tag-list Key=Environment,Value=Production

list-tags

Elasticsearchドメインのタグを取得します。

$ aws es list-tags --arn arn:aws:es:ap-northeast-1:123456789012:domain/myes

{
    "TagList": [
        {
            "Value": "Production",
            "Key": "Environment"
        }
    ]
}

remove-tags

Elasticsearchドメインのタグを削除します。

$ aws es remove-tags --arn arn:aws:es:ap-northeast-1:123456789012:domain/myes --tag-keys Environment

$  aws es list-tags --arn arn:aws:es:ap-northeast-1:123456789012:domain/myes
{
    "TagList": []
}

delete-elasticsearch-domain

Elasticsearchドメインを削除します。

$ aws es delete-elasticsearch-domain --domain-name myes

{
    "DomainStatus": {
        "ElasticsearchClusterConfig": {
            "DedicatedMasterEnabled": false,
            "InstanceCount": 1,
            "ZoneAwarenessEnabled": false,
            "InstanceType": "m3.medium.elasticsearch"
        },
        "Endpoint": "search-myes-6ild3gcpntssl3i66e33szeobq.ap-northeast-1.es.amazonaws.com",
        "Created": true,
        "Deleted": true,
        "DomainName": "myes",
        "EBSOptions": {
            "EBSEnabled": false
        },
        "SnapshotOptions": {
            "AutomatedSnapshotStartHour": 1
        },
        "DomainId": "123456789012/myes",
        "AccessPolicies": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:ap-northeast-1:123456789012:domain/myes/*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"X.X.X.X\"}}}]}",
        "Processing": true,
        "AdvancedOptions": {
            "rest.action.multi.allow_explicit_index": "true"
        },
        "ARN": "arn:aws:es:ap-northeast-1:123456789012:domain/myes"
    }
}

最後に

Amazon ESも他のAWS同様にaws cliで簡単に操作できます。ぜひお試しください!