Stacker AwsvpcConfiguration作为yaml传入并收到str

时间:2017-12-20 22:16:21

标签: python yaml troposphere

我试图获得一个Stacker项目(使用Troposphere)并且正在运行,我已经能够创建多个资源,但我没有想到子网分段。我曾试图通过几种不同的方式将它们传递到我的蓝图课程中,但我认为这是最接近的,它与其他不完全爆炸的实现相似。我们的想法是接受像这样的子网的一些配置。

在我的my_cluster.yaml中,我有:

 stacks:
 ...
  - name: cluster-networks
    description: "Networks for an ECS cluster"
    class_path: blueprints.networks.Networks
    variables:
      Networks: 
        InternalComms:
          AssignPublicIp: False
          SecurityGroups: 
            - sg-id
          Subnets: 
            - subnet-id1
            - subnet-id2

并且要阅读该配置,我有一个名为blueprints/networks.py的蓝图,其中包含此内容:

class Networks(Blueprint):
"""Manages creation of networks for ECS clusters"""

# Variables that are passed from the my_cluster.yaml
VARIABLES = {
    "Networks": {
        "type": TroposphereType(ecs.AwsvpcConfiguration, many=True),
        "description": "Dictionary for ECS cluster networks"
    }
}

def create_template(self):
    """method that Stacker calls to manipulate the template(s)"""
    variables = self.get_variables()
    for config in variables['Networks']:
        network = ecs.NetworkConfiguration(config)
        t.add_resource(network)
        # do other useful stuff like set outputs

如果您想知道为什么我创建一个AwsvpcConfiguration对象然后创建一个NetworkConfiguration对象,原因是因为我尝试使用{{1}传递此信息我采用NetworkConfiguration对象之前的对象,它也没有用。我使用此file来指导我,因为它定义了这些对象。下一步是AwsvpcConfiguration资源,所以当我通过运行此命令执行此操作时:

build

我收到一条错误消息:

stacker build path/to/my.env path/to/my_cluster.yaml

这可能是我在堆叠器,yaml和python缺乏技能,但我很难过,已经有一天左右。

我无法弄清楚如何将配置从yaml传递到蓝图作为字典,就像我以相同的方式处理在AWS-land中成功创建的其他资源。如果你能指出我的错误,我会非常感激,并且肯定会告诉圣诞老人你有多好。

1 个答案:

答案 0 :(得分:1)

troposphere maintainer / stacker author here here。所以有一些事情:

  1. 您想使用TroposphereType(ecs.NetworkConfiguration, many=True),因为这是您要创建的对象类型。
  2. ecs.NetworkConfiguration(https://github.com/cloudtools/troposphere/blob/master/troposphere/ecs.py#L71)是一个AWSProperty类型,这意味着它内部的值,如果使用“many”需要是每个字典列表:http://stacker.readthedocs.io/en/latest/blueprints.html#property-types
  3. 所以我认为你想要的是你的配置

    variables: Networks: - AwsvpcConfiguration: AssignPublicIp: False SecurityGroups: - sg-id Subnets: - subnet-id1 - subnet-id2

    这是因为TroposphereType希望您传入该类型所需的确切参数。 NetworkConfiguration需要一个密钥AwsvpcConfiguration,而您传递的值是AwsvcpConfiguration对象所期望的值。

    现在更大的问题是你期望如何使用这些对象。在Cloudformation / troposphere中,属性类型不是自己创建的 - 它们用作实际资源的属性 - 在这种情况下是ecs.Service类型。您是否打算将服务纳入同一蓝图?如果没有,您打算与这些服务所在的其他蓝图共享这些属性的计划是什么?

    更合适的可能是建立一个构建服务的蓝图NetworkConfiguration。然后,如果您希望使用该蓝图在多个堆栈之间轻松共享相同的NetworkConfiguration,您可以使用YAML锚点执行某些操作:

    common_network_configuration: &common_network_configuration - AwsvpcConfiguration: AssignPublicIp: False SecurityGroups: - sg-id Subnets: - subnet-id1 - subnet-id2

    然后在变量中的任何地方使用它:

    variables: << : *common_network_configuration

    我希望这是有道理的 - 如果您还有其他问题,您也可以随时通过堆叠器Slack与我们联系:https://empire-slack.herokuapp.com/

    有一个好的!