如何在用户数据中获取 AWS 实例标签? [英] How can I get AWS Instance Tags in user-data?
问题描述
我将 aws ec2 userdata 与 windows powershell 脚本一起使用.我需要实例引导.我的想法是:
I use aws ec2 userdata with windows powershell scripts. I need instance bootstrapping. My idea is:
- 添加EC2实例标签.键名Version",值1.0.0.158-branchname"
我试图在用户数据中获取版本标记值.我检查了 aws http api.它不能返回标签.我写了简单的 powershell 脚本:
I have tried to get version tag value in userdata. I checked aws http api. It can't return tags. I wrote simple powershell scripts:
$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 用户数据无法启动aws.exe".
I can get instance Id with aws http api. I can't get instance tags because AWS ec2 userdata can't start "aws.exe".
这个脚本是正确的——它是在实例启动之前手动运行的.
This script is correct - it is manually run before the instance is started.
注意:aws"是aws.exe"(https://aws.amazon.com/cli/)
Note: "aws" is "aws.exe" (https://aws.amazon.com/cli/)
推荐答案
我解决了,我的问题是使用 aws cli .我使用 powershell api 并修复它.
I resolve it, my problem is using aws cli . I use powershell api and fix it.
$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'}
这篇关于如何在用户数据中获取 AWS 实例标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!