CloudFormation:无法通过 public DNS 访问使用 CloudFomation 创建的 EC2 实例

CloudFormation: Unable to access the EC2 instance created using CloudFomation through public DNS

我正在使用 CloudFormation 部署 EC2 实例。然后我安装了 apache 并在部署后将文件上传到 EC2 实例。部署实例后,我无法从浏览器使用 public DNS 访问它。

这是我的 EC2 实例资源及其安全组。

WebServerInstance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      SubnetId: !Ref PublicSubnet1
      ImageId:
        Fn::FindInMap:
          - AWSRegionArch2AMI
          - Ref: AWS::Region
          - Fn::FindInMap:
              - AWSInstanceType2Arch
              - Ref: InstanceType
              - Arch
      AvailabilityZone: !Select
        - 0
        - Fn::GetAZs: !Ref AWS::Region
  WebServerSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable HTTP access via port 80
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '80'
          ToPort: '80'
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp:
            Ref: SSHLocation
      VpcId: !Ref Vpc

当我从浏览器访问它时,它一直在加载加载加载。我也在安全组上设置了入站规则。它有什么问题,我该如何解决?

这是我的 public DNS, http://ec2-3-{xxx-xxx-xx}.eu-west-1.compute.amazonaws.com/

这是 Public 子网资源。

PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Select [ 0, !Cidr [ !Ref VpcCidr, 12, 8 ] ]
      MapPublicIpOnLaunch: True
      AvailabilityZone: !Select
        - 0
        - Fn::GetAZs: !Ref AWS::Region

public 子网有一个路由 table。

在互联网网关控制台中,只有一个网关,没有绑定到模板中的VPC。这可能是问题所在吗?

编辑 我收到这个错误

安全组之外有多种原因允许访问。应检查以下内容:

检查您的实例子网在其 0.0.0.0/0 的路由 table 中有一个路由,该路由的目标是互联网网关。

每个子网都有一个可用路由 table(如果您没有指定,这将是默认路由 table)。

这可以通过使用下面的 CloudFormation 来完成

  InternetGateway:
    Type: AWS::EC2::InternetGateway
  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId:
        Ref: VPC
      InternetGatewayId:
        Ref: InternetGateway    
  RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId:  
        Ref: myVPC
  Route:
    Type: AWS::EC2::Route
    DependsOn: InternetGateway
    Properties:
       RouteTableId:
         Ref: RouteTable
       DestinationCidrBlock: 0.0.0.0/0
       GatewayId:
         Ref: InternetGateway
  SubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId:
        Ref: Subnet
      RouteTableId:
        Ref: RouteTable

如果您更新了默认 NACL,请确保将端口 80 和临时端口都添加到规则中。

确保主机上 运行 有 apache(不仅仅是安装)。这可以通过 运行 systemctl start apache 在基于 debian 的 OS 或 systemctl start httpd 在基于 RHEL 的

上完成。