doc_noob
doc_noob

Reputation: 625

Download files from S3 using terraform locally

I am trying to download files from s3 bucket to the server in which i am running terraform, is this possible? i tried the below code

data "aws_s3_bucket_objects" "my_objects" {
  bucket = "examplebucket"
}

data "aws_s3_bucket_object" "object_info" {
  key    = "${element(data.aws_s3_bucket_objects.my_objects.keys, count.index)}"
  bucket = "${data.aws_s3_bucket_objects.my_objects.bucket}"

}
provisioner "local-exec" {
    content = "${data.aws_s3_bucket_object.object_info.body}"
 }

When i run terraform plan i am getting the below error

Error: Unsupported block type

  on s3.tf line 11:
  11: provisioner "local-exec" {

Blocks of type "provisioner" are not expected here.

Am i missing something here? Any help on this would be appreciated.

Upvotes: 3

Views: 10269

Answers (2)

provisioner "local-exec" must be inside some resource, and there is no argument content for it. You might execute some command line or a custom scripts there only. See the domumentation https://www.terraform.io/docs/language/resources/provisioners/local-exec.html#example-usage

Upvotes: 0

dmkvl
dmkvl

Reputation: 768

Just use the local provider

data "aws_s3_bucket_objects" "my_objects" {
  bucket = "examplebucket"
  //prefix = "your_prefix"
}    

data "aws_s3_bucket_object" "object_info" {
  count  = "${length(data.aws_s3_bucket_objects.my_objects.keys)}"
  key    = "${element(data.aws_s3_bucket_objects.my_objects.keys, count.index)}"
  bucket = "${data.aws_s3_bucket_objects.my_objects.bucket}"

}

resource "local_file" "foo" {
    count    = "${length(data.aws_s3_bucket_objects.my_objects.keys)}"
    content  = "${data.aws_s3_bucket_object.object_info[count.index].body}"
    filename = "/path/to/file-${count.index}"
}

PS: make sure your objects have a human-readable Content-Type, the body field is available only for such objects.

Upvotes: 5

Related Questions