
Verwenden von Terraform v1.0.11 unter Ubuntu 18.04
Nachdem terraform apply
Sie das main.tf
Folgende ausgeführt und darauf gewartet haben, dass die Instanz die Prüfungen besteht (und dann noch eine Minute), stoßen SSH-Versuche auf eine Mauer.
$ 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
Ich habe eine Instanz manuell mit demselben AMI und Schlüsselpaar gestartet und kann mich per SSH anmelden. Beim Vergleich der Netzwerk- und Sicherheitseinstellungen in der Konsole sind mir nur folgende Unterschiede aufgefallen: Die manuell bereitgestellte Instanz verwendet das Standard-VPC und „Antwort auf DNS-Name privater Ressource“ zeigt für die manuell bereitgestellte Instanz „IPv4 (A)“ und für die Terraformed-Instanz „-“. Beides scheint harmlos, aber ich kann mich irren.
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"
}
Wenn Ihnen unten keines der Probleme auffällt und Sie mir ein funktionierendes Beispiel zeigen können, wäre ich Ihnen auch dankbar.
Gelöst
Bei näherer Betrachtung zeigte sich, dass die Routing-Tabelle nur für das Subnetz und nicht für 0.0.0.0/0 routete. Durch Hinzufügen des Folgenden wurde das Problem behoben.
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
}
Antwort1
Ein Zeitüberschreitung bei der Verbindung weist in der Regel auf ein Netzwerkproblem hin. Überprüfen Sie:
- Sicherheitsgruppe/NACLs öffnen sich für Ihre IP auf Port 22
- VPC verfügt über ein Internet-Gateway
- Das Subnetz verfügt über eine Route zum Internet-Gateway
- Die Instanz hat eine öffentliche IP
- Das Routing ist korrekt eingerichtet
Dies ist die Schlüsselzeile Ihres Verbindungsversuchs, die Ihnen sagt, was passiert
ssh: connect to host 18.144.125.224 port 22: Connection timed out