Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I launch more than one EC2 instances using aws cloudformation without using AutoScaling?

I want to launch more than one Ec2 instances using aws cloudformation template without using AutoScaling. Please let me know how can I launch?

like image 938
Priya Rani Avatar asked Dec 20 '25 03:12

Priya Rani


2 Answers

There are several ways to launch multiple instances using CloudFormation without having Autoscaling Group in place.

  1. Create required number of resources in same Cloudformation template. Eg. If you want to launch 3 instances then you must write the code to launch 3 AWS instances in your Cloudformation Template.

Following template has 2 resource which will launch 2 EC2 instance. You can add more resources as per requirement.

server1:
Type: AWS::EC2::Instance
Properties:
  InstanceType: !Ref Server1InstanceType
  KeyName: !Ref ServerKeypair
  ImageId: !Ref ServerImageId
  SecurityGroupIds: 
    - !Ref ServerSG
  SubnetId: !Ref PrivateWeb1b
  Tags:
  - Key: Name
    Value: server1

server2:
Type: AWS::EC2::Instance
Properties:
  InstanceType: !Ref Server2InstanceType
  KeyName: !Ref ServerKeypair
  ImageId: !Ref ServerImageId
  SecurityGroupIds: 
    - !Ref ServerSG
  SubnetId: !Ref PrivateWeb1b
  Tags:
  - Key: Name
    Value: server2
  1. Create multiple Cloudformation Stacks using same Cloudformation template. Eg. You have to create 2 Cloudformation stacks from same Cloudformation template which has Resource to launch 1 EC2 instance each.

Following template has 1 resource which will launch 1 EC2 instance. As per 2nd method, you can create multiple Cloudformation stacks using same template to get multiple EC2 instances.

server1:
Type: AWS::EC2::Instance
Properties:
  InstanceType: !Ref Server1InstanceType
  KeyName: !Ref ServerKeypair
  ImageId: !Ref WebserverImageId
  SecurityGroupIds: 
    - !Ref WebserverSG
  SubnetId: !Ref PrivateWeb1b
  Tags:
  - Key: Name
    Value: server1
like image 165
sharvil_parekh Avatar answered Dec 23 '25 21:12

sharvil_parekh


Try using Type: AWS::EC2::EC2Fleet

You can specifies a configuration information to launch a fleet--or group--of instances. An EC2 Fleet can launch multiple instance types across multiple Availability Zones, using the On-Demand Instance, Reserved Instance, and Spot Instance purchasing models together. Using EC2 Fleet, you can define separate On-Demand and Spot capacity targets, specify the instance types that work best for your applications, and specify how Amazon EC2 should distribute your fleet capacity within each purchasing model.

**YAML**
Type: AWS::EC2::EC2Fleet
Properties: 
  ExcessCapacityTerminationPolicy: String
  LaunchTemplateConfigs: 
    - FleetLaunchTemplateConfigRequest
  OnDemandOptions: 
    OnDemandOptionsRequest
  ReplaceUnhealthyInstances: Boolean
  SpotOptions: 
    SpotOptionsRequest
  TagSpecifications: 
    - TagSpecification
  TargetCapacitySpecification: 
    TargetCapacitySpecificationRequest
  TerminateInstancesWithExpiration: Boolean
  Type: String
  ValidFrom: String
  ValidUntil: String

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-ec2fleet.html

like image 37
Elies Jebri Avatar answered Dec 23 '25 21:12

Elies Jebri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!