简体   繁体   English

为什么无法通过 HTTP 访问我的 ec2 实例?

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

Hi I have tested my ec2 instance working or not from AWS console.您好,我已经从 AWS 控制台测试了我的 ec2 实例是否正常工作。

And it works fine.它工作正常。 I have added the sample script to show hello world text in user data section.我添加了示例脚本以在用户数据部分显示 hello world 文本。 And then pasted the ip address without http 's'.然后粘贴ip地址没有http's'。 Of course, it shows the text.当然,它显示了文本。

And I am trying to show the same text, but this time by using cloudformation.我正在尝试显示相同的文本,但这次是使用 cloudformation。 I have made it as followings.我做了如下。 Everything looks the same as the one made through AWS console.一切看起来都与通过 AWS 控制台制作的一样。 However, the cloudformation one does not allow me to assess on web and the request gets hanged.但是,cloudformation 不允许我在 web 上进行评估,请求被挂起。 I have no idea what I am missing, Can Someone please point out?我不知道我错过了什么,有人可以指出吗?

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

There is nothing wrong with the template you showed and it works as expected (assuming that your AMI is for Amazon Linux 2).您显示的模板没有任何问题,并且按预期工作(假设您的 AMI 适用于 Amazon Linux 2)。 So probably your template in the question is not the one you are actually using, or perhaps you are using different operating system that you think you are.因此,问题中的模板可能不是您实际使用的模板,或者您使用的操作系统与您认为的不同。 You have to double check your actual code.您必须仔细检查您的实际代码。

Which parameter value did you use for SSHLocation?您为 SSHLocation 使用了哪个参数值?

If you want to have 80 and 443 publicly accessible and SSH only with your own IP, you will probably want to put instead the following SG.如果您希望 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

Then SSH your machine and check the web server conf inside.然后 SSH 你的机器并检查里面的 web server conf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 ec2 实例上部署的 8080 上访问我的 http 服务 - unable to access my http service on 8080 deployed on ec2 instance 无法通过 NodeJs 连接到我的 MongoDB EC2 实例 - Can't connect to my MongoDB EC2 instance via NodeJs AWS ELB/ALB http/2 通过 http/2(不是 http/1.1)传递到 EC2 实例 - AWS ELB/ALB http/2 pass thru to EC2 instance via http/2 (not http/1.1) 为什么我的 EC2 实例是否公开可用很重要? - Why does it matter if my EC2 instance is publicly available? Docker 服务可从 EC2 实例(本地主机)访问,但不能远程访问 - Docker service is accessible from EC2 Instance (localhost) but not remotely 分配Elastic IP地址后,EC2实例无法再访问 - After assigning Elastic IP address, the EC2 instance is no longer accessible 无法通过 ssh/http 进入 EC2 实例 - Not able to ssh/http into EC2 instance 为什么通过Session Manager访问私有EC2实例需要NAT Gateway? - Why does accessing private EC2 instance via Session Manager need NAT Gateway? 为什么我的 lambda function 不能访问 S3 和 SQS 而同一 VPC 中的 EC2 实例可以访问? - Why can my lambda function not access S3 and SQS when an EC2 instance in the same VPC can? 无法通过云端和域连接到 EC2 实例 - Unable to connect to EC2 instance via cloudfront and domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM