Powershell 如何在用户数据中获取AWS实例标记?

Powershell 如何在用户数据中获取AWS实例标记?,powershell,amazon-web-services,amazon-ec2,user-data,aws-powershell,Powershell,Amazon Web Services,Amazon Ec2,User Data,Aws Powershell,我将aws ec2用户数据与windows powershell脚本一起使用。我需要实例引导。我的想法是: EC2实例标记添加了。它的键名为“Version”,值为“1.0.0.158-branchname” 我已尝试在userdata中获取版本标记值。我检查了aws http api。它不能返回标签。我编写了简单的powershell脚本: $instanceId = (New-Object System.Net.WebClient).DownloadString("http://169.

我将aws ec2用户数据与windows powershell脚本一起使用。我需要实例引导。我的想法是:

  • EC2实例标记添加了。它的键名为“Version”,值为“1.0.0.158-branchname”
我已尝试在userdata中获取版本标记值。我检查了aws http api。它不能返回标签。我编写了简单的powershell脚本:

$instanceId = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
aws ec2 describe-tags --filters $filter --query 'Tags[*]'
我可以通过aws http api获取实例Id。我无法获取实例标记,因为AWS ec2 userdata无法启动“AWS.exe”

此脚本是正确的-它是在实例启动之前手动运行的


注意:“aws”是“aws.exe”(

我解决了它,我的问题是使用aws cli。我使用powershell api并修复它

$instanceId = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
$versionTag =  Get-EC2Tag | ` Where-Object {$_.ResourceId -eq $instanceId -and $_.Key -eq 'Version'}

只是一些澄清,因为我面临着类似的问题

如果实例具有具有EC2读取访问权限的IAM角色

# My need to use AWSDefaults cmdlet to get temp credentials from STS
Initialize-AWSDefaults
$instanceId = irm -uri http://169.254.169.254/latest/metadata/instance-id
$instance = (Get-Instance -InstanceId $instanceId).Instances[0]
$instanceName = ($instance.Tags | ? { $_.Key -eq "Name"} | select -expand Value)