Can I create multiple EC2 instances in AWS CloudFormation with values ​​from mappings without an AutoScaling group?

Let's say I want to create EC2 instances, one for each Type instance, but otherwise they are the same.

So, I would create a mapping as follows:

"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"
    ],

and later I would like to have

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

where I would magically get all types of instances created according to the mapping.

Is this possible without AutoScaling?

+6
source share
4 answers

, , , . . . CloudFormation, , conditions . , , , , , .

+2

, , . 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()
+8

cloudformation . , spotfleet, .

, @Ben Whaley, , python.

EJS ejs-cli , 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 %> <% } %>

.

+3
source

AWS CloudFormation Macros was launched on September 6, 2018, as explained in AWS Reinvent: 2018. Now you can add lambda functions to your template deployment.

0
source

All Articles