Я создал экземпляр aws ec2 с terraform, теперь я не могу подключиться к машине по SSH. это код терраформа, который я использовал, регионы, которые я считаю правильными
resource "aws_key_pair" "test-terraform" {
key_name = "test-terraform"
public_key = file("~/Documents/key-pairs/test-terraform.pub")
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "main"
}
}
resource "aws_subnet" "main_subnet" {
vpc_id = aws_vpc.main.id # reference the related VPC id here
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a" # optional
tags = {
Name = "main_subnet"
}
}
resource "aws_security_group" "main_security_group" {
name = "main-security-group"
description = "Security group for ec2 instances"
vpc_id = aws_vpc.main.id # reference the related VPC id
tags = {
Name = "main_security_group"
}
# Allow all outbound traffic
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow SSH inbound traffic
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] # You can restrict this to specific IP addresses for better security
}
}
resource "aws_instance" "web_server" {
ami = "ami-080e1f13689e07408" # Update with your desired AMI ID
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.main_security_group.id]
subnet_id = aws_subnet.main_subnet.id # Associate with the public subnet
associate_public_ip_address = true # Allocate a public IP address to the instance
key_name = aws_key_pair.test-terraform.key_name # ti add jkey from referencing it, key must be generated locally and the public key must be referenced check the block below
# Optional but good security measure
# metadata_options {
# http_tokens = "required" # Require the use of IMDSv2
# http_put_response_hop_limit = 1 # Ensure only one hop for HTTP PUT requests
# }
# Add tags (optional)
tags = {
Name = "Web Server Instance"
}
}
output "public_ip" {
value = aws_instance.web_server.public_ip
}
Я проверил сеть, вроде все в порядке, Даже консоль Aws не может подключиться, что говорит мне о том, что проблема не в ключе. возможно, это глупая ошибка, но я с нетерпением жду решения
Спасибо за подсказку, я добавил эту часть `resource "aws_internet_gateway" "main_internet_gateway" { vpc_id = aws_vpc.main.id } resources "aws_route_table" "main_route_table" { vpc_id = aws_vpc.main.id Route { cidr_block = "0.0. 0.0/0" шлюз_ид = aws_internet_gateway.main_internet_gateway.id } } ресурс "aws_route_table_association" "main_route_association" { subnet_id = aws_subnet.main_subnet.idroute_table_id = aws_route_table.main_route_table.id } `
Устранило ли вашу проблему добавление IGW и соответствующей записи таблицы маршрутизации общедоступной подсети с маршрутом по умолчанию к IGW?
В вашем шаблоне Terraform отсутствует несколько вещей, необходимых для входящего и исходящего сетевого трафика, а именно:
Вы можете легко добавить их, примерно так:
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "VPC IGW"
}
}
resource "aws_route_table" "rtb2" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "Route Table 2 for IGW"
}
}
Вопрос не соответствует теме Stack Overflow, как это определено в справочном центре . Пожалуйста, не отвечайте на такие вопросы; вместо этого вам следует пометить их как требующие внимания, и они будут закрыты или перенесены соответствующим образом.
У вас нет интернет-шлюза. См. Шаг 3. Настройка интернет-шлюза здесь.