【问题标题】:Cannot access AWS EC2 public IP created through Terraform无法访问通过 Terraform 创建的 AWS EC2 公共 IP
【发布时间】:2021-07-06 04:16:34
【问题描述】:

我正在尝试运行 Terraform Up and Running 一书中的第一个 basic examples。我的main.tf 与链接中的几乎相同,除了版本:

provider "aws" {
  region = "us-east-2"
}

resource "aws_instance" "example" {
  ami                    = "ami-0c55b159cbfafe1f0"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.instance.id]

  user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF

  tags = {
    Name = "terraform-example"
  }
}

resource "aws_security_group" "instance" {

  name = var.security_group_name

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

variable "security_group_name" {
  description = "The name of the security group"
  type        = string
  default     = "terraform-example-instance"
}

output "public_ip" {
  value       = aws_instance.example.public_ip
  description = "The public IP of the Instance"
}

我运行了terraform apply,一切似乎都已成功创建。但是,当我尝试运行 curl http://&lt;EC2_INSTANCE_PUBLIC_IP&gt;:8080 时,命令挂起。

我在运行示例之前创建了我的 AWS 账户,因此它使用默认网络配置。

路由表有一个条目指向 VPC 的 Internet 网关:

Destination    |      Target      |     Status      |   Propagated
0.0.0.0/0      |     igw-<igwId>  |      active     |      No

网络 ACL 具有默认设置:

Rule number   |    Type    | Protocol  | Port range | Source    | Allow/Deny
100           | All traffic|    All    |    All     | 0.0.0.0/0 |    Allow
*             | All traffic|    All    |    All     | 0.0.0.0/0 |    Deny

我的 Terraform 版本是 v0.14.10

关于如何通过 EC2 的公共 IP 访问实例的服务器有什么想法吗?

【问题讨论】:

    标签: amazon-web-services networking amazon-ec2 terraform


    【解决方案1】:

    TF 程序没有问题。我使用我的沙盒帐户验证它,它按预期工作。脚本开始工作需要 1-2 分钟,所以您可能测试得太早了。

    因此,无论您遇到什么困难,都不是脚本本身造成的。因此,要么您在 SO 上发布的内容不是您的实际代码,要么您以某种方式修改了您的 VPC 配置,导致实例无法访问。

    【讨论】:

    • 这就是我如此困惑的原因。我专门为此示例创建了 AWS,但它不起作用。我从示例中复制粘贴了脚本,所以这是实际代码。您知道如何尝试调试网络配置吗?
    • 好的,在我发布我之前的评论之后,我更接近于意识到问题所在。当我连接到我的 WiFi 网络时尝试从我的计算机访问它时出现一些问题。当我使用 4G 从手机访问它时,它按预期工作。我是网络新手,所以知道如何检查本地是否存在问题,或者 AWS 是否出于某种原因不喜欢我的 IP?
    • @IvayloToskov 很高兴它成功了。 AWS 主动监控传入流量。它可能会认为您的 IP 是恶意的。如果答案有帮助,我们将不胜感激。
    猜你喜欢
    • 2020-11-03
    • 2021-03-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多