使用ebextensions动态设置每个环境的EC2实例类型

问题描述 投票:3回答:1

我想在所有环境中创建EC2实例类型t3.medium,在生产环境中创建m5.large

我像这样使用.ebextensions(YAML):

选项1:

Mappings:
  EnvironmentMap:
    "production":
      TheType: "m5.large"
      SecurityGroup: "foo"
      ...
    "staging":
      TheType: "t3.medium"
      SecurityGroup: "bar"
      ...

option_settings:
  aws:autoscaling:launchconfiguration:
    IamInstanceProfile: "aws-elasticbeanstalk-ec2-role"
    InstanceType: !FindInMap
      - EnvironmentMap
      - !Ref 'AWSEBEnvironmentName'
      - TheType
    SecurityGroups:
      - {"Fn::FindInMap": ["EnvironmentMap", {"Ref": "AWSEBEnvironmentName"}, "SecurityGroup"]}

选项2:

    InstanceType: {"Fn::FindInMap": ["EnvironmentMap", {"Ref": "AWSEBEnvironmentName"}, "EC2InstanceType"]}

选项3:

    InstanceType:
      - {"Fn::FindInMap": ["EnvironmentMap", {"Ref": "AWSEBEnvironmentName"}, "EC2InstanceType"]}

结果

选项1因无效Yaml而失败(但我是从此AWS example中获得的。

选项2和3失败,并出现相同的问题。FindInMap函数不是“调用”的:Invalid option value: '{"Fn::FindInMap":["EnvironmentMap","EC2InstanceType"]},{"Ref":"AWSEBEnvironmentName"}' (Namespace: 'aws:autoscaling:launchconfiguration', OptionName: 'InstanceType'): Value is not one of the allowed values: [c1.medium, c1.xlarge, c3.2xlarge, .... 它试图将整个函数/事物解释为字符串。

对于SecurityGroups属性,它起作用,对于InstanceType,它不起作用。

我无法动态执行此操作,也无法在AWS文档,SO或其他任何地方找到如何实现此目标的方法。我认为这是简单的东西。我想念什么?


编辑:

选项4:使用条件

Conditions:
  IsProduction: !Equals [ !Ref AWSEBEnvironmentName, production ]

option_settings:

  aws:autoscaling:launchconfiguration:
    InstanceType: !If [ IsProduction, m5.large, t3.medium ]
    SecurityGroups:
      - {"Fn::FindInMap": ["EnvironmentMap", {"Ref": "AWSEBEnvironmentName"}, "SecurityGroup"]}

错误:YAML exception: Invalid Yaml: could not determine a constructor for the tag !Equals in...

但这来自conditionsif上的文档。


编辑2:

我最终发现选项InstanceTypeobsolute,我们应该使用:

aws:ec2:instances
  InstanceTypes: "t3.medium"

但是,这也不能解决问题,因为我也不能在这里使用替换功能(Fn:findInMap

amazon-web-services amazon-elastic-beanstalk amazon-cloudformation ebextensions
1个回答
1
投票

FindInMapoption_settings中不起作用的原因是,实际上只有四个内在函数被允许(来自docs):

  • 参考
  • Fn :: GetAtt
  • Fn :: Join
  • Fn :: GetOptionSetting

我不相信SecurityGroups有效。我认为您的脚本在FindInMap中的SecurityGroups获得评估机会之前就失败了。

但是,我试图找到一种方法使用Resources。我的关闭与以下config文件有关:

Mappings:
  EnvironmentMap:
    production:
      TheType: "t3.medium"
    staging:
      TheType: "t2.small"

Resources:
  AWSEBAutoScalingLaunchConfiguration:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      InstanceType:
        ? "Fn::FindInMap"
        :
          - EnvironmentMap
          - 
            Ref: "AWSEBEnvironmentName"
          - TheType

尽管距离更近了,但它[[最终失败]也是如此。原因是,当EB将Resources配置文件与其自己的模板结合在一起时,它将产生以下内容:

"InstanceType": { "Ref": "InstanceType", # <--- this should NOT be here :-( "Fn::FindInMap": [ "EnvironmentMap", { "Ref": "AWSEBEnvironmentName" }, "TheType" ] },
而不是

"InstanceType": { "Fn::FindInMap": [ "EnvironmentMap", { "Ref": "AWSEBEnvironmentName" }, "TheType" ] },

并且发生这种情况是因为原始的InstanceType(在联合操作之前是:)>

"InstanceType":{"Ref":"InstanceType"},

因此,EB 

而不是替换

InstanceType用配置文件中提供的自定义InstanceType进行合并。
© www.soinside.com 2019 - 2024. All rights reserved.