AWSCLIでタグ付きEBSスナップショットを取得する

2018.04.03

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

概要

これまでawscliを使ってタグを付与したスナップショットを作成する際、create-snapshot(タグなし)で スナップショットを作成し、create-tagsでタグを作成/付与する形でした。 昨日のアップデートにてタグを付与した形でスナップショットを取得出来るようになりましたので試してみます。

試してみる

AWSCLIをアップデートする

% sudo pip install --upgrade awscli

<省略>

% aws --version
aws-cli/1.14.69 Python/3.5.2 Darwin/15.6.0 botocore/1.9.22

create-snapshotコマンドを確認してみる

awscliバージョンアップ前

% aws --version
aws-cli/1.14.15 Python/3.5.2 Darwin/15.6.0 botocore/1.8.19

% aws ec2 create-snapshot help
SYNOPSIS
create-snapshot
[--description ]
--volume-id
[--dry-run | --no-dry-run]
[--cli-input-json ]
[--generate-cli-skeleton ]

awscliバージョンアップ後(1.14.69)

% aws --version
aws-cli/1.14.69 Python/3.5.2 Darwin/15.6.0 botocore/1.9.22

% aws ec2 create-snapshot help
SYNOPSIS
create-snapshot
[--description ]
--volume-id
[--tag-specifications ]
[--dry-run | --no-dry-run]
[--cli-input-json ]
[--generate-cli-skeleton ]

--tag-specificationsが増えてました。 このオプションを使用してコマンドを実行してみます。

% aws ec2 create-snapshot --volume-id vol-xxxxx --tag-specification 'ResourceType="snapshot",Tags=[{Key="Name",Value="TESTSS"}]' --description "add tag snapshot"
{
"Tags": [
{
"Value": "TESTSS",
"Key": "Name"
}
],
"State": "pending",
"VolumeSize": 8,
"VolumeId": "vol-xxxxx",
"SnapshotId": "snap-yyyy",
"OwnerId": "xxxxxxxxxxxx",
"Encrypted": false,
"Progress": "",
"StartTime": "2018-04-03T04:33:53.000Z",
"Description": "add tag snapshot"
}
% aws ec2 describe-snapshots
{
"Snapshots": [
{
"StartTime": "2018-04-03T04:33:53.000Z",
"Encrypted": false,
"State": "completed",
"VolumeId": "vol-xxxxx",
"Progress": "100%",
"OwnerId": "xxxxxxxxxxxx",
"Description": "add tag snapshot",
"Tags": [
{
"Value": "TESTSS",
"Key": "Name"
}
],
"SnapshotId": "snap-yyyy",
"VolumeSize": 8
},
{
"StartTime": "2018-04-03T02:33:18.000Z",
"Encrypted": false,
"State": "completed",
"VolumeId": "vol-xxxxx",
"Progress": "100%",
"OwnerId": "xxxxxxxxxxxx",
"Description": "notag snapshot",
"SnapshotId": "snap-yyyy",
"VolumeSize": 8
}
]
}

無事、スナップショットにタグが付与されました。

本記事を書くにあたり、検証時にResourceTypeは何を指定した良いか把握しておらず、 volumeやinstanceなどを叩いてましたが冷静に考えてsnapshotと気づきました。(どうかしてました) その時のエラーは以下のようになります。

% aws ec2 create-snapshot --volume-id vol-xxxxx --tag-specification 'ResourceType=volume,Tags=[{Key=Name,Value=TESTSS}]' --description "add tag snapshot"
An error occurred (InvalidParameterValue) when calling the CreateSnapshot operation: 'volume' is not a valid taggable resource type for this operation.

最後に

これまでcreate-tagsというステップを経て取得できていたスナップショットがすんなり取得できるようになりました。 劇的に運用が変わるものではありませんが、なるべく一つのコマンドで完結できる点でawscliを利用する選択肢が少しでも増えると嬉しいなと思いました。

参考リンク

create-snapshot