1

I want to list down IpAddresses of EC2 Instances of a CloudFormation stack using PowerShell. I'm trying the below command but it is not returning IpAddress.

Get-CFNStackResourceList -StackName 'teststack' -LogicalResourceId 'EC2Instance' -region 'eu-west-1'

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
srinivasb
  • 55
  • 1
  • 7

1 Answers1

0

I suggest to check with

Basic example:

PS C:\> Get-EC2InstanceStatus -InstanceId i-12345678

AvailabilityZone : us-west-2a
Events           : {}
InstanceId       : i-12345678
InstanceState    : Amazon.EC2.Model.InstanceState
Status           : Amazon.EC2.Model.InstanceStatusSummary
SystemStatus     : Amazon.EC2.Model.InstanceStatusSummary

PS C:\> $status = Get-EC2InstanceStatus -InstanceId i-12345678
PS C:\> $status.InstanceState

Code    Name
----    ----
16      running

Then, collect all IPv4 addresses like this:

$EC2Instances = Get-EC2Instance

foreach($instance in $EC2Instances.Instances){
  $addresses = "";
  foreach($networkInterface in $instance.NetworkInterfaces){
    $addresses = $addresses, $networkInterface.PrivateIpAddresses.PrivateIpAddress -join ","
  }
  "$($instance.InstanceID): $($addresses.Trim(','))"    
}

Furthermore, it might be helpful to count the instances like this:

$filterRunning = New-Object Amazon.EC2.Model.Filter -Property @{Name = "instance-state-name"; Value = "running"}
$runningInstances = @(Get-EC2Instance -Filter $filterRunning)
# Count the running instances
$runningInstances.Count

See also: AWS Developer Blog - Scripting your EC2 Windows fleet using Windows PowerShell and Windows Remote Management

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • 1
    I found another way to solve my problem.. I made my CloudFormation to Output the Name of the Auto Scaling Group and I used the below command in the PowerShell Script to get the IP's of EC2 Instances. $iplist = (Get-ASAutoScalingInstance | ? {$_.AutoScalingGroupName -eq $ASGName} | select -expandproperty InstanceId | Get-EC2Instance | select -expandproperty RunningInstance ).PublicIpAddress – srinivasb Jun 01 '18 at 06:52