Search code examples
aws-powershell

How to get an ec2 instance id from instance name


i want to retrieve an ec2 instance's ID from it's name which I have set as the value of it's tag. I have a VM with key as 'Name' and value as 'testvm1'. Is there something like

(get-ec2 instance id) | where-object {$_.(key.value) - eq "testvm1"}


Solution

  • this worked

     $ec2Name = New-Object Amazon.EC2.Model.Filter -Property @{Name = "tag:Name"; Values = "testvm1"}
        $instances = @(Get-EC2Tag -Filters $ec2Name) 
          $instances | Select-Object -ExpandProperty resourceid