【问题标题】:What is the error for intrinsic function !Ref in this Cloudformation template in AWS?AWS 中此 Cloudformation 模板中的内部函数 !Ref 的错误是什么?
【发布时间】:2021-06-23 01:41:02
【问题描述】:

最近我开始探索 YAML 格式的 AWS CloudFormation。我收到错误消息:

遇到不支持的属性类型

YAML 代码说明如下:-

AWSTemplateFormatVersion: 2010-09-09
Resources: 
  DevEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-04aa88aebb9fefd83
      Type: t2.micro
      KeyName: Newkey
      SecurityGroups:
       - default
       - !Ref SSHSecurityGroup
SSHSecurityGroup:
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupDescription: Group_For_CloudFormation
    SecurityGroupIngress:
    - Ipprotocol: tcp
      FromPort: '22'
      ToPort: '22'
      CidrIp: 0.0.0.0/0

【问题讨论】:

标签: amazon-web-services yaml amazon-cloudformation


【解决方案1】:

你能检查一下这个 sn-p 是否适合你吗?

Resources:

  DevEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-04aa88aebb9fefd83
      InstanceType: t2.micro
      KeyName: Newkey
      SecurityGroups:
        - default
        - !Ref SSHSecurityGroup
  SSHSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Group_For_CloudFormation
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

【讨论】:

    【解决方案2】:

    所以正确的代码如下:-

    AWSTemplateFormatVersion: 2010-09-09
    Resources: 
      DevEC2Instance:
       Type: AWS::EC2::Instance
       Properties:
         ImageId: ami-04aa88aebb9fefd83
         InstanceType: t2.micro
         KeyName: Newkey
         SecurityGroups:
          - default 
          - !Ref SSHSecurityGroup
      SSHSecurityGroup:
         Type: AWS::EC2::SecurityGroup
         Properties:
          GroupDescription: SMy Security Group
          SecurityGroupIngress:
           - IpProtocol: tcp
             FromPort: '22'
             ToPort: '22'
             CidrIp: 0.0.0.0/0
    

    【讨论】:

      【解决方案3】:

      错误是由于资源SSHSecurityGroup 上缺少缩进,在 YAML 中您需要小心缩进。这使得 Cloudformation 不会将元素作为资源,因为它处于同一缩进级别。

      这应该可行:

      AWSTemplateFormatVersion: 2010-09-09
      Resources: 
        DevEC2Instance:
          Type: 'AWS::EC2::Instance'
          Properties:
            ImageId: ami-04aa88aebb9fefd83
            InstanceType: t2.micro
            KeyName: Newkey
            SecurityGroups:
             - default
             - !Ref SSHSecurityGroup
        SSHSecurityGroup:
          Type: AWS::EC2::SecurityGroup
          Properties:
            GroupDescription: Group_For_CloudFormation
            SecurityGroupIngress:
             - IpProtocol: tcp
               FromPort: '22'
               ToPort: '22'
               CidrIp: 0.0.0.0/0
      

      【讨论】:

      • Type 应该是 ImageType 并且应该是 IpProtocol。
      • 您对 IpProtocol 的看法是正确的。对于 Type 应该是 InstanceType,ImageType 没有出现在 Ec2 Instance 的官方文档中。两者都已更正。
      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 2019-12-13
      • 2018-08-31
      • 1970-01-01
      • 2022-07-21
      • 2017-02-14
      • 2019-02-15
      • 2021-05-24
      相关资源
      最近更新 更多