Using terraform to download zip file from aws s3

amazon s3amazon-web-servicesterraform

I can't seem to find a documentation that will help me download a zip file from aws s3 to an instance using terraform, can someone help me find a solution to this ?

Thank you.

Best Answer

There are various ways to download file from S3 depending on your needs.


  • Option 1.1. You can use remote-exec provisioner. This one is MIME agnostic.
resource "aws_instance" "web" {
  ## [...]

  provisioner "remote-exec" {
    command = "curl -XGET [...]"
  }
}
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)}",
    ]
  }
}

It will work perfectly with text files.

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
  }
}

resource "aws_instance" "web" {
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"
  user_data     = [...]

  tags = {
    Name = "HelloWorld"
  }
}

Remember to give an instance an IAM role with proper permissions, if you execute command from the instance, or give proper permissions to role, that executes Terraform.

Note: Said that, I doubt Terraform is best choice for instance provisioning. Take a look at SaltStack, Ansible or Chef. These are the tools designed to work with instance provisioning.