skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. From Instance metadata categories – Amazon Elastic Compute Cloud:

    ami-launch-index
    If you started more than one instance at the same time, this value indicates the order in which the instance was launched. The value of the first instance launched is 0.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search