AWS OpenSearch Serviceでスナップショット作成
1. スナップショットを保存するS3バケット、OpenSearchからS3へのWrite権限を持つIAM Roleを作成
S3バケット: 任意
IAM Role policy
code:iam
{
"Statement": [
{
"Action": "s3:ListBucket",
"Effect": "Allow",
"Resource": "arn:aws:s3:::<your_s3_bucket_name>",
"Sid": ""
},
{
"Action": [
"s3:PutObject",
"s3:HeadObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<your_s3_bucket_name}/*",
"Sid": "writePDF"
}
],
"Version": "2012-10-17"
}
2. OpenSearchのRoleに1で作成されたRoleをMapする
OpenSearchダッシュボードのSecurity -> Roles より manage_snapshots Roleを探しBacked Roleに1.で作成したIAM RoleのARNを追加する
https://gyazo.com/cea8b45bdbe0160250bf194c36427089
3. snapshot レポジトリを登録する
※以下IAM Roleベースでリクエストを認証している場合
登録するにはOpenSearch管理権限のあるIAM Roleでリクエストする必要がある
VPC内にOpenSearchが存在する場合管理権限ポリシーがinstance profileにアタッチされているEC2やECSコンテナ上で作業すると楽かもしれない
以下のIAM PolicyがattachされているRoleであればOK
code:iam instance profile
{
"Action": [
"iam:PassRole",
"es:ESHttpPut"
],
"Effect": "Allow",
"Resource": "*"
}
2. でMapしたように管理権限のあるRoleにsnapshot repository登録リクエスト元のRoleをMapする
https://gyazo.com/dd5b876cebab4e4b7804f12453b1c895
この時点で初めてSnapshotレポジトリを登録出来るようになる
以下のスクリプトで登録する(公式推奨方法). 変数は適宜作成したバケットとIAM Roleに置き換え
code:python
import boto3
import requests
from requests_aws4auth import AWS4Auth
# VPC内OpenSearchの場合
region = "ap-northeast-1" # e.g. us-west-1
service = "es"
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(
credentials.access_key,
credentials.secret_key,
region,
service,
session_token=credentials.token,
)
# Register repository
path = "_snapshot/snapshot-repo" # the OpenSearch API endpoint
url = host + path
payload = {
"type": "s3",
"settings": {
"bucket": "opensearch-snapshot", # 適宜置き換え
"region": "ap-northeast-1",
"role_arn": "arn:aws:iam::~~~~~opensearch-snapshot", # 適宜置き換え
},
}
headers = {"Content-Type": "application/json"}
r = requests.put(url, auth=awsauth, json=payload, headers=headers)
print(r.status_code)
print(r.text)
上記スクリプトを実行し権限等も問題ないと
code:bash
200
{"acknowledged":true}
のような出力になる
OpenSearchリクエストの権限が足りないと以下のような出力になる場合がある。この場合は arn:aws:iam::1234567890:role/bastion-instance roleを上記のようにBacked RoleにOpenSearch Dashboardから管理権限のあるRoleにMappingしてやればOK
code:error
リポジトリが登録出来たらOpenSearchのDev Consoleから実際にスナップショットを作成出来る
Snapshotリポジトリ一覧表示
code:console
GET /_cat/repositories?v
id type
cs-automated-enc s3
snapshot-repo s3
手動スナップショット作成
code:console
PUT /_snapshot/snapshot-repo/2022-11-10
{
"accepted" : true
}
スナップショット一覧表示
stateが SUCCESS となっていれば成功作成. Indexが大きい場合作成にしばらく時間がかかる
code:console
GET /_snapshot/snapshot-repo/_all
{
"snapshots" : [ {
"snapshot" : "2022-11-10",
"uuid" : "X6hBmHdMSzip85q9RPBFMQ",
"version_id" : 135248027,
"version" : "1.3.2",
"data_streams" : ,
"include_global_state" : true,
"state" : "SUCCESS",
"start_time" : "2022-11-10T05:24:51.803Z",
"start_time_in_millis" : 1668057891803,
"end_time" : "2022-11-10T05:24:54.804Z",
"end_time_in_millis" : 1668057894804,
"duration_in_millis" : 3001,
"failures" : ,
"shards" : {
"total" : 8,
"failed" : 0,
"successful" : 8
}
} ]
}
リストア. indices で除外したいIndexや対象にしたいIndexを指定出来る. (※AWS管理のため以下のIndexを除外しないとDashboard上からはRestore出来なかった)
code:console
POST /_snapshot/snapshot-repo/2022-11-10/_restore
{
"indices": "-.kibana*,-.opendistro_security"
}
ちなみに既存のIndexが存在する状態でリストアしようとすると以下のようになるのでリストア先を別の名前にするか既存のIndexを削除する
code:console
{
"error" : {
"root_cause" : [
{
"type" : "snapshot_restore_exception",
"reason" : "snapshot-repo:2022-11-10/X6hBmHdMSzip85q9RPBFMQ cannot restore index index-foo because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name" }
],
"type" : "snapshot_restore_exception",
"reason" : "snapshot-repo:2022-11-10/X6hBmHdMSzip85q9RPBFMQ cannot restore index index-foo because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name" },
"status" : 500
}
リストア先を別の名前にするには以下のようにリクエストする
この場合 <index名>-backup という名前でリストア先のIndexが作成される
code:console
POST /_snapshot/snapshot-repo/2022-11-10/_restore
{
"indices": "-.kibana*,-.opendistro_security"
"rename_pattern": "(.+)",
"rename_replacement": "$1-backup",
}
ダウンタイム無しでリストアするなら別名でリストアしアプリケーションから参照するindex名を別の物にするなどの対応が想定出来る
ダウンタイムありでリストアするなら既存の名前が重複するIndexを全て削除した上でリストアすればアプリケーションから参照する名前は変更する必要がない