【问题标题】:How to get EC2 Instances IpAddress of a cloudformation stack using PowerShell?如何使用 PowerShell 获取 cloudformation 堆栈的 EC2 实例 IpAddress?
【发布时间】:2018-10-01 20:35:21
【问题描述】:

我想使用 PowerShell 列出 CloudFormation 堆栈的 EC2 实例的 IpAddress。我正在尝试以下命令,但它没有返回 IpAddress。

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

【问题讨论】:

  • 太棒了。赞赏。

标签: powershell amazon-ec2 amazon-cloudformation aws-powershell


【解决方案1】:

我建议检查

基本示例:

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

然后,像这样收集所有 IPv4 地址:

$EC2Instances = Get-EC2Instance

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

此外,计算 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

另请参阅:AWS 开发人员博客 - Scripting your EC2 Windows fleet using Windows PowerShell and Windows Remote Management

【讨论】:

  • 我找到了另一种解决问题的方法。我让 CloudFormation 输出 Auto Scaling 组的名称,并在 PowerShell 脚本中使用以下命令来获取 EC2 实例的 IP。 $iplist = (Get-ASAutoScalingInstance | ? {$_.AutoScalingGroupName -eq $ASGName} | 选择 -expandproperty InstanceId | Get-EC2Instance | 选择 -expandproperty RunningInstance ).PublicIpAddress
猜你喜欢
  • 2016-08-31
  • 2014-12-29
  • 2016-07-13
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2018-11-14
  • 1970-01-01
相关资源
最近更新 更多