如何在不使用 AutoScaling 的情况下使用 aws cloudformation 启动多个 EC2 实例?

Pri*_*ani 2 amazon-web-services aws-cloudformation

我想使用 aws cloudformation 模板启动多个 Ec2 实例,而不使用 AutoScaling。请让我知道如何启动?

sha*_*ekh 6

有多种方法可以使用 CloudFormation 启动多个实例,而无需安装 Autoscaling Group。

  1. 在同一 Cloudformation 模板中创建所需数量的资源。例如。如果您想启动 3 个实例,则必须编写代码以在 Cloudformation 模板中启动 3 个 AWS 实例。

以下模板有 2 个资源,将启动 2 个 EC2 实例。您可以根据需要添加更多资源。

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
Run Code Online (Sandbox Code Playgroud)
  1. 使用相同的 Cloudformation 模板创建多个 Cloudformation 堆栈。例如。您必须从同一 Cloudformation 模板创建 2 个 Cloudformation 堆栈,每个堆栈都有启动 1 个 EC2 实例的资源。

以下模板有 1 个资源,将启动 1 个 EC2 实例。根据第二种方法,您可以使用同一模板创建多个 Cloudformation 堆栈来获取多个 EC2 实例。

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