簡體   English   中英

為什么無法通過 HTTP 訪問我的 ec2 實例?

[英]Why is my ec2 instance not accessible via HTTP?

您好,我已經從 AWS 控制台測試了我的 ec2 實例是否正常工作。

它工作正常。 我添加了示例腳本以在用戶數據部分顯示 hello world 文本。 然后粘貼ip地址沒有http's'。 當然,它顯示了文本。

我正在嘗試顯示相同的文本,但這次是使用 cloudformation。 我做了如下。 一切看起來都與通過 AWS 控制台制作的一樣。 但是,cloudformation 不允許我在 web 上進行評估,請求被掛起。 我不知道我錯過了什么,有人可以指出嗎?

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription: must be the name of an existing EC2 KeyPair.
  InstanceType:
    Description: WebServer EC2 instance type
    Type: String
    Default: t2.micro
    AllowedValues:
    - t1.micro
    - t2.nano
    - t2.micro
  SSHLocation:
    Description: The IP address range that can be used to SSH to the EC2 instances
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
    AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.

Mappings:
  AWSInstanceType2Arch:
    t1.micro:
      Arch: PV64
    t2.nano:
      Arch: HVM64
    t2.micro:
      Arch: HVM64
  AWSInstanceType2NATArch:
    t1.micro:
      Arch: NATPV64
    t2.nano:
      Arch: NATHVM64
    t2.micro:
      Arch: NATHVM64
  AWSRegionArch2AMI:
    ca-central-1:
      PV64: NOT_SUPPORTED
      HVM64: ami-730ebd17
      HVMG2: NOT_SUPPORTED

Resources:
  # ===== EC2 Instance =====
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType:
        Ref: InstanceType
      SecurityGroups:
      - Ref: InstanceSecurityGroup
      KeyName:
        Ref: KeyName
      UserData: 
        Fn::Base64:
          !Sub |
            #!/bin/bash

            sudo su
            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
      ImageId:
        Fn::FindInMap:
        - AWSRegionArch2AMI
        - Ref: AWS::Region
        - Fn::FindInMap:
          - AWSInstanceType2Arch
          - Ref: InstanceType
          - Arch

  # ===== Security Group =====
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
      # SSH
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp:
          Ref: SSHLocation
      # HTTP
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp:
          Ref: SSHLocation
      # HTTPS
      - IpProtocol: tcp
        FromPort: 443
        ToPort: 443
        CidrIp:
          Ref: SSHLocation

您顯示的模板沒有任何問題,並且按預期工作(假設您的 AMI 適用於 Amazon Linux 2)。 因此,問題中的模板可能不是您實際使用的模板,或者您使用的操作系統與您認為的不同。 您必須仔細檢查您的實際代碼。

您為 SSHLocation 使用了哪個參數值?

如果您希望 80 和 443 可公開訪問,而 SSH 僅可與您自己的 IP 一起使用,您可能需要使用以下 SG。

InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
      # SSH
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp:
          Ref: SSHLocation
      # HTTP
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0
      # HTTPS
      - IpProtocol: tcp
        FromPort: 443
        ToPort: 443
        CidrIp: 0.0.0.0/0

然后 SSH 你的機器並檢查里面的 web server conf。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM