【问题标题】:Download files from S3 using terraform locally在本地使用 terraform 从 S3 下载文件
【发布时间】:2020-08-15 00:56:51
【问题描述】:

我正在尝试将文件从s3 存储桶下载到我正在运行terraform 的服务器,这可能吗?我试过下面的代码

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

当我运行terraform plan 时,出现以下错误

Error: Unsupported block type

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

Blocks of type "provisioner" are not expected here.

我在这里错过了什么吗?对此的任何帮助将不胜感激。

【问题讨论】:

    标签: amazon-s3 terraform terraform-provider-aws


    【解决方案1】:

    只需使用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:请确保您的对象具有human-readable Content-Typebody 字段仅适用于此类对象。

    【讨论】:

    • 感谢回复,收到以下错误Error: Missing resource instance key on s3.tf line 12, in resource "local_file" "foo": 12: content = "${data.aws_s3_bucket_object.object_info.body}" Because data.aws_s3_bucket_object.object_info has "count" set, its attributes must be accessed on specific instances. For example, to correlate with indices of a referring resource, use: data.aws_s3_bucket_object.object_info[count.index]
    • @doc_noob,修复了答案
    • 谢谢,我做了一个plan,它成功了,你能告诉我如何访问bucket 中的特定文件夹吗?例如:/bucket/folder1/newfiles/
    • aws_s3_bucket_objects资源terraform.io/docs/providers/aws/d/s3_bucket_objects.html使用prefix参数
    • 当我给prefix 像这样prefix = "/folder1/newfiles" 时,它会显示在warning Warning: Interpolation-only expressions are deprecated on s3.tf line 24, in resource "local_file" "foo": 24: count = "${length(data.aws_s3_bucket_objects.my_objects.keys)}" 下方
    【解决方案2】:

    provisioner "local-exec" 必须在某个资源中,并且没有 content 的参数。您可能只在那里执行一些命令行或自定义脚本。 查看文档https://www.terraform.io/docs/language/resources/provisioners/local-exec.html#example-usage

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 2021-10-24
      • 2020-11-27
      • 2020-04-09
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      相关资源
      最近更新 更多