0
0

More than 3 years have passed since last update.

EBSスナップショットの作成時間の統計方法

Posted at

AMIの作成時間を測定したいので、色々調べて見た。

  1. AWSにはAMI作成に関してイベントが提供なし
  2. スナップショットを作成イベント (createSnapshots)より時間を測定できれば、それでもOK、SnapshotからAMIを作成の時間を短いですから

アーキテクチャ

file

下記はcreateSnapshotsイベントから着手、1から説明する

SNSトピックとサブスクリプション作成

snapshot-topic トピック作成
file
ARNをメモしておく(arn:aws:sns:ap-northeast-1:123456789012:snapshot-topic)

受信メールアドレスを記入して、サブスクリプション作成
file

サブスクリプションからの確認メールからリンクをクリックして、SNSの配置完成
file

Lambda関数作成

lambda-snapshotのLambda関数作成

file
注意点:
LambdaからSNSを呼び出すので、SNS発行可能なロールを新規作成。

ソースは下記となり。
```python
import boto3

def lambda_handler(event, context):
result = event['detail']['result']
snapshotId = ",".join(event['resources'])
startTime = event['detail']['startTime']
endTime = event['detail']['endTime']

client = boto3.client('sns')

sns_response = client.publish(
    TopicArn="arn:aws:sns:ap-northeast-1:123456789012:snapshot-topic",
    Message=('SNS のスナップショットが作成されました。\n詳細は以下です。\n\n結果:'
    + result
    + '\nスナップショットID:'
    + snapshotId
    + '\n作成開始時間:'
    + startTime
    + '\n作成終了時間:'
    + endTime
    + '\n\n'),
    Subject='demo-SnapshotCreateTime'
)

return sns_response
## CloudWatchEventsを設定

注意:イベントタイプは下記に選択
**EBS Multi-Volume snapshots completion status**

![](https://cloud5.jp/wp-content/uploads/2020/05/キャプチャ.jpg)

JSON
```json
{
  "source": [
    "aws.ec2"
  ],
  "detail-type": [
    "EBS Multi-Volume Snapshots Completion Status"
  ],
  "detail": {
    "event": [
      "createSnapshots"
    ],
    "result": [
      "succeeded",
      "failed"
    ]
  }
}

file

file

試してみる

EC2配下、
ELASTIC BLOCK STORE配下の「スナップショットの作成」をクリック

作成タイプはインスタンスを選択する
file

スナップショット作成成功
file

スナップショット作成時間メールを届けた
file

最後に

この記事が誰かのお役に立てば幸いです

参考資料

AMIの作成時間が知りたかったのでSNS Topicで通知させてみた
https://dev.classmethod.jp/articles/ami-create-time-sns/
説明の方法にはAMIの作成時間を測定できなく、一つEBSのスナップショットのみ測定しかない。

EBS スナップショットイベント
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ebs-cloud-watch-events.html#snapshot-events

create-snapshots CLIヘルパー
https://docs.aws.amazon.com/cli/latest/reference/ec2/create-snapshots.html

スナップショットを作成 (createSnapshots)
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ebs-cloud-watch-events.html#create-snapshots-complete

0
0
0

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
0