简体   繁体   English

Cloudformation yaml - 在 AutoScalingGroup 资源中生成实例列表

[英]Cloudformation yaml- generate a list of instances in AutoScalingGroup resource

I am struggling to figure out how to write overrides section of the yaml template for AutoScalingGroup resource.我正在努力弄清楚如何为AutoScalingGroup资源编写 yaml 模板的覆盖部分。 Currently, what I have only works for a fixed length of the string.目前,我所拥有的仅适用于固定长度的字符串。 In this case string passed into the template is called InstanceTypesOverride .在这种情况下,传入模板的字符串称为InstanceTypesOverride

Below you can see the snippet of the code:您可以在下面看到代码片段:

InstanceTypesOverride = "m5.large,m5d.large,m5a.large"

Type: AWS::AutoScaling::AutoScalingGroup
  Properties:
    AutoScalingGroupName: !Ref NodeGroupName
    DesiredCapacity: !Ref NodeAutoScalingGroupDesiredCapacity
    MixedInstancesPolicy:
      InstancesDistribution:
        OnDemandAllocationStrategy: prioritized
        OnDemandBaseCapacity: !Ref OnDemandBaseCapacity
        OnDemandPercentageAboveBaseCapacity: !Ref OnDemandPercentageAboveBaseCapacity
        SpotAllocationStrategy: !Ref SpotAllocStrategy
      LaunchTemplate:
        LaunchTemplateSpecification:
          LaunchTemplateId: !Ref SpotLaunchTemplate
          Version: !GetAtt SpotLaunchTemplate.LatestVersionNumber
        Overrides:
          - InstanceType: !Select [0, !Split [ ",", !Ref InstanceTypesOverride ] ]
          - InstanceType: !Select [1, !Split [ ",", !Ref InstanceTypesOverride ] ]
          - InstanceType: !Select [2, !Split [ ",", !Ref InstanceTypesOverride ] ]

I would like to find out whether it is possible to write the overrides section of the template so that InstanceTypesOverride can handle more instance types or less.我想知道是否可以编写模板的覆盖部分,以便InstanceTypesOverride可以处理更多或更少的实例类型。 As you know, various regions can handle different instances types, therefore it is essential for me to have the ability for the cloudformation to work with different string lengths.如您所知,不同的区域可以处理不同的实例类型,因此对我来说,让 cloudformation 能够处理不同的字符串长度是必不可少的。 Can someone suggest something that would work?有人可以建议一些可行的方法吗?

Ideally, I would like to have a cfm template which works with both InstanceTypesOverride = "m5.large,m5d.large,m5a.large" and InstanceTypesOverride = "m5.large,m5d.large,m5a.large,m5ad.large,m5n.large,m5dn.large"理想情况下,我想要一个 cfm 模板,它可以同时使用InstanceTypesOverride = "m5.large,m5d.large,m5a.large"InstanceTypesOverride = "m5.large,m5d.large,m5a.large,m5ad.large,m5n.large,m5dn.large"

Sadly, you can't do this using plain CloudFormation (CFN).遗憾的是,您不能使用普通的 CloudFormation (CFN) 来做到这一点 This would require looping mechanisms which are not supported in CFN.这将需要 CFN 不支持的循环机制。

The only way to do it would be through CFN custom resources or CFN macro .唯一的方法是通过 CFN 自定义资源或 CFN

In the first case you would have to develop a custom lambda function which would create the entire AutoScalingGroup using AWS SDK (eg python).在第一种情况下,您必须开发一个自定义 lambda function它将使用 AWS SDK(例如 python)创建整个AutoScalingGroup In the second case, lambda would also need to be created but it would parse the original template, expand the Overrides , and return the modified template to CFN for execution.在第二种情况下,lambda 也需要创建,但它会解析原始模板,展开Overrides并将修改后的模板返回给 CFN 执行。

Alternative is to not use CFN, but terraform (TF) for example.替代方案是不使用 CFN,但例如 terraform (TF)。 TF has very reach and extensive support for loops which would allow you to easily do what you want. TF 对循环有非常广泛的支持和广泛的支持,可以让你轻松地做你想做的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM