Following definition of Amazon EC2 instance is taken from AWS documentation. Please refer here for more information – “Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) cloud. Using Amazon EC2 eliminates your need to invest in hardware up front, so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic.”
The script I am providing below will help you to generate a quick inventory of EC2 instances deployed in your Account. I tried to include all those properties you might need as part of any standard EC2 inventory. Feel free to add your stuff as require. This script assumes you have already setup your AWS credentials and AWS Powershell SDK to run this script on your working system. If you face any issue setting those, please refer following documents to set those:
- https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html
- https://aws.amazon.com/powershell/
Let me know if this is helpful!
$date = Get-Date -UFormat "%m-%d-%Y"
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\EC2_Details_$($date).csv"
if(Test-Path $oFile){Remove-Item $oFile -Force}
"EC2 Name,Architecture,Platform,Image Id,Instance Id,Instance Type,Launch Time, Power State, Private IP Address, Private DNS Name,Public IP Address, Public DNS Name,Root Device Type" | Out-File $oFile -Append -Encoding ascii
Get-EC2Instance | Select-Object Instances -ExpandProperty Instances | ForEach-Object{
$ec2LogicalName = $instanceId = $imageId = $tags = $prvIPAddress = $prvDNSName = $platform = $launchTime = $powerState = $pubIPAddress = $pubDNSName = $instanceType = $architecture = $rootDevType = ""
$instanceId = $_.InstanceId
$imageId = $_.ImageId
$instanceType = $_.InstanceType
$tags = $_.Tags
$architecture = $_.Architecture
$launchTime = $_.LaunchTime
if(!([string]::IsNullOrEmpty($tags))){
if($tags.Key -eq "Name"){$ec2LogicalName = $tags | Where-Object { $_.Key -eq "Name" } | Select-Object -expand Value}
}
$prvIPAddress = $_.PrivateIpAddress
$prvDNSName = $_.PrivateDnsName
$pubIPAddress = $_.PublicIpAddress
$pubDNSName = $_.PublicDnsName
$platform = $_.Platform
$powerState = $_.State.Name
$rootDevType = $_.RootDeviceType
"$ec2LogicalName,$architecture,$platform,$imageId,$instanceId,$instanceType,$launchTime,$powerState,$prvIPAddress,$prvDNSName,$pubIPAddress,$pubDNSName,$rootDevType" | Out-File $oFile -Append -Encoding ascii
}
Exit
Before running aws scripts, set up your aws credentials following provided documentation above.
Download above script and save it with a .ps1 file extension.After that, open aws powershell sdk tool console. Once, it is open run the script saved previously.
Once the script execution completes, it will generate a CSV file output like below screenshot: