CloudFormation模板启动EC2时报SecurityGroups属性需为字符串列表错误如何解决
报错原因
AWS::EC2::Instance的SecurityGroups属性要求类型为字符串列表,你当前直接传入单个!Ref MySG返回的是单个字符串,类型不匹配- 你指定了
SubnetId(即VPC场景下启动EC2),SecurityGroups需要传入安全组ID而非安全组名称,你现有参数MySG的类型配置错误
修改方案
修改两处配置即可:
- 将
MySG参数的Type改为AWS::EC2::SecurityGroup::Id,默认值替换为你实际的安全组ID(AWS安全组ID为小写sg-开头,原大写SG-格式错误) - 将
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
相关产品推荐
相关产品推荐

