Using the AWS CLI on Windows with Powershell
Overview
Building on the lessons from AWS - Configure the AWS CLI with SSO and multiple profiles, we can perform similar queries from a Windows machine with the AWS CLI using Powershell to extract similar instance metadata.
How-to
- Set the Windows Powershell environment variable for the profile we wish to use:
$Env:AWS_PROFILE="dev-account.developer"
- Test that the SSO profile has works as expected:
PS> aws ec2 describe-instances
- Filter the output of
aws ec2 describe-instances
:PS> aws ec2 describe-instances | ConvertFrom-Json | ` Select-Object -ExpandProperty Reservations | ForEach-Object { $_.Instances | ` ForEach-Object { $tag = ($_.Tags | Where-Object { $_.Key -eq 'aws:cloud9:owner' }); ` if ($tag) { [PSCustomObject]@{ "Instance ID" = $_.InstanceId; "Tag aws:cloud9:owner" = $tag.Value; "Instance State" = $_.State.Name } } } }
- Review the output:
Instance ID Tag aws:cloud9:owner Instance State ----------- -------------------- -------------- i-034e459b55d574564 A0880D35011EEA187F057:bob.typeytype stopped i-0af34b3e0f9fecc9e A0880D35011EEA187F057:alan.syscall running
- Optionally, start the desired instance:
PS> aws ec2 start-instances --instance-ids i-034e459b55d574564
- And connect via SSH or SSH over SSM:
PS> aws ssm start-session --target i-034e459b55d574564
Alternative Queries
As an alternative to Powershell, it’s also straightforward to select/filter specific instance data using
jq
:aws ec2 describe-instances | jq -r '.Reservations[].Instances[] | "\(.InstanceId),\(.Tags[] | select(.Key == "SERVERNAME").Value),\(.Tags[] | select(.Key == "aws:cloudformation:logical-id").Value)"' i-034e459b55d574564,VM01,AppInstance i-0af34b3e0f9fecc9e,VM02,DatabaseInstance