微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

模板格式错误:模板资源块中未解析的资源依赖关系 [EC2Instance1, EC2Instance2]

如何解决模板格式错误:模板资源块中未解析的资源依赖关系 [EC2Instance1, EC2Instance2]

我是 cloudformation 和 YAML 的新手,我正在尝试在不同区域创建 2 个 ec2 实例并将它们附加到负载均衡器

这些是参数

Parameters:
  KeyName:
    Description: Name of an existing EC2 Key Pair
    Type: AWS::EC2::KeyPair::KeyName

  VPC:
    Type: AWS::EC2::VPC::Id
    Description: Choose which VPC that the Application Load Balancer should be deployed to

  subnets:
    Description: Choose minimum of 2 subnets (2 different availability zones) that Application Load Balancer should be deployed to
    Type: List<AWS::EC2::subnet::Id>

之后我添加了资源。第一个是安全组。

Resources:
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDEscription: Enable SSH access via port 22 and Enable Http via port 80
      SecurityGroupEngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: '80'
          ToPort: '80'
          CidrIp: 0.0.0.0/0

一个位于法兰克福的 ec2 实例

# Frankfurt
  Ec2Instance1:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-043097594a7df80ec
      KeyName: !Ref KeyName
      SecurityGroups:
        - !Ref SecurityGroup
      UserData:
        'Fn::Base64':
          !Sub |
            #!/bin/bash -xe
            yum update -y
            yum install -y httpd
            systemctl start httpd
            systemctl enable httpd
            echo "<h1> Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

位于弗吉尼亚州的第二个 ec2 实例

# N. Virginia
  Ec2Instance2:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0d5eff06f840b45e9
      KeyName: !Ref KeyName
      SecurityGroups:
        - !Ref SecurityGroup
      UserData:
        'Fn::Base64':
          !Sub |
            #!/bin/bash -xe
            yum update -y
            yum install -y httpd
            systemctl start httpd
            systemctl enable httpd
            echo "<h1> Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

负载均衡器的代码。应用程序负载均衡器:

  ApplicationLoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: 'MyLoadBalancer1'
      subnets: !Ref subnets
      SecurityGroups: [!GetAtt SecurityGroup.GroupId]

之后是 ALBListener:

  ALBListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref ALBTargetGroup
      LoadBalancerArn: !Ref ApplicationLoadBalancer
      Port: 80
      Protocol: HTTP

和目标组:

  ALBTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Protocol: HTTP
      Port: 80
      HealthCheckIntervalSeconds: 30
      HealthCheckProtocol: HTTP
      HealthCheckTimeoutSeconds: 10
      HealthyThresholdCount: 3
      Matcher:
        HttpCode: '200'
      Name: MyTargets
      Targets: 
        - Id: 
            Ref: EC2Instance1
          Port: 80
        - Id: 
            Ref: EC2Instance2
          Port: 80
      VpcId: !Ref VPC

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。