【问题标题】:CloudFormation: Unable to access the EC2 instance created using CloudFomation through public DNSCloudFormation:无法通过公共 DNS 访问使用 CloudFomation 创建的 EC2 实例
【发布时间】:2020-11-16 02:14:45
【问题描述】:

我正在使用 CloudFormation 部署一个 EC2 实例。然后我安装了 apache 并在部署后将文件上传到 EC2 实例。部署实例后,我无法使用浏览器中的公共 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

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

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

这是公共子网资源。

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

有一个公有子网的路由表。

在互联网网关控制台中,只有一个网关,并且未附加到模板中的 VPC。这可能是问题吗?

编辑 我收到了这个错误

【问题讨论】:

  • 子网是否有附有互联网网关的路由表?
  • 嗨。你有完整的 CFN 模板要展示吗,就像上次一样?这将使我能够在我的沙盒中启动它,并希望更快地验证实例连接性。
  • 嗨,是的。我们可以在聊天中这样做吗?
  • 我看到问题已经解决了 :-)
  • 是的。已解决。谢谢。

标签: amazon-web-services amazon-ec2 amazon-cloudformation amazon-vpc


【解决方案1】:

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

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

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

这可以通过使用下面的 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 正在主机上运行(不仅仅是安装)。这可以通过在基于 debian 的操作系统上运行 systemctl start apache 或在基于 RHEL 的操作系统上运行 systemctl start httpd 来完成。

【讨论】:

  • 我更新了问题以包含路由表的屏幕截图。你指的是那个吗?
  • 另外,包括互联网网关的详细信息。请看一下。
  • 是的,您需要创建一个新的互联网网关(它们只能连接到单个 VPC)并将其添加为路由。我在这个答案中添加了一些 CloudFormation,其中包括如何创建一个
  • 好的。谢谢。我会试一试的。
  • DestinationCidrBlock。 0.0.0.0/0 应该没问题还是必须和 VPC 中的一样?
猜你喜欢
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 2021-03-01
  • 2012-05-02
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
相关资源
最近更新 更多