在AWS CloudFormation中,是否可以使用Mappings中没有AutoScaling组的值创建多个EC2实例?

use*_*170 6 amazon-web-services aws-cloudformation

假设我想为每个InstanceType创建一个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"
    ],
Run Code Online (Sandbox Code Playgroud)

后来我想拥有

 "Resources" : {  
    "MyEc2Instances" : {    
             "Type" :
                 "AWS::EC2::Instance",
Run Code Online (Sandbox Code Playgroud)

我会神奇地获取根据映射创建的所有实例类型.

没有AutoScaling可以吗?

Ben*_*ley 8

听起来你想循环遍历每个实例类型,创建其中一个.这在CloudFormation模板中是不可能的.

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

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()
Run Code Online (Sandbox Code Playgroud)


小智 2

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