
Ubuntu 18.04에서 Terraform v1.0.11 사용
terraform apply
아래 내용을 수행 한 후 main.tf
인스턴스가 검사를 통과할 때까지 기다린 후(1분 더) SSH 시도가 벽에 부딪힙니다.
$ ssh -v -i ~/.ssh/toydeploy.pem [email protected]
OpenSSH_7.6p1 Ubuntu-4ubuntu0.5, OpenSSL 1.0.2n 7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 18.144.125.224 [18.144.125.224] port 22.
debug1: connect to address 18.144.125.224 port 22: Connection timed out
ssh: connect to host 18.144.125.224 port 22: Connection timed out
동일한 AMI와 키 쌍을 사용하여 수동으로 인스턴스를 가져왔고 SSH를 통해 연결할 수 있습니다. 콘솔의 네트워크 및 보안 설정을 비교했을 때 제가 발견한 유일한 차이점은 수동으로 배포한 인스턴스가 기본 VPC를 사용하고 있다는 것입니다. "개인 리소스 DNS 이름에 응답"에는 수동으로 배포된 인스턴스의 경우 "IPv4(A)"가 표시되고 Terraformed 인스턴스의 경우 "-"가 표시됩니다. 둘 다 괜찮아 보이지만 내 생각이 틀렸을 수도 있습니다.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
}
provider "aws" {
profile = "default"
region = "us-west-1"
}
variable "cidr_vpc" {
description = "CIDR block for VPC"
default = "10.1.0.0/16"
}
variable "cidr_subnet" {
description = "CIDR block for subnet"
default = "10.1.0.0/20"
}
resource "aws_vpc" "toydeploy-vpc" {
cidr_block = var.cidr_vpc
enable_dns_hostnames = true
enable_dns_support = true
}
resource "aws_subnet" "toydeploy-subnet" {
vpc_id = aws_vpc.toydeploy-vpc.id
cidr_block = var.cidr_subnet
}
resource "aws_security_group" "toydeploy-sg" {
name = "toydeploy-sg"
vpc_id = aws_vpc.toydeploy-vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"
]
}
# Terraform removes the default rule, so we re-add it.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "toydeploy" {
ami = "ami-083f68207d3376798" # Ubuntu 18.04
instance_type = "t2.micro"
security_groups = ["${aws_security_group.toydeploy-sg.id}"]
subnet_id = aws_subnet.toydeploy-subnet.id
associate_public_ip_address = true
key_name = "toydeploy"
}
아래에서 문제가 되는 부분이 없고 실제 사례를 알려주시면 감사하겠습니다.
해결됨
자세히 조사한 결과 라우팅 테이블은 0.0.0.0/0이 아닌 서브넷에 대한 라우팅만 이루어진 것으로 나타났습니다. 다음을 추가하면 문제가 해결되었습니다.
resource "aws_internet_gateway" "toydeploy-ig" {
vpc_id = aws_vpc.toydeploy-vpc.id
}
resource "aws_route_table" "toydeploy-rt" {
vpc_id = aws_vpc.toydeploy-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.toydeploy-ig.id
}
}
resource "aws_route_table_association" "toydeploy-rta" {
subnet_id = aws_subnet.toydeploy-subnet.id
route_table_id = aws_route_table.toydeploy-rt.id
}
답변1
연결 시간 초과는 일반적으로 네트워크 문제를 나타냅니다. 확인하다:
- 보안 그룹/NACL은 포트 22에서 IP에 열려 있습니다.
- VPC에 인터넷 게이트웨이가 있음
- 서브넷에는 인터넷 게이트웨이에 대한 경로가 있습니다.
- 인스턴스에 공개 IP가 있습니다.
- 라우팅이 올바르게 설정되었습니다.
이것은 무슨 일이 일어나고 있는지 알려주는 연결 시도의 핵심 라인입니다.
ssh: connect to host 18.144.125.224 port 22: Connection timed out