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

发布时间:2025-12-15 点击:5
云计算
公司的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 $credentialexit}# 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 clinicwrite-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 yellowwrite-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来分类,就可以看见每个诊所的开支记录了

不能收到邮件-企业邮局
openEuler:打造多样性计算场景操作系统
ssl证书如何部署
金错刀拷问张峰:如何用小米模式找到电池的软肋?
租用的万网云服务器一直被攻击
电脑中360游戏大厅总是开机时自动启动如何关闭
阿里云服务器怎么配置代理
台湾天驱服务器云服务器区别