Terraform的文件提供程序看起来成功了,但没有复制文件。

huangapple go评论88阅读模式
英文:

terraform file provisioner looks like it succeeds but doesn't copy file

问题

我正在学习如何使用Terraform来提供AWS资源。在我的当前测试项目中,除其他事项外,我正在创建一个EC2实例,并尝试使用文件提供程序将一些文件复制到服务器上。

我的实例配置如下:

resource "aws_instance" "minecraft_ec2" {
  instance_type = "c6g.large"
  ami           = data.aws_ami.server_ami.id

  key_name        = aws_key_pair.minecraft_auth.key_name
  security_groups = [aws_security_group.minecraft_security_group.id]
  subnet_id       = aws_subnet.minecraft_subnet.id

  user_data = file("userdata.tpl")

  provisioner "file" {
    source      = "./scripts"
    destination = "~/"

    connection {
      type        = "ssh"
      user        = "ec2-user"
      private_key = file("~/.ssh/minecraft_key")
      host        = self.public_ip
      timeout     = "1m"
    }
  }
}

其中./scripts是我基础目录中的一组Python文件(与我的tf文件相同的目录),其中包含我想在实例创建后运行的一些逻辑。

而我提供的SSH私钥与我用作aws_instance密钥的相同:

resource "aws_key_pair" "minecraft_auth" {
  key_name   = "minecraft_key"
  public_key = file("~/.ssh/minecraft_key.pub")
}

当我运行terraform apply来创建或替换AWS实例时,文件提供似乎没有错误(我已经通过设置非常短的超时来强制它出错,当它失败时输出明显不同)。

但是,当我SSH登录到实例以查看情况时,脚本目录似乎没有被传送上去。

我猜测可能有一些故障被吞没或抑制了,但我不确定如何查看它或可能出现什么问题。我可以使用相同的私钥SSH登录到实例,因此连接似乎不应该是问题,但是我不确定问题出在哪里。

有什么建议吗?

英文:

I'm learning how to use terraform to provision aws resources. In my current test project amongst other things I'm creating an ec2 instance and trying to use a file provisioner to copy some files up to the server.

My instance configuration looks like this:

resource "aws_instance" "minecraft_ec2" {
  instance_type = "c6g.large"
  ami           = data.aws_ami.server_ami.id

  key_name        = aws_key_pair.minecraft_auth.key_name
  security_groups = [aws_security_group.minecraft_security_group.id]
  subnet_id       = aws_subnet.minecraft_subnet.id

  user_data = file("userdata.tpl")

  provisioner "file" {
    source      = "./scripts"
    destination = "~/"

    connection {
      type        = "ssh"
      user        = "ec2-user"
      private_key = file("~/.ssh/minecraft_key")
      host        = self.public_ip
      timeout     = "1m"
    }
  }

Where ./scripts is a set of python files in my infrastructure directory (same directory as my tf files) that has some logic I want to run on the instance after it's created.

Terraform的文件提供程序看起来成功了,但没有复制文件。

And the ssh private key that I'm providing is the same one I'm using as the key for my aws_instance:

resource "aws_key_pair" "minecraft_auth" {
  key_name   = "minecraft_key"
  public_key = file("~/.ssh/minecraft_key.pub")
}

When I run my my terraform apply to create or replace the aws instance the file provisioning seems to run without error (I've forced it to error by giving it a super short timeout and the output definitely shows differently when it fails):

Terraform的文件提供程序看起来成功了,但没有复制文件。

but when I ssh into the instance to see how things went the scripts directory has not been scp-ed up:

Terraform的文件提供程序看起来成功了,但没有复制文件。

I'm guessing there's some failure that's being swallowed or suppressed, but I'm not sure how to see it or what could be failing. I can ssh into the instance using the same private key, so it seems like the connection shouldn't be an issue, but yeah I'm not sure where things are going wrong.

Any ideas?

答案1

得分: 0

而不是:

provisioner "file" {
source = "./scripts"
destination = "~/"


尝试:

provisioner "file" {
source = "${path.module}/scripts"
destination = "/home/ec2-user/"


我还建议检查根目录下的 `scripts`,以防万一,因为很奇怪的是,terraform 配置成功,但你找不到它。

最后但同样重要的是,在服务器启动后检查任何配置日志。
英文:

Instead of:

provisioner "file" {
    source      = "./scripts"
    destination = "~/"

try:

provisioner "file" {
    source      = "${path.module}/scripts"
    destination = "/home/ec2-user/"

I would also check your root home directory for scripts just in case since it is weird that terraform provisioned successfully yet you can't find it.

And last but not least check any provisioning logs on the server once it's up and running.

huangapple
  • 本文由 发表于 2023年3月9日 21:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定