使用 terraform 从 aws s3 下载 zip 文件

log*_*gax 1 amazon-s3 amazon-web-services terraform

我似乎找不到可以帮助我使用 terraform 将 zip 文件从 aws s3 下载到实例的文档,有人可以帮我找到解决方案吗?

谢谢。

lim*_*kzi 5

根据您的需要,有多种方法可以从 S3 下载文件。


  • 选项 1.1。您可以使用remote-exec配置程序。这是MIME不可知论者。
resource "aws_instance" "web" {
  ## [...]

  provisioner "remote-exec" {
    command = "curl -XGET [...]"
  }
}
Run Code Online (Sandbox Code Playgroud)
resource "null_resource" "cluster" {
  # Changes to any instance of the cluster requires re-provisioning
  triggers = {
    cluster_instance_ids = "${join(",", aws_instance.cluster.*.id)}"
  }

  # Bootstrap script can run on any instance of the cluster
  # So we just choose the first in this case
  connection {
    host = "${element(aws_instance.cluster.*.public_ip, 0)}"
  }

  provisioner "remote-exec" {
    # Bootstrap script called with private_ip of each node in the cluster
    inline = [
      "bootstrap-cluster.sh ${join(" ", aws_instance.cluster.*.private_ip)}",
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

它将与文本文件完美配合。

data "aws_s3_bucket_object" "secret_key" {
    bucket = "awesomecorp-secret-keys"
    key    = "awesomeapp-secret-key"
}

resource "aws_instance" "web" {
  ## [...]
  provisioner "file" {
    content     = data.aws_s3_bucket_object.secret_key.body
    destination = /tmp/file
  }
}
Run Code Online (Sandbox Code Playgroud)
resource "aws_instance" "web" {
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"
  user_data     = [...]

  tags = {
    Name = "HelloWorld"
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您从实例执行命令,或者向执行Terraform.

注意:也就是说,我怀疑Terraform这是实例配置的最佳选择。看看SaltStack AnsibleChef。这些是设计用于实例配置的工具。