例としてNameタグが付いていないEC2、EBS、AMI、EBSのスナップショット、それぞれのIDを取得するAWS CLIコマンドを備忘として記す。
EC2
aws ec2 describe-instances --query 'Reservations[].Instances[?!not_null(Tags[?Key == `Name`])] | [].[InstanceId]' --output text
EBS
aws ec2 describe-volumes --query 'Volumes[?!not_null(Tags[?Key == `Name`].Value)] | [].[VolumeId]' --output text
AMI
aws ec2 describe-images --query 'Images[?!not_null(Tags[?Key == `Name`])] | [].[ImageId]' --owners self --output text
EBS Snapshot
aws ec2 describe-snapshots --query 'Snapshots[?!not_null(Tags[?Key == `Name`])] | [].[SnapshotId]' --owner-ids self --output text
AMIとスナップショットは自分自身が保有するリソース以外も対象となるため、それぞれのオプションでselfを指定する必要がある。
PowerShellシェルスクリプト
WindowsサーバからNameタグが付いていない一覧をメール送信するシェルスクリプトを作成したので、備忘として記載しておく。
Function NullCheck ( $CheckId ) {
if( [string]::IsNullOrEmpty( $CheckId ) ) {
$TempBody = "None"
return $TempBody
}
else {
return $CheckId
}
}
Function SetMailBody( $ResId , $Ec2Info ) {
[int]$Index = 0
$ResIdArr = $ResId.split( " " )
$Ec2InfoArr = $Ec2Info.split( "," )
$Ec2InfoArr = $Ec2InfoArr.Replace( " " , "" )
while( $ResIdArr[$Index] ) {
$TempBody += $ResIdArr[$Index] + "`t" + $Ec2InfoArr[$Index] + "`r`n"
$Index++
}
return $TempBody
}
Function GetEc2NameFromVolId( $VolId ) {
$VolIdArr = $VolId.split( " " )
[Array]$TempBody = @()
foreach( $TempVolId in $VolIdArr ) {
$Ec2Id = aws ec2 describe-volumes --volume-id $TempVolId --query 'Volumes[*].Attachments[].InstanceId' --output text
if( ( $LASTEXITCODE -ne 0 ) -or ( [string]::IsNullOrEmpty( $Ec2Id ) ) ) {
$TempBody += "インスタンス情報無し"
}
else {
$Ec2Name = aws ec2 describe-instances --instance-ids $Ec2Id --query 'Reservations[*].Instances[].Tags[?Key==`Name`] |[].[Value]' --output text
if( [string]::IsNullOrEmpty( $Ec2Name ) ) {
$TempBody += $Ec2Id
}
else {
$TempBody += $Ec2Name
}
}
$TempBody += ","
}
return $TempBody
}
Function GetVolIdFromSnapId( $SnapId ) {
$SnapIdArr = $SnapId.split( " " )
foreach( $TempSnapId in $SnapIdArr ) {
$VolId += aws ec2 describe-snapshots --snapshot-ids $TempSnapId --query 'Snapshots[*].VolumeId' --output text
$VolId += " "
}
$Ec2Info = GetEc2NameFromVolId $VolId
return $Ec2Info
}
Function GetEc2Id {
$FuncEc2Id = aws ec2 describe-instances --query 'Reservations[].Instances[?!not_null(Tags[?Key == `Name`])] | [].[InstanceId]' --output text
[String]$Result = NullCheck $FuncEc2Id
$TidyResult = $Result.Replace( " " , "`r`n" )
return $TidyResult
}
Function GetVolId {
$FuncVolId = aws ec2 describe-volumes --query 'Volumes[?!not_null(Tags[?Key == `Name`].Value)] | [].[VolumeId]' --output text
[String]$Result = NullCheck $FuncVolId
if( $Result -ne "None" ) {
$Result = GetEc2NameFromVolId $Result
$Result = SetMailBody $FuncVolId $Result
}
return $Result
}
Function GetAmiId {
$FuncAmiId = aws ec2 describe-images --query 'Images[?!not_null(Tags[?Key == `Name`])] | [].[ImageId]' --owners self --output text
[String]$Result = NullCheck $FuncAmiId
$TidyResult = $Result.Replace( " " , "`r`n" )
return $TidyResult
}
Function GetSnapId {
$FuncSnapId = aws ec2 describe-snapshots --query 'Snapshots[?!not_null(Tags[?Key == `Name`])] | [].[SnapshotId]' --owner-ids self --output text
[String]$Result = NullCheck $FuncSnapId
if( $Result -ne "None" ) {
$Result = GetVolIdFromSnapId $Result
$Result = SetMailBody $FuncSnapId $Result
}
return $Result
}
$FromAdd = "no-reply <Administrator@example.com>"
$ToAdd = @( "ec2-user@example.com" , "ec2-user2@example.com" )
$Subject = "【AWS】NullNameTagList"
$Body = ""
$Port = "25"
$SmtpSrv = "SMTP.example.com"
$Encoding = [System.Text.Encoding]::UTF8
$Body += "EC2`r`nリソースID`r`n"
$Body += GetEc2Id
$Body += "`r`n`r`n"
$Body += "EBS`r`nリソースID`tインスタンス情報`r`n"
$Body += GetVolId
$Body += "`r`n"
$Body += "AMI`r`nリソースID`r`n"
$Body += GetAmiId
$Body += "`r`n`r`n"
$Body += "EBS Snapshot`r`nリソースID`tインスタンス情報`r`n"
$Body += GetSnapId
Send-MailMessage -From $FromAdd -To $ToAdd -Subject $Subject -Body $Body -Port $Port -SmtpServer $SmtpSrv -Encoding $Encoding