我使用 terraform 创建了一个 ec2 实例(没有 .pem key )。我可以在本地系统和 ec2 实例之间建立 ssh 连接吗?
最佳答案
假设您使用 Terraform v0.12.+
配置了一个实例使用这种结构:
resource "aws_instance" "instance" {
ami = "${var.ami}"
instance_type = "t2.micro"
count = 1
associate_public_ip_address = true
}
您可以进行一些额外的设置:
output
: output "instance_ip" {
description = "The public ip for ssh access"
value = aws_instance.instance.public_ip
}
aws_key_pair
使用现有的 ssh 公钥或创建 new one例如:
resource "aws_key_pair" "ssh-key" {
key_name = "ssh-key"
public_key = "ssh-rsa AAAAB3Nza............"
}
key_name
在 instance
像这样的资源:resource "aws_instance" "instance" {
ami = var.ami
instance_type = "t2.micro"
count = 1
associate_public_ip_address = true
key_name = "ssh-key"
}
terraform apply
和 terraform output
返回公网 IP ssh <PUBLIC IP>
或使用公钥路径
ssh -i "~/.ssh/id_rsa.pub" <PUBLIC IP>
资料来源:
关于amazon-ec2 - 我可以在 terraform 创建的 ec2 实例中执行 ssh 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59708577/