amazon-web-services - 如何引用现有子网和现有安全组参数并在 YAML cloudformation 模板中引用它们

标签 amazon-web-services amazon-ec2 aws-cloudformation

如何引用现有子网和现有安全组参数并在 YAML cloudformation 模板中引用它们?

我尝试将子网和安全组的参数从硬编码 ID 更改为变量,但收到错误“属性 SubnetId 的值必须为字符串类型”。

 Parametres:
   PublicSecurityGroup:
   Description: WebSecurityGroup
   #Type: String
   Type: AWS::EC2::SecurityGroup::Id
   #Default: sg-081d3059c58edb3b6

 PublicSubnet:
  Description: Web/PublicSecurityGroup
  #Type : String
  #Default: subnet-0b3ea12c33b327f0a
  Type: 'List<AWS::EC2::Subnet::Id>'



Resources:

WebInstance: 
Type: AWS::EC2::Instance
Properties:
  KeyName:
    Ref: KeyName
  InstanceType:
    !FindInMap [
      EnvironmentToInstanceType,
      !Ref EnvironmentInstanceType,
      InstanceType,
    ]
  ImageId: !Ref ImageId
  # AvailabilityZone: !Ref AvailabilityZone
  #SubnetId: !Ref PublicSubnet
  SubnetId:
        - Ref: PublicSubnet
  SecurityGroupIds:
        - Ref: PublicSecurityGroup

最佳答案

AWS::EC2::Instance 只能位于单个子网中,不能位于多个子网中。因此,您必须指定一个子网,而不是子网列表。

 Parametres:
   PublicSecurityGroup:
   Description: WebSecurityGroup
   #Type: String
   Type: AWS::EC2::SecurityGroup::Id
   #Default: sg-081d3059c58edb3b6

 PublicSubnet:
  Description: Web/PublicSecurityGroup
  #Type : String
  #Default: subnet-0b3ea12c33b327f0a
  Type: 'AWS::EC2::Subnet::Id'



Resources:

WebInstance: 
Type: AWS::EC2::Instance
Properties:
  KeyName:
    Ref: KeyName
  InstanceType:
    !FindInMap [
      EnvironmentToInstanceType,
      !Ref EnvironmentInstanceType,
      InstanceType,
    ]
  ImageId: !Ref ImageId
  # AvailabilityZone: !Ref AvailabilityZone
  #SubnetId: !Ref PublicSubnet
  SubnetId: !Ref PublicSubnet
  SecurityGroupIds:
        - Ref: PublicSecurityGroup

关于amazon-web-services - 如何引用现有子网和现有安全组参数并在 YAML cloudformation 模板中引用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71953888/

相关文章:

linux - 如何将AWS老一代实例迁移到当前一代实例而不丢失数据

amazon-web-services - 即使在运行 jenkins 作业时在 jenkins windows 从属上安装 aws cli 后,仍出现 aws 命令未找到错误

amazon-web-services - 无法执行 AWS Pipeline 错误 : "An error occurred (AccessDenied) when calling the PutObject operation: Access Denied"

amazon-web-services - 如何借助新的 AWS Certificate Manager 服务将 SSL 证书添加到 AWS EC2

amazon-web-services - SQS lambda 集成 - Lambda 未收到任何消息

mysql - 无法从 EC2 实例连接到 RDS 实例

amazon-web-services - 使用为云形成堆栈提供的参数命名资源

amazon-web-services - 我可以使用 cloudformation 将现有 vpc 迁移到新帐户吗?

amazon-web-services - 我收到一些 AWS CodeBuild 错误。我不知道如何解决它

windows - 在 Azure VM(或 EC2)中的 Windows 变体上安装/执行产品测试?