AWS通过VPC中的ELB连接到EC2,如何配置?

AWS connect to EC2 through ELB in a VPC, how to configure?

我是 AWS 的新手,我正在研究 Cloudformation 模板以获得工作堆栈。我的想法是使用 EC2 作为 web 服务器的静态 hello world html 页面无法从外部访问,但只能通过 ELB(面向互联网)访问。全部在 VPC 中。我已经部署了一个堆栈但仍然无法正常工作,我想这取决于网络配置。我该如何设置?

从 awslabs 的示例开始,并遵循每个资源的 Cloudformation 文档,我制作了一个 yaml 模板并部署了堆栈。

这里只有EC2和ELB配置

  MYInstance1:
      Type: AWS::EC2::Instance
      Properties:
        DisableApiTermination: 'false'
        InstanceInitiatedShutdownBehavior: stop
        ImageId: ami-70edb016
        InstanceType: t2.micro
        Monitoring: 'false'
        UserData:
          Fn::Base64:
            !Sub |
            #!/bin/bash
            yum update -y
            yum install -y httpd24
            service httpd start
            chkconfig httpd on
            groupadd www
            usermod -a -G www ec2-user
            chown -R root:www /var/www
            chmod 2775 /var/www
            find /var/www -type d -exec chmod 2775 {} +
            find /var/www -type f -exec chmod 0664 {} +
            echo '<html><head><title>Test</title></head><body><p>Hello world!</p></body></html>' > /var/www/html/demo.html
        Tags:
          - Key: environment
            Value: demo-test
        NetworkInterfaces:
        - AssociatePublicIpAddress: 'true'
          DeleteOnTermination: 'true'
          Description: Primary network interface
          DeviceIndex: 0
          SubnetId: !Ref 'PublicSubnetA'
          GroupSet: [!Ref 'MYSGapp']

    MYelb:
      Type: AWS::ElasticLoadBalancing::LoadBalancer
      Properties:
        Subnets: [!Ref 'PublicSubnetB']
        Instances: [!Ref 'MYInstance1']
        SecurityGroups: [!Ref 'MYSGELB']
        Listeners:
        - LoadBalancerPort: '80'
          InstancePort: '80'
          Protocol: HTTP
        HealthCheck:
          HealthyThreshold: '2'
          Interval: '15'
          Target: TCP:443
          Timeout: '5'
          UnhealthyThreshold: '2'

您可以在此处找到包含我当前配置的完整模板:

https://pastebin.com/PjDtiwfC

查看控制台,我对 ACL 和路由有点困惑。

我希望能够在 ELB-Public-DNS.com/demo.html.

看到 html 页面

关于我现在所在位置的更多详细信息: CloudFormation 启动正常,服务器 运行。我尝试在 Ec2 安全组入站规则中打开所有流量,EC2 DNS/demo.html 实际显示该页面。但我需要通过 ELB DNS 而不是 EC2 访问它。 从 ELB 界面中,我看到该实例处于 OutOfService 状态,因为它未通过 UnhealthyTreshold。 在 EC2 安全组中,我已经设置了来自 ELB 安全组的入站规则 TCP 80 和 443,但仍然无法正常工作。

您已将负载均衡器运行状况检查配置为使用端口 443,但您似乎没有在 EC2 服务器上安装 SSL 证书。您可以直接访问您的 EC2 服务器的 HTTPS URL 吗?

您可能希望使用 Amazon Certificate Manager 服务获取 SSL 证书并将其安装在您的负载均衡器上,在这种情况下,您的 EC2 服务器永远不会拥有 SSL 证书,也永远不会侦听端口 443,所以需要将健康检查端口改为80.

如果您是 cloudformation 的新手,可以尝试 cloudkast。它是一个在线 aws cloudformation 模板生成器。它对开始使用 cloudformation 的任何人都非常有用。