Generate an EC2 instance report using PowerShell…

In AWS, under the EC2 instances view, you can customize the details you want to see about your EC2 instances, however once you do that there is no way of generating a CSV based EC2 instance report using the AWS UI. Plus you don’t have the option of displaying properties such as the VPC or Subnet name – which is a little annoying as working with long ids can get difficult. To make the reporting process a little easier, I put together the script below – which generates a CSV based report.

NOTE – The resulting CSV report is extremely detailed – you can simply remove properties you feel are unnecessary for your reporting purposes.

Before you can run this script install the AWS PowerShell Modules.

 Install-Module -Name AWSPowerShell

Then I would recommend creating an access key profile in the SDK store – by running the following in PowerShell.

Set-AWSCredentials -AccessKey AccessKey -SecretKey SecretKey -StoreAs ProfileName

And to make things easier maybe associate a region with that profile.

Initialize-AWSDefaultConfiguration -ProfileName ProfileName -Region eu-central-1

Report script:

$ec2 = Get-EC2Instance -ProfileName $StoredAWSCredentials

$ec2list = $ec2.Instances

$ec2listdetails = $ec2list | ForEach-Object {
    $properties = [ordered]@{
    Name = ($_ | Select-Object -ExpandProperty tags | Where-Object -Property Key -eq Name).value
    InstanceState = $_.State.Name
    InstanceID = $_.InstanceId
    InstanceType = $_.InstanceType
    Platform = $_.Platform
    LaunchTime = $_.launchtime
    KeyName = $_.KeyName
    AmiID = $_.ImageID
    ImageName = (Get-EC2Image -ImageId $_.ImageID).Name
    PrivateIP = $_.PrivateIpAddress
    SubnetId = $_.SubnetId
    SubnetName = (Get-EC2Subnet -subnetid ($_.SubnetId) | Select-Object -ExpandProperty tags | 
    Where-Object -Property Key -eq Name).value
    NetworkInterfaceId = $_.networkinterfaces.networkinterfaceid
    MAC = $_.networkinterfaces.MacAddress
    VPCId = $_.VpcId
    VPCName = (Get-EC2VPC -vpcid ($_.vpcId) | Select-Object -ExpandProperty tags | Where-Object - 
    Property Key -eq Name).value
    AZ = $_.placement.AvailabilityZone
    SG = $_.SecurityGroups.GroupName
    BackupTag = ($_ | Select-Object -ExpandProperty tags | Where-Object -Property Key -eq 
    BackupTag).value
    }
    New-Object -TypeName PSObject -Property $properties
}

$ec2listdetails | Sort-Object -Property SubnetName | Export-Csv -Path c:\temp\ec2-report.csv

One thought on “Generate an EC2 instance report using PowerShell…

  1. mbourgon

    DUDE! Thanks for this. Had been trying to figure out how to use describe Get-EC2Instanceattribute and this gives me all the info I need. Had no idea that I could just use get-ec2instance. Appreciate it a ton!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.