【问题标题】:SSH isnt working in Windows with Terraform provisioner connection typeSSH 在 Windows 中无法使用 Terraform 配置程序连接类型
【发布时间】:2019-11-11 21:15:20
【问题描述】:

我尝试使用 Terraform 在 AWS 中创建实例,并尝试将一组文件复制到新创建的 AWS 实例中。我同样使用了“provisioner”,但对于连接,它总是说连接超时。

在下面的示例中,我展示了它的 AWS Pem 文件,但我尝试了 ppk 和 pem 文件。没有任何效果。

provider "aws" {
    region = "ap-southeast-1"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}
resource "aws_instance" "firsttest" {
    ami = "ami-061eb2b23f9f8839c"
    instance_type = "t2.micro"
    key_name = "deepak"
    provisioner "file" {
        source      = "index.html"
        destination = "/home/ubuntu/index.html"
    connection {
        type     = "ssh"
        user     = "ubuntu"
        private_key = file("D:/awskeyterraform/deepak.pem")
        host = "${aws_instance.firsttest.public_ip}"
        }
    }
    user_data = <<-EOF
        #!/bin/bash
        apt-get update -y
        apt-get install -y nginx
        systemctl enable nginx
        service nginx restart
        touch index.html
        EOF
    tags = {
        name = "terraform-firsttest"
    }
}

预期应该复制 index.html,但实际连接超时以连接到新创建的实例

【问题讨论】:

  • 您没有定义任何安全组(AWS 采用有状态防火墙),因此没有任何东西可以进出实例(包括 SSH)。因为这是 AWS 的一个非常重要的方面,所以我现在会放弃 terraform 并尝试使用控制台创建实例以了解 EC2 的基础知识,然后跳上 terraform。否则你会打两场大战。
  • 我已将默认安全组修改为 any: any,当我从 Linux 执行相同但仅在 Windows 中出现问题时,它工作正常。我可以理解 Linux 默认有 SSH 代理但在 windows 中没有!!!只是好奇如何在没有在 windows 中安装任何 ssh-agent 的情况下处理这种情况。
  • @DeenaDeepak 你应该可以使用 WinRM 配置器
  • @ScottHeath,感谢您的建议。 WinRM Provisioner 适用于 Windows VM,但我设法通过将“deepak.pem”重命名为“id_rsa”来使其工作。它按预期工作。

标签: terraform terraform-provider-aws


【解决方案1】:

在 Windows 中,SSH 模块连接不接受“*.pem”。相反,它在将 PEM 文件重命名为“id_rsa”后接受它。

provider "aws" {
    region = "ap-southeast-1"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}
resource "aws_instance" "firsttest" {
    ami = "ami-061eb2b23f9f8839c"
    instance_type = "t2.micro"
    key_name = "deepak"
    provisioner "file" {
        source      = "index.html"
        destination = "/home/ubuntu/index.html"
    connection {
        type     = "ssh"
        user     = "ubuntu"
        private_key = "${file("D:/awskeyterraform/id_rsa")}"
        host = "${aws_instance.firsttest.public_ip}"
        }
    }
    user_data = <<-EOF
        #!/bin/bash
        apt-get update -y
        apt-get install -y nginx
        systemctl enable nginx
        service nginx restart
        touch index.html
        EOF
    tags = {
        name = "terraform-firsttest"
    }
}

希望这能解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多