Last active
December 13, 2019 07:58
-
-
Save giseongeom/9e6d80cf70c89875f7cd61a25402ed7f to your computer and use it in GitHub Desktop.
Export EC2 status to .CSV across AWS region(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # prepare credential | |
| Set-AWSCredential -ProfileName **** | |
| $env:AWS_PROFILE='****' | |
| $ts_begin = get-date | |
| Function Get-EC2PeakCPU ($region, $instanceId) { | |
| # Last 7 Days (1w) | |
| $cw_data = Get-CWMetricStatistic -Region $region -MetricName CPUUtilization ` | |
| -UTCStartTime (GET-DATE).AddDays(-7).ToUniversalTime() -UTCEndTime (Get-DATE).ToUniversalTime() ` | |
| -Statistic Average -Namespace "AWS/EC2" -Period 420 ` | |
| -Dimension @{Name="InstanceId"; Value="$instanceId"} | |
| $computed_output = ($cw_data.Datapoints | Measure-Object -Maximum -Property Average).Maximum | |
| if ($computed_output) { return "{0:N1}" -f $computed_output } | |
| } | |
| Function Export-EC2Data ($region, $ec2data) { | |
| # get EC2 data Per region | |
| $my_ec2 = @() | |
| $ss = $ec2data | |
| $ss | % { | |
| $instance = $_ | |
| $tags = @{} | |
| if ($instance.Tags.count -gt 0) { | |
| $json = $instance.Tags | ConvertTo-Json | |
| $fromjson = ConvertFrom-Json $json | |
| $fromjson | ForEach-Object { | |
| $h = $_ | |
| $tags[$h.Key] = $h.Value | |
| } | |
| } | |
| $ec2 = New-Object psobject | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name AWS_Region -Value $region | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name Name -Value $tags['Name'] | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name InstanceId -Value $instance.InstanceId | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name InstanceType -Value $instance.InstanceType | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name LaunchTime -Value $instance.LaunchTime | |
| $ec2_vm_uptime = "{0:N0}" -f ((Get-Date) - $instance.LaunchTime).TotalHours | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName int -Name 'LaunchTime(Hour)' -Value $ec2_vm_uptime | |
| $ec2_cpu_util = Get-EC2PeakCPU -region $region -instanceId $instance.InstanceId | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName int -Name 'Peak CPU(%)/Last 1 week' -Value $ec2_cpu_util | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name Owner -Value $tags['Owner'] | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name AutoScalingGroupName -Value $tags['aws:autoscaling:groupName'] | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name KubernetesCluster -Value $tags['KubernetesCluster'] | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name Environment -Value $tags['Environment'] | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name Terraform -Value $tags['Terraform'] | |
| #Remove *processed* Tags | |
| if ($tags.Contains('Name')) { $tags.Remove('Name') } | |
| if ($tags.Contains('Owner')) { $tags.Remove('Owner') } | |
| if ($tags.Contains('aws:autoscaling:groupName')) { $tags.Remove('aws:autoscaling:groupName') } | |
| if ($tags.Contains('KubernetesCluster')) { $tags.Remove('KubernetesCluster') } | |
| if ($tags.Contains('Environment')) { $tags.Remove('Environment') } | |
| if ($tags.Contains('Terraform')) { $tags.Remove('Terraform') } | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name 'Tags(Key)' -Value ($Tags.keys -join '|') | |
| $ec2 | Add-Member -MemberType NoteProperty -TypeName string -Name 'Tags(Value)' -Value ($Tags.values -join '|') | |
| $my_ec2 += $ec2 | |
| } | |
| $my_ec2 | Export-Csv -NoTypeInformation -Path $PSScriptRoot/ec2-$region.csv | |
| If (Test-Path $PSScriptRoot\ec2-all.csv) { | |
| $my_ec2 | Export-Csv -NoTypeInformation -Path $PSScriptRoot\ec2-all.csv -Append -Force | |
| } | |
| } | |
| Write-Output "`n" | |
| $ec2_count = 0 | |
| $ec2_count_total = 0 | |
| If (Test-Path $PSScriptRoot\ec2-all.csv) { | |
| Remove-Item $PSScriptRoot\ec2-all.csv -ErrorAction SilentlyContinue -Force | out-null | |
| New-Item $PSScriptRoot\ec2-all.csv -ErrorAction SilentlyContinue -Force | out-null | |
| } else { | |
| New-Item $PSScriptRoot\ec2-all.csv -ErrorAction SilentlyContinue -Force | out-null | |
| } | |
| $AWS_region = aws ec2 describe-regions --all-regions --output json | jq .Regions | convertfrom-json | |
| $AWS_region | ? { $_.OptInStatus -eq 'opt-in-not-required' } | Sort-Object -Property RegionName | | |
| Select-Object -ExpandProperty RegionName | % { | |
| $region = $_ | |
| $ss = (Get-EC2Instance -Region $region).Instances | ? { $_.State.Name -eq 'Running' } | |
| $ec2_count = ($ss | Measure-Object).Count | |
| $ec2_count_total += $ec2_count | |
| if ($ec2_count -gt 0) { | |
| Write-Output "AWS_Region $region : $ec2_count" | |
| Export-EC2Data -region $region -ec2data $ss | |
| } | |
| } | |
| $ts_end = get-date | |
| $ts_duration = "{0:N1}" -f ($ts_end - $ts_begin).TotalSeconds | |
| Write-Output "`n" | |
| Write-Output "EC2 (Total): $ec2_count_total" | |
| Write-Output "Elasped Time (sec): $ts_duration" |
Author
Author
실제 데이터에서 발췌
"AWS_Region","Name","InstanceId","InstanceType","LaunchTime","LaunchTime(Hour)","Peak CPU(%)/Last 1 week","Owner","AutoScalingGroupName","KubernetesCluster","Environment","Terraform","Tags(Key)","Tags(Value)"
"ap-northeast-2","hello-china","i-0b6519","m4.2xlarge","11/27/2018 4:19:30 PM","9,145","37.0","DTeam",,,,"true","",""
"ap-southeast-1","hello-prod-asg","i-0b7c5bea","m4.5xlarge","11/9/2019 5:21:34 AM","828","14.7",,"autoscaling-group",,,,"",""
~생략~
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
실행결과