AWS 通过成本分配标签来查看账单

  • 来源:网络
  • 更新日期:2020-06-29

摘要:云计算 公司的AWS上运行了很多不同的服务,EC2,S3,VPC等等,他们属于20多个不同的诊所。之前的管理员并没有进行很好的Tag,因此每个月底的账单都

云计算

公司的AWS上运行了很多不同的服务,EC2,S3,VPC等等,他们属于20多个不同的诊所。之前的管理员并没有进行很好的Tag,因此每个月底的账单都只能看见一大堆总的开支,具体到每个服务,每个诊所很难确定具体的开支,结果就是所有的开支都从IT部门的预算走的,而不是分摊到实际的各个诊所上去。

为了解决这个问题,可以对每个服务都进行自定义的Tag标签,然后在Cost Allocation Tag的控制台里激活自定义的标签,一天之后,就可以在Billing账单里面根据自己定义的标签来过滤查询了。

比如说,为了区分诊所,我定义了一个Tag,key是Clinic,value就是每个诊所的名字了

因为我有上百个volume和上千个Snapshot,豆子写了个简单的PowerShell脚本来添加标签。EC2实例上手动添加了对应的标签,然后根据EC2关联的volume添加Tag,再通过volume来关联snapshot添加Tag

Write-Host Checking EC2 instance Tags status -ForegroundColor Yellow

$all=Get-EC2Instance | select -expand instances

$return=$all | Where-Object {$_.tag.key -notcontains Clinic}

if($return -ne $null){
$username = example@aa.com 
$password = password | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username,$password)

Send-MailMessage -From example@aa.com -to example@bb.com -SmtpServer smtp.office365.com -Port 587 -UseSsl -Subject EC2 instance Tag -Credential $credential
exit

}
# confirm EC2 instances were tagged

$result=@()
foreach($item in $all){

    $Name=$item.tag | Where-Object {$_.Key -eq \'Name\'} | select -ExpandProperty value
    $clinic=$item.tag | Where-Object {$_.Key -eq \'clinic\'} | select -ExpandProperty value
    $item | add-member -NotePropertyName Description -NotePropertyValue $name
    $item | add-member -NotePropertyName Clinic -NotePropertyValue $clinic

    $item = $item | select *
    $result+=$item

}

$result | select Description, InstanceId, privateIpaddress, Clinic | Group-Object Clinic

write-host Updating Volume Tags Status ...  -ForegroundColor Yellow 
#Tag all volumes based on their attached EC2 Clinic Tag

$allvol=Get-EC2Volume | Where-Object {$_.tag.key -notcontains Clinic}

foreach($item in $result){
    foreach($item2 in $allvol){

        if ($item2.attachments.instanceid -eq $item.InstanceId){
                $value=$item.Clinic
              New-EC2Tag -Resource $item2.VolumeId -Tag @{Key=Clinic;value=$value} 
           }

        }

}

write-host Done ! -ForegroundColor Yellow

Write-Host Updating Snapshot Tags Status... -ForegroundColor Yellow 
#Tag all snapshots based on the volume Tag
$allvol=Get-EC2Volume 
$filter= New-Object Amazon.EC2.Model.Filter -Property @{Name = owner-id; Values =\'xxxxxxx\' } 
$snapshots=Get-EC2Snapshot -Filter $filter 

$snapshots= $snapshots | ? {$_.Tag.key -notcontains Clinic} 

foreach($i in $snapshots){
    $volid=$i.VolumeId

    foreach($j in $allvol){

        if($volid -eq $j.Volumeid){

            $value=$j.tag | Where-Object {$_.key -eq \'Clinic\'} | select -ExpandProperty value

            $name=$j.Tag | Where-Object {$_.key -eq Name} | select -ExpandProperty value

            $snapid=$i.snapshotid
            $snapid
            New-EC2Tag -Resource $snapid -Tag @{Key=Clinic;value=$value} 
            New-EC2Tag -Resource $snapid -Tag @{Key=Name;value=$name}

        }
    }

}

write-host Done ! -ForegroundColor Yellow

执行之后大概是这样, 确认工作之后把他放到任务计划里面自动跑就行了

然后过了24小时,登录billing的控制台,根据Tag来分类,就可以看见每个诊所的开支记录了

新网虚拟主机