温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-29 04:51:04 来源:网络 阅读:3545 作者:beanxyz 栏目:云计算

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

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

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

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

因为我有上百个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 

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

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

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI