I need to add tag Name dynamically based on the hostcount.
ex: now all my instance has a name ex: web-linux-dev
I need add web-linux-dev-1 for first instance, web-linux-dev-2 for second instance and so on.
LauncConfig:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AvailabilityZones:
- !Ref Azs
MaxSize: 3
MinSize: 1
DesiredCapacity: !Ref InstanceCount
LaunchTemplate:
LaunchTemplateId: !Ref Launchtemplate
Version: !Ref LaunchTemplateVersionNumber
Tags:
- Key: "Environment"
PropagateAtLaunch: true
Value: !If [SelectEnv,Dev,Prod]
- Key: "Name"
PropagateAtLaunch: true
Value: !Sub
- "web-${os}-${env}"
- os: !Ref AMIs
env: !Ref Environment
2
Answers
You can’t define dynamic names for instances launched by an autoscaling group.
You can however configure a lambda function to run whenever the autoscaling launches new instances, and you can name the instances from the lambda.
From Instance metadata categories – Amazon Elastic Compute Cloud:
This metadata value is useful for naming or identifying instances launched under Auto Scaling. For example, the value can be used to differently configure a specific instance within an Auto Scaling group.
For your particular situation, you could use a User Data script that retrieves this value and then updates the Tag of the instance accordingly.
However, it will not be useful for future instances launched by Auto Scaling, since they will all be zero.