Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

AWS views bills through cost allocation tags

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The company's AWS runs a lot of different services, EC2,S3,VPC and so on, and they belong to more than 20 different clinics. The previous administrator did not do a very good Tag, so the bill at the end of each month can only see a large amount of total expenditure. For each service, it is difficult for each clinic to determine the specific expenditure. As a result, all the expenses come from the budget of the IT department, rather than apportioned to the actual clinics.

To solve this problem, you can customize the Tag tag for each service, then activate the custom tag in the Cost Allocation Tag console, and after a day, you can filter the query based on the self-defined tag in the billing bill.

For example, in order to distinguish between clinics, I defined a Tag,key that is Clinic,value, which is the name of each clinic.

Because I have hundreds of volume and thousands of Snapshot, Douzi wrote a simple PowerShell script to add tags. Manually add the corresponding tag on the EC2 instance, then add Tag according to the volume associated with EC2, and then associate snapshot with volume to add 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

After the execution, it is about like this. After confirming the work, just put him in the task plan and run automatically.

Then, 24 hours later, log in to the console of billing, categorize according to Tag, and you can see the expenditure records of each clinic.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report