1

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.

1
  • 1
    did you try to write any code?
    – c4f4t0r
    Apr 17 '20 at 10:53
4

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.

0

Your question is not clear.

  1. What do you want to achieve? (i.e. why do you need the zipfile? is that configuration, or deployment, or something else)
  2. Is this a Terraform question at all? (i.e. do you need that zipfile during the Terraform run? or is Terraform your only hammer and you are searching for something else?)
  3. What have you tried so far?

Assuming this you do not need the file in Terraform, then I would suggest to write a cloud-init script for the file download. Use Terraform to configure all IAM permissions, and to write the script into the instance's user-data. Then at boot time the server runs cloud-init and will download the file, either with curl/wget (from public URLs) or with aws s3 (authenticated via instance profile).

0

It's not clear but I think you're asking how to download a file to an instance you've created with terraform?

That should be done via a user_data script or by configuration management software on your instance's image. There is no resource in terraform that will do this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.