【问题标题】:how to create an auto scaling group on non default vpc through cloudformation?如何通过cloudformation在非默认vpc上创建弹性伸缩组?
【发布时间】:2020-04-29 01:33:49
【问题描述】:

我正在尝试通过 非默认 VPC 上的云形成来创建自动缩放组,但我收到了两个错误:

默认 VPC 中不存在安全组“SecurityGroup”

此用户没有默认 VPC

我的第一次尝试是在一个没有默认 VPC 的区域上进行的,那是我第一次得到 No default VPC for this user 时,我觉得奇怪的是,当我不尝试使用它时它想要/需要默认 vpc。然后,我继续重新创建默认 VPC,并获得了 The security group 'SecurityGroup' does not exist in default VPC

我继续在没有自动缩放部分的情况下运行我的模板,只是为了让网络基础设施到位,以便我可以在控制台上玩(请参见下文)

Resources:
  VpcT:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 192.168.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Sub ${AWS::Region}a
      VpcId: !Ref VpcT
      CidrBlock: 192.168.0.0/24
      MapPublicIpOnLaunch: true

  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Sub ${AWS::Region}b
      VpcId: !Ref VpcT
      CidrBlock: 192.168.1.0/24
      MapPublicIpOnLaunch: true
  
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: sg
      VpcId: !Ref VpcT
      SecurityGroupIngress:
        - IpProtocol: -1
          CidrIp: 0.0.0.0/0
      SecurityGroupEgress:
        - IpProtocol: -1
          CidrIp: 0.0.0.0/0

使用该模板,我得到一个 VPC(VpcT)、两个分配自动 IP 地址的子网(PublicSubnet1 和 PublicSubnet2),最后是一个安全组(SecurityGroup)。

通过控制台并配置了我的启动配置,并通过高级详细信息我选择了Assign a public IP address to every instance,然后在您选择安全组的最后一部分,我收到了以下消息No default VPC found Select another VPC, or contact AWS Support if you want to create a new default VPC,我只是忽略并完成了启动配置

继续使用先前创建的启动配置创建 Auto Scaling 组,并在我尝试创建启动配置 No default VPC found Select another VPC, or contact AWS Support if you want to create a new default VPC 时得到完全相同的消息,我再次忽略了继续该过程并选择了 PublicSubnet1 和 PublicSubnet2我之前已经创建并完成了 Auto Scaling 组配置。一切正常,我的 Auto Scaling 组上指定的实例已创建,一切正常。

所以我验证了您可以在没有默认 vpc 的情况下创建自动缩放组,并且我的 cloudformation 自动缩放模板中可能缺少一些东西(请参见下文)

  LaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      LaunchConfigurationName: LaunchConfig
      AssociatePublicIpAddress: true
      ImageId: ami-02ccb28830b645a41
      EbsOptimized: false
      InstanceMonitoring: false
      InstanceType: t2.micro
      KeyName: gh_rsa
      SecurityGroups:
        - SecurityGroup

  AutoScalingGrpPubInstances:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      Cooldown: 300
      HealthCheckGracePeriod: 300
      VPCZoneIdentifier:
        - !Ref PublicSubnet1
        - !Ref PublicSubnet2
      LaunchConfigurationName: !Ref LaunchConfig
      DesiredCapacity: 1
      MaxSize: 3
      MinSize: 1

根据文档,我需要提供让 Auto Scaling 组创建实例的位置是 VPCZoneIdentifier,我尝试使用 !Ref PublicSubnetX 和实际子网 ID subnet-0e15c1ff36f7a228b,但没有成功。

有人可以为我指出如何解决这个问题的正确方向吗?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    知道了,您需要引用安全组。你错过了!Ref

      LaunchConfig:
        Type: AWS::AutoScaling::LaunchConfiguration
        Properties:
          LaunchConfigurationName: LaunchConfig
          AssociatePublicIpAddress: true
          ImageId: ami-02ccb28830b645a41
          EbsOptimized: false
          InstanceMonitoring: false
          InstanceType: t2.micro
          KeyName: gh_rsa
          SecurityGroups:
            - !Ref SecurityGroup
    

    希望这会有所帮助。

    【讨论】:

    • Arun-K 谢谢!这些类型的错误现在最难发现一切正常
    • 很高兴能帮上忙。
    【解决方案2】:

    现在我建议迁移到AWS::EC2::LaunchTemplate 而不是使用AWS::AutoScaling::LaunchConfiguration。前者是可变的,并且提供了更多的功能。

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 1970-01-01
      • 2019-04-27
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2015-05-18
      • 1970-01-01
      相关资源
      最近更新 更多