模板格式错误:模板资源块中未解析的资源依赖关系 [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 (将#修改为@)