【问题标题】:Value of property SecurityGroupIds must be of type List of String error while updating stack更新堆栈时,属性 SecurityGroupIds 的值必须是字符串错误列表类型
【发布时间】:2019-03-16 16:35:47
【问题描述】:

我在尝试使用以下代码更新堆栈时收到 ROLLBACK_COMPLETE。在事件下,我没有收到错误消息,因为“属性 SecurityGroupIds 的值必须是字符串列表类型”。请帮助我找到解决方案。

第一个堆栈的Mycode:

Resources:
  myvpc:
    Type: AWS::EC2::VPC
    Properties:
        CidrBlock: 10.0.0.0/16
        EnableDnsSupport: true
        EnableDnsHostnames: true
        InstanceTenancy: default
        Tags:
            - Key: Name
              Value: myvpc

 myinternetgateway:
    Type: AWS::EC2::InternetGateway
    Properties:
        Tags: 
            - Key: Name
              Value: mygtwy

 mygatewayattach:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
        InternetGatewayId: !Ref myinternetgateway
        VpcId: !Ref myvpc

 mysubnet1:
    Type: AWS::EC2::Subnet
    Properties:
        AvailabilityZone: us-east-1a
        VpcId: !Ref myvpc
        CidrBlock: 10.0.1.0/24
        MapPublicIpOnLaunch: true

 Routetable:
    Type: AWS::EC2::RouteTable
    Properties:
        VpcId: !Ref myvpc

 Route:
    Type: AWS::EC2::Route
    DependsOn: myinternetgateway
    Properties:
        DestinationCidrBlock: 0.0.0.0/0
        GatewayId: !Ref myinternetgateway
        RouteTableId: !Ref Routetable

 SubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
        RouteTableId: !Ref Routetable
        SubnetId: !Ref mysubnet1

更新时,我添加了以下内容。在此期间,我收到了我之前提到的错误

 Myec2:
    Type: 'AWS::EC2::Instance'
    Properties:
        SecurityGroupIds:
            - !Ref Mysecgroup
        KeyName: !Ref KeyName
        ImageId: ami-0922553b7b0369273
        InstanceType: t2.micro
        SubnetId: !Ref mysubnet1

 Mysecgroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
        GroupDescription: Enable SSH access via port 22
        VpcId: !Ref myvpc
        SecurityGroupIngress:
            - IpProtocol: tcp
              FromPort: '22'
              ToPort: '22'
              CidrIp: 0.0.0.0/0  

【问题讨论】:

    标签: amazon-web-services amazon-ec2 cloud amazon-cloudformation devops


    【解决方案1】:

    当您将 AWS::EC2::SecurityGroup 类型指定为 Ref 函数,AWS CloudFormation 返回安全组名称或 安全组 ID(对于不在 默认 VPC)。

    您的模板引用了您应该引用组 ID 的安全组名称。

    Myec2:
        Type: 'AWS::EC2::Instance'
        Properties:
            SecurityGroupIds:
                - !GetAtt "Mysecgroup.GroupId"
            KeyName: !Ref KeyName
            ImageId: ami-0922553b7b0369273
            InstanceType: t2.micro
            SubnetId: !Ref mysubnet1
    
     Mysecgroup:
        Type: 'AWS::EC2::SecurityGroup'
        Properties:
            GroupDescription: Enable SSH access via port 22
            VpcId: !Ref myvpc
            SecurityGroupIngress:
                - IpProtocol: tcp
                  FromPort: '22'
                  ToPort: '22'
                  CidrIp: 0.0.0.0/0 
    

    【讨论】:

    • @George..thanx dude...我真的整天都在玩。感谢您的解决方案。我有一个疑问..cloud形成只会返回默认vpc的安全组ID..正确的?否则我们必须使用 !GetAtt 来获取 id。对吗?
    • 如果您在 VPC 中创建资产,GetAtt 很好;如果您使用的是 AWS 经典版,则必须切换到 Ref。底线是您的模板正在创建一个 VPC,因此您可以安全地使用 GetAtt
    【解决方案2】:

    按名称(而不是 SecurityGroupIds)引用安全组对我有用:

    EC2SG1IKTA:
        Type: 'AWS::EC2::SecurityGroup'
    EC2I1K240:
        Type: 'AWS::EC2::Instance'
        Properties:
          SecurityGroups:
            - !Ref EC2SG1IKTA
    

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 2020-03-25
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2021-08-06
      • 1970-01-01
      相关资源
      最近更新 更多