我不能ssh到terraform中新创建的ec2实例



我不能使用terraform ssh到我新创建的ec2实例

继续获取拒绝的权限(公钥(

我使用ssh keygen-t rsa命令生成了密钥

请帮助我通过这个错误

ssh -i ~/.ssh/ubuntu/mykey ubuntu@44.228.131.143

Root/main.tf,在这里我定义了它,然后传递了变量.tf

module "compute" {
source          = "./compute"
instance_count  = 1
instance_type   = "t3.micro"
public_sg       = module.networking.public_sg
public_subnets  = module.networking.public_subnets
vol_size        = 10
public_key_path = "/home/ubuntu/.ssh/mykey.pub"
key_name        = "mykey"
user_data_path = "${path.root}/userdata.tpl"
db_endpoint     = module.database.db_endpoint
dbuser          = var.dbuser
dbpassword          = var.dbpassword
dbname          = var.dbname
}

计算main.tf,参考我的实例

resource "aws_key_pair" "my_key" {
key_name   = var.key_name
public_key = file(var.public_key_path)
}
resource "aws_instance" "my_instance" {
count         = var.instance_count
instance_type = var.instance_type
ami           = data.aws_ami.server_ami.id
tags = {
Name = "my-node ${random_id.random[count.index].dec}"
}
key_name               = aws_key_pair.my_key.id
vpc_security_group_ids = [var.public_sg]
subnet_id              = var.public_subnets[count.index]
user_data = templatefile(var.user_data_path,
{
nodename    = "my-node ${random_id.random[count.index].dec}"
db_endpoint = var.db_endpoint
dbuser      = var.dbuser
dbpass      = var.dbpassword
dbname      = var.dbname
}
)
root_block_device {
volume_size = var.vol_size
}
}

通过root/main.tf 的变量.tf

variable "instance_count" {}
variable "instance_type" {}
variable "vol_size" {}
variable "public_sg" {}
variable "public_subnets" {}
variable "public_key_path" {}
variable "key_name" {}
variable "db_endpoint" {}
variable "dbname" {}
variable "dbpassword" {}
variable "dbuser" {}
variable "user_data_path" {}
key_name               = aws_key_pair.my_key.key_name

我想我也有同样的问题。这个修复了它。它应该是"key_name"而不是"id">

顺便说一句,您使用的是变量,这提高了代码对不同配置的可重用性。

干杯

最新更新