2014-02-24 20 views
2

假设我想为每个InstanceType创建一个EC2实例,否则它们是相同的。在AWS CloudFormation中,是否可以使用来自Mappings的值而不使用AutoScaling组创建多个EC2实例?

所以我想创建这样的映射:

"Mappings" : { 
    "MyAWSInstanceTypes" : [ 
     "t1.micro", 
     "m1.small", 
     "m1.medium", 
     "m1.large", 
     "m1.xlarge", 
     "m3.xlarge", 
     "m3.2xlarge", 
     "m2.xlarge", 
     "m2.2xlarge", 
     "m2.4xlarge", 
     "c1.medium", 
     "c1.xlarge", 
     "cc1.4xlarge", 
     "cc2.8xlarge", 
     "cg1.4xlarge", 
     "hi1.4xlarge", 
     "hs1.8xlarge" 
    ], 

,后来我想有

"Resources" : { 
    "MyEc2Instances" : {  
      "Type" : 
       "AWS::EC2::Instance", 

在那里我会神奇地得到所有我的根据映射创建的实例类型。

这是可能的,而不自动缩放?

+0

感谢您的回复。我可以看到这可以如何自动复制粘贴。我希望完全消除这一点。 – user1249170

回答

0

不,这是不可能的,没有迭代可以在模板中指定。但是,您可以为每个实例类型创建一个实例资源。这是复制和粘贴的问题。为了便于告诉CloudFormation在堆栈创建时运行哪些实例,您可以在模板中指定functionsconditions。例如,您可以创建一个或多个参数来指示要启动的实例类型,并使用条件仅启动您指定的实例类型。

+0

感谢您的提示。功能和条件绝对是我不知道的好功能,但不幸的是,在我的情况下不会消除复制粘贴。 – user1249170

5

这听起来像你想通过每个实例类型的循环,创建每个之一。这在CloudFormation模板中是不可能的。

您可以以编程方式生成的模板。 troposphere Python库提供了一个很好的抽象来生成模板。例如:

import json 
from troposphere import Template, ec2 


types = [ 
    "t1.micro", 
    "m1.small", 
    "m1.medium", 
    "m1.large", 
    "m1.xlarge", 
    "m3.xlarge", 
    "m3.2xlarge", 
    "m2.xlarge", 
    "m2.2xlarge", 
    "m2.4xlarge", 
    "c1.medium", 
    "c1.xlarge", 
    "cc1.4xlarge", 
    "cc2.8xlarge", 
    "cg1.4xlarge", 
    "hi1.4xlarge", 
    "hs1.8xlarge"] 
ami = "ami-12345678" 
t = Template() 

for type in types: 
    t.add_resource(ec2.Instance(
     type.replace('.', ''), #resource names must be alphanumeric 
     ImageId=ami, 
     InstanceType=type, 
     )) 

print t.to_json() 
0

我们还与客户端几个cloudformation模板工作后遇到同样的挑战者。实质上,我们想要遍历列表实例类型来生成Spotfleet启动配置,但我们不想在模板中手动复制这些代码。

我们试图对流层为@Ben惠利提到的,但它不是太适合我们的场景中,我们需要重写现有cloudformation模板与蟒蛇。

经过一番调查后,我们决定使用与EJS-CLI EJS模板来生成程序cloudformation模板,这使我们插值变量,包括不同的片段为目标CFT。

  • 片段管理

    Resources: <% include ./partials/resources.yml %> ...

  • 变量插值

    apiTaskDefinition: Type: AWS::ECS::TaskDefinition DependsOn: ECSTaskRole Properties: ContainerDefinitions: - Name: api Essential: true Image: <%= container_path %>/api Memory: <%= container.api.memory %>

  • 遍历列表

    Properties: SpotFleetRequestConfigData: IamFleetRole: !GetAtt iamFleetRole.Arn SpotPrice: !Ref 'ECSSpotPrice' TargetCapacity: !Ref 'DesiredCapacity' TerminateInstancesWithExpiration: false AllocationStrategy: lowestPrice LaunchSpecifications: <% for(var i in instancesTypes) {%> <% include ./partials/instance-launch-specification.yml %> <% } %>

您可以在这里找到演示source code和我们blog post

相关问题