使用 cloudformation 将嵌套堆栈创建的 ec2 实例附加到负载均衡器堆栈

attaching ec2 instances created by nested stack with load balancer stack using cloudformation

我使用嵌套堆栈创建了两个 Web 服务器。还为负载均衡器创建了单独的嵌套堆栈。我想附加两个使用 Load balancder 创建的网络服务器。我真的对嵌套堆栈了解不多。如果有人提供帮助,那就太好了。这是用于使用嵌套堆栈

创建我的体系结构的代码

使用嵌套堆栈创建弹性负载均衡器的代码

AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation Sample Template for creating EC2 instance
Parameters:
  InstanceType:
    Description: WebServer EC2 instance type
    Type: String
    Default: t2.micro
    ConstraintDescription: must be a valid EC2 instance type.
  KeyName:
    Description: Key pair name
    Type: 'AWS::EC2::KeyPair::KeyName'
    Default: muneeshlab
  AMIid:
    Description: Image ID
    Type: 'AWS::EC2::Image::Id'
    Default: ami-00eb20669e0990cb4
  SSHLocation:
    Description: The IP address range that can be used to SSH to the EC2 instances
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
Resources:
  WebInstanceOne:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: !Ref InstanceType
      SecurityGroups:
        - !Ref InstanceSecurityGroup
      KeyName: !Ref KeyName
      ImageId: !Ref AMIid
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: !Ref SSHLocation
Outputs:
  InstanceId:
    Value:
      Ref: WebInstanceOne
    Description: ID of virtual server

**code for Load balancer**

AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation Sample Template for creating LoadBalancer
Resources:
  ElasticLoadBalancer:
    Type: AWS::ElasticLoadBalancing::LoadBalancer
    Properties:
      AvailabilityZones: !GetAZs ''
      CrossZone: 'true'
      Listeners:
      - LoadBalancerPort: '80'
        InstancePort: '80'
        Protocol: HTTP
      HealthCheck:
        Target: TCP:80
        HealthyThreshold: '3'
        UnhealthyThreshold: '5'
        Interval: '30'
        Timeout: '5'
      ConnectionDrainingPolicy:
        Enabled: 'true'
        Timeout: '300'  

**code for final nested stack**

AWSTemplateFormatVersion: '2010-09-09'
Description: EC2 instance with functions
Parameters:
  KeyName:
    Description: Key pair name
    Type: AWS::EC2::KeyPair::KeyName
    Default: muneeshlab
Resources:
  MyWebserverstack1:
    Type: AWS::CloudFormation::Stack
    Properties:
        Parameters:
          KeyName: !Ref KeyName
        TemplateURL: https://s3.amazonaws.com/cloudstack-buck/cloud_web_server.yaml
        TimeoutInMinutes: '5'
  MyWebserverstack2:
    Type: AWS::CloudFormation::Stack
    Properties:
        Parameters:
          KeyName: !Ref KeyName
        TemplateURL: https://s3.amazonaws.com/cloudstack-buck/cloud_web_server.yaml
        TimeoutInMinutes: '5'
  ElasticLoadBalancer:
    Type: 'AWS::CloudFormation::Stack'
    DependsOn:
      - MyWebserverstack2
      - MyWebserverstack1
    Properties:
        TemplateURL:  https://s3.amazonaws.com/cloudstack-buck/LB.yaml

注意:我可以毫无问题地创建三个堆栈。我唯一需要的是使用负载均衡器附加实例

您想做的是:

  • 在您的负载均衡器堆栈中输出 ELB 资源 Ref
  • 改为使用 AWS::AutoScaling::AutoScalingGroup 而不是 AWS::EC2::Instance。您可以利用 min/max/desired 来模拟单个实例行为。
  • 在您的父堆栈中,将负载均衡器堆栈的输出作为参数传递给 EC2 堆栈,例如:LoadBalancerName: !GetAtt 'ElasticLoadBalancer.Outputs.LoadBalancerOutputName'
  • 为 LoadBalancerNames 使用对参数 LoadBalancerName 的引用 属性 的 AWS::AutoScaling::AutoScalingGroup