无法访问使用terraform创建的EC2实例



我一直在跟随youtube指南学习地形,并遵循每一步。在运行terraform apply之后,它能够按预期设置一切。我已经在aws控制台验证了这一点。但是当试图访问公共ip时,提示连接被拒绝。

下面是我的主要内容。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"
}    
}

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

sudo apt install -y apache2

您错过了安装apache2后需要另一个命令来启动它

sudo systemctl start apache2

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

这里的主要问题是在安全组。对于SSH配置,您应该打开端口22,默认的SSH端口。

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

除此之外,你应该用:

修复你的userdata:
#! /bin/bash
sudo apt update -y 
sudo apt install apache2 -y
sudo systemctl start apache2
sudo bash -c 'echo your very first web server > /var/www/html/index.html'

我希望这可以帮助你和其他有相同或类似问题的人。

相关内容

  • 没有找到相关文章

最新更新