You need to enable JavaScript to run this app. CloudFormation模板启动EC2时报SecurityGroups属性需为字符串列表错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

CloudFormation模板启动EC2时报SecurityGroups属性需为字符串列表错误如何解决

报错原因
  • AWS::EC2::Instance的SecurityGroups属性要求类型为字符串列表,你当前直接传入单个!Ref MySG返回的是单个字符串,类型不匹配
  • 你指定了SubnetId(即VPC场景下启动EC2),SecurityGroups需要传入安全组ID而非安全组名称,你现有参数MySG的类型配置错误
修改方案

修改两处配置即可:

  1. 将MySG参数的Type改为AWS::EC2::SecurityGroup::Id,默认值替换为你实际的安全组ID(AWS安全组ID为小写sg-开头,原大写SG-格式错误)
  2. 将SecurityGroups的赋值改为列表形式,用- !Ref MySG包裹即可

修改后完整模板代码:

Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: 'AWS::EC2::KeyPair::KeyName'
    ConstraintDescription: must be the name of an existing EC2 KeyPair.

  MySubnet:
    Description: My subnet from my VPC
    Type: 'AWS::EC2::Subnet::Id'
    Default: subnet-YYYYYYYY
  
  MySG:
    Description: My Security Group from my VPC
    Type: 'AWS::EC2::SecurityGroup::Id'
    Default: sg-YYYYYYYY
   
Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-09e67e426f25ce0d7
      SecurityGroups: 
        - !Ref MySG
      SubnetId: !Ref MySubnet
      KeyName: !Ref KeyName

内容的提问来源于stack exchange,提问作者Delton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 03:06:00