Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

AWS EC2 CPU metric report using Powershell

Posted on September 13, 2019February 27, 2022

Amazon CloudWatch is cloud based monitoring solution by Amazon. It provides with data and actionable insights to monitor applications, respond to system-wide performance changes and optimize resource utilization. It has many more features apart from those I just mentioned. Please refer to AWS documentation for more details.

CloudWatch provides you with options to create nice graphs based on numerous performance metrics for different resource types in your environment. It also provides you options to save those different graph widgets in Dashboard , which you can share with different stakeholders.

However, recently I came across a requirement where my customer wanted to dump CloudWatch performance metric data for our EC2 Instances in a CSV file. They wanted to use the raw data for further processing and presentations. AWS provides AWS tools for Powershell and a list of APIs for CloudWatch. Refer to AWS documentation to know more about all different APIs that AWS provides for Powershell.

Following script provides EC2 Instance Percentage CPU Utilization metric data. I am using Get-CWMetricStatistic with different parameter options. Refer API guide document provided above to get full details. I am using 4 hours interval for data points.You may use whatever you prefer, based on the Start and End date and time you set for time range. AWS provides metric data for a range of services. You can get the list of metric namespaces with Get-CWMetricList command as shown below. It will provide the list of namespaces based on deployed resources in the account.

Each namespace has a list of available metrics. These metrics will be used to generate reports based on different dimensions.

As my requirement was to generate a report of CPU Utilization for each EC2 Instance, I am also using Get-EC2Instance command to get the list of EC2 Instances.

To run this script you need to install AWS Tools for Powershell and configure AWS credentials. I am assuming you have already set up your system. If you still have any issue, refer following documents by Amazon :

  • https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html
  • https://aws.amazon.com/powershell/

Download the script provided below and run it with required parameters. For example – ./Generate-EC2PerformanceMetricData.ps1 -startTime “08/01/2019 0:00 AM” -endTime “09/01/2019 11:00 PM”

Param(
    [Parameter(Mandatory=$true)]
    [String]$startTime,
    [Parameter(Mandatory=$true)]
    [String]$endTime
)

$sTime = Get-Date $startTime
$eTime = Get-Date $endTime

$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\EC2_CPU_MetricData.csv"
$tFile = "$($currentDir)\EC2_CPU_MetricData_temp.csv"
if(Test-Path $oFile){Remove-Item $oFile -Force}
if(Test-Path $tFile){Remove-Item $tFile -Force}
"Instance Name,Instance Id, TimeStamp, %CPUUtilization" | Out-File $tFile -Append -encoding ASCII

Get-EC2Instance | ForEach-Object{
    $instance = $instanceId = $insTags = $instanceName = ""
    $instanceId = $_.Instances.InstanceId
    $insTags = $_.Instances.Tags
    if($insTags.Key -eq "Name"){$instanceName =  $insTags | Where-Object { $_.Key -eq "Name" } | Select-Object -expand Value}
    $instance = New-Object Amazon.CloudWatch.Model.Dimension
    $instance.set_Name("InstanceId")
    $instance.set_Value($instanceId)
    $Data = Get-CWMetricStatistic -Namespace AWS/EC2 -MetricName CPUUtilization -StartTime $sTime -EndTime $eTime -Period 14400 -Statistics @("Average") -Dimensions $instance
    ForEach($d in $Data.Datapoints){
        $timeStamp = $utlValue = ""
        $timeStamp = $d.TimeStamp
        $utlValue = $d.Average
        "$instanceName,$instanceId,$timeStamp,$utlValue" | Out-File $tFile -Append -encoding ASCII
    }
}

Import-Csv $tFile | Sort-Object 'Instance Name','Instance Id', 'TimeStamp' | Export-Csv -Path $oFile -NoTypeInformation
if(Test-Path $tFile){
    Remove-Item $tFile -Force
}

Above script will generate a formatted CSV file with CPU utilization data for all EC2 instances in the AWS Account.

Let me know if you face any issue while running this script or any suggestion to improve it. Happy Scripting!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes