S tommo
S tommo

Reputation: 165

How to dynamically change autoscaling instance names

I have created a heat stack which autoscales depending on CPU use. Each time a new instance is created, it is given a random name.

Is there a way to set a specific name with a counter added to the end of it so that each time a new instance is created it increases by 1?

E.g. Myinstance1, Myinstance2, Myinstance3 ... MyinstanceX

Thanks in advance!

Upvotes: 1

Views: 787

Answers (4)

Corey
Corey

Reputation: 1382

Update at 21/09/2020 :

Seems that creating an incremental number is impossible so far, but I found a workaround to achieve my goal, so post here hoping that could give you some ideas.

Mindset:
I tried to find something (which is number) that is created dynamically with the instance for scaling up, to me that is OS::Neutron::Port, so I append one part of IP address after a string to get a distinctive name for each instance.

Solution:
1.Create a port OS::Neutron::Port.
2.Get IP address using get_attr.
3.Split it with dot as delimiter using str_split.
4.Append one part of the address to the string using str_replace.

Sample Code:

lb_server.yaml

resources:
  corey_port:
    type: OS::Neutron::Port
    properties:
      network: { get_param: network }
      fixed_ips:
        - subnet: { get_param: subnet }

  number:
    type: OS::Heat::Value
    properties:
      value:
        # 192.168.xxx.yyy => [192,168,xxx,yyy]
        str_split: ['.', { get_attr: [corey_port, fixed_ips, 0, ip_address] }]

  server:
    type: OS::Nova::Server
    properties:
      name:
        str_replace:
          template: Corey-%last%
          params:
            #  0   1   2   3
            #[192,168,xxx,yyy]
            "last%": { get_attr: [number, value, 3] } 
      flavor: { get_param: flavor }
......

The outcome shoud be Corey-168, Corey-50, Corey-254, etc.

Upvotes: 0

Mahdi
Mahdi

Reputation: 3349

You can set the custom names by going to your Auto Scaling Groups and Tags tab, and then adding a tag with the key of "Name" and the value of "MyInstance". Numbering does not make that much sense since your instances are going to be launched and terminated constantly.

Upvotes: 0

Jayaprakash
Jayaprakash

Reputation: 763

In Openstack HEAT, stack resource names are manipulated with stack_name and suffixed with a short_id. That's why on every autoscaled up instance you could see the instance name as such. This is how the implementation done in overall HEAT project and it is not possible to define instance name suffixed with incremental number.

Upvotes: 1

yd1
yd1

Reputation: 277

if i understood you correctly, and if you are Object Oriented Programing:

you are looking for a design pattern called Factory, or more simply, create a static member that will increase in the constructor, and will be added to the name member of the instance created.

Upvotes: 0

Related Questions