【问题标题】:unable to access EC2 instance created using terraform无法访问使用 terraform 创建的 EC2 实例
【发布时间】:2021-11-17 09:32:02
【问题描述】:

我一直在关注youtube guide 来学习 terraform 并遵循每个步骤。 运行 terraform apply 后,它能够按预期设置所有内容。我已经在 aws 控制台上验证了这一点。但是在尝试访问公共 IP 时,它说连接被拒绝。

下面是我的 main.tf 文件的内容。

provider "aws" {
  region     = "us-east-1"
  access_key = "ACCESS-KEY"
  secret_key = "SECERT-KEY"
}

# VPC
resource "aws_vpc" "prod-vpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
      Name = "production"
  }
}

# create internet gateway 
resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.prod-vpc.id
  tags = {
    Name : "Prod gateway"
  }
}

# create custom route table 

resource "aws_route_table" "prod-route-table" {
  vpc_id = aws_vpc.prod-vpc.id

  route {
      cidr_block = "0.0.0.0/0"
      gateway_id = aws_internet_gateway.gw.id
  }

  route {
      ipv6_cidr_block        = "::/0"
      gateway_id = aws_internet_gateway.gw.id
  }

  tags = {
    Name = "Prod"
  }
}

# Create a subnet 

resource "aws_subnet" "subnet-1" {
    vpc_id = aws_vpc.prod-vpc.id
    cidr_block = "10.0.1.0/24"
    availability_zone = "us-east-1a"
    map_public_ip_on_launch = true

    tags = {
        Name = "prod-subnet"
    }
}

# Associate subnet with Route Table 

resource "aws_route_table_association" "a" {
  subnet_id      = aws_subnet.subnet-1.id
  route_table_id = aws_route_table.prod-route-table.id
}

# Create Security Group to allow port 22, 80, 443

resource "aws_security_group" "allow_web" {
  name        = "allow_web_traffic"
  description = "Allow Web traffic"
  vpc_id      = aws_vpc.prod-vpc.id

  ingress {
      description      = "HTTPS"
      from_port        = 443
      to_port          = 443
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]
    }

  ingress {
      description      = "HTTP"
      from_port        = 80
      to_port          = 80
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]
    }
  ingress {
      description      = "SSH"
      from_port        = 2
      to_port          = 2
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]
    }

  egress {
      from_port        = 0
      to_port          = 0
      protocol         = "-1"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = ["::/0"]
    }

  tags = {
    Name = "allow_web"
  }
}

# Create a network interface with an ip in the subnet that was created earlier 

resource "aws_network_interface" "web-server-nic" {
  subnet_id       = aws_subnet.subnet-1.id
  private_ips     = ["10.0.1.50"]
  security_groups = [aws_security_group.allow_web.id]

  tags = {
    Name : "prod-network-interface"
  }
}

# Assign an elastic ip to the network interface created in previous step

resource "aws_eip" "one" {
  vpc                       = true
  network_interface         = aws_network_interface.web-server-nic.id
  associate_with_private_ip = "10.0.1.50"
  depends_on = [aws_internet_gateway.gw, aws_instance.web-server-instance]

  tags = {
    Name : "Prod-Elastic-ip"
  }
}

# Create Ubuntu server and install/enable apache2

resource "aws_instance" "web-server-instance" {
    ami = "ami-0747bdcabd34c712a"
    instance_type = "t2.micro"
    availability_zone = "us-east-1a"
    key_name = "main-key"

    network_interface {
        device_index = 0
        network_interface_id = aws_network_interface.web-server-nic.id
    }

    user_data = <<-EOF
        #! /bin/bash
        sudo apt update -y 
        sudo apt install apache2
        sudo bash -c 'echo your very first web server > /var/www/html/index.html'
        EOF

    tags = {
      Name : "Web-Server"
    }    
}

【问题讨论】:

  • SSH 端口是 22,而不是 2
  • 网络服务器是否正在运行并监听指定的端口?使用“curl”或“wget”通过服务器控制台检查它。
  • 如果配置VPC分配公网IP,为什么需要弹性IP?
  • 感谢您的建议和帮助@30thh。我仍在学习和探索这些东西,所以我只是按照教程进行操作。但我会进一步探讨您的建议。

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

您的用户数据中缺少-y,因此您的用户数据只会挂起等待确认。应该是:

sudo apt install -y apache2

【讨论】:

  • 感谢@Marcin 的快速帮助。如果其他人正在遵循相同的教程并面临一些问题,我会提到我面临的一个问题。在为弹性 ip 编写脚本时出现错误,我搜索并认为最好也将依赖项放在 aws 实例上。你可以参考我脚本中的这个小改动。 resource "aws_eip" "one" { ... depends_on = [aws_internet_gateway.gw, **aws_instance.web-server-instance**] tags = { Name : "Prod-Elastic-ip" } }
【解决方案2】:

你错过了另一个命令需要在安装后启动这个 apache2

sudo systemctl start apache2

【讨论】:

    【解决方案3】:

    似乎安全组上的 SSH 端口配置不正确。对于from_portto_port,它可能应该是22 而不是2

    【讨论】:

      猜你喜欢
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 2021-02-04
      • 1970-01-01
      • 2019-11-21
      • 2021-08-19
      • 2017-05-03
      相关资源
      最近更新 更多