使用 terraform 将目录从本地复制到 ec2

Copy directory from local to ec2 using terraform

我正在尝试将本地目录复制到 ec2 实例。如果我复制一个文件而不是目录,它工作正常。任何帮助将不胜感激。

resource "null_resource" "cp_ansible" {
  depends_on = [null_resource.provisioner]

  triggers = {
    always_run = timestamp()
  }

  provisioner "file" {
    source      = "../ansible/"
    destination = "/home/ec2-user/ansible/"

    connection {
      type        = "ssh"
      host        = aws_instance.bastion.public_ip
      user        = "ec2-user"
      private_key = file("/tmp/keys/ec2-key")
      insecure    = true
    }
  }
}

错误:

│ Error: file provisioner error
│ 
│   with null_resource.cp_ansible,
│   on inventory.tf line 68, in resource "null_resource" "cp_ansible":
│   68:   provisioner "file" {
│ 
│ Upload failed: scp: /home/ec2-user/ansible/: Is a directory

你应该使用:

provisioner "file" {
  source      = "../ansible"
  destination = "/home/ec2-user"

  connection {
    type        = "ssh"
    host        = aws_instance.bastion.public_ip
    user        = "ec2-user"
    private_key = file("/tmp/keys/ec2-key")
    insecure    = true
  }
}

来自documentation

The existence of a trailing slash on the source path will determine whether the directory name will be embedded within the destination, or whether the destination will be created. For example:

  • If the source is /foo (no trailing slash), and the destination is /tmp, then the contents of /foo on the local machine will be uploaded to /tmp/foo on the remote machine. The foo directory on the remote machine will be created by Terraform.