1 個回答
- 最新
- 最多得票
- 最多評論
2
Yes, the "Name" tag value can be set dynamically.
Example:
I can see you are having Hybrid managed instances so as an example,
If you want to set Hostname of the instance as the Tag value, you can use below Shell or PowerShell scripts which will pull out the hostname and create a "Name" tag key with its value set as the hostname of the instance.
You can modify the script to set any value for the "Name" tag key instead of hostname and this can be pushed to multiple instances using "AWS-PowerShellScript" or "AWS-RunShellScript" document.
Linux:
instance=$(ssm-cli get-instance-information | jq -r '.["instance-id"]')
aws ssm add-tags-to-resource --resource-type "ManagedInstance" --resource-id $instance --tags Key=Name,Value=$(hostname)
Windows:
[System.Net.ServicePointManager]::SecurityProtocol = 'TLS12'
Install-Module -Name AWSPowerShell -Confirm:$false
Get-CimInstance -ClassName Win32_ComputerSystem
$hostname = (Get-CimInstance -ClassName Win32_ComputerSystem).Name
$path = & 'C:\Program Files\Amazon\SSM\ssm-cli.exe' get-instance-information
$instanceHostname = ($path | ConvertFrom-Json).'instance-id'
$tag = New-Object Amazon.SimpleSystemsManagement.Model.Tag
$tag.Key = "Name"
$tag.Value = '{0}' -F $hostname
Add-SSMResourceTag -ResourceType "ManagedInstance" -ResourceId $instanceHostname -Tag $tag -Force
Reference:
(-) https://docs.aws.amazon.com/systems-manager/latest/userguide/tagging-managed-instances.html
Hi Aamir, thanks for the answer. Your solution makes sense to me, as a way to add the tags post creation. Do you know if there is some magic settings for greengrass component configuration that will always set the tag as above, but on creation. This is my current configuration I add to the aws.greengrass.SystemsManagerAgent on deployment.
{ "SSMRegistrationRole": "SSMServiceRole_****", "SSMResourceTags": [ { "Key": "project", "Value": "*****" } ], "SSMOverrideExistingRegistration": true }
My greatest wish would be to be able to access the hostname in the the configuration above, like key: { "Key": "Name", "Value": $(hostname) }