【问题标题】:Terraform and S3 - How to upload files to an existing bucketTerraform 和 S3 - 如何将文件上传到现有存储桶
【发布时间】:2019-09-14 05:10:00
【问题描述】:

如何在不破坏存储桶内容或重新创建存储桶的情况下将文件上传到现有 s3 存储桶(由 terraform 基础架构创建)。

terraform {
  backend "s3" {
    bucket = "terraformtests"
    key    = "terraformstate.tf"
    region = "us-east-1"
  }
}


resource "aws_s3_bucket_object" "terraformtests" {
  bucket = "terraformtests"
  key    = "test/prod/1000/keys"
  source = "deploy"
  etag   = "${md5(file("keys"))}"

}

这就是我所拥有的。它在第一次运行时运行良好并将其上传到键空间“1000”。当我编辑它并使用 1001 重新运行时,脚本会尝试破坏之前在 test/prod/1000/keys 中创建的文件。我要做的就是

  • 导入现有表状态而不重新创建它
  • 将文件夹添加到键空间,而不触及表的先前内容。

【问题讨论】:

  • 您可能没有正确使用 terraform。 Terraform 配置语言是声明性的。它描述了您的基础设施应该处于的状态。它维护所有已创建对象的状态并删除那些不再描述的对象(例如 /test/prod/1000/keys 中的文件)。我建议使用其他方式将文件移动到 s3 存储桶。最简单的是 bash 脚本恕我直言。

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


【解决方案1】:

您得到的结果是 Terraform 所期望的。

TF 在 .tfstate 文件中维护基础设施的“状态”。它们是上次同步时基础架构的镜像。

基础架构的所需状态在 .tf 文件中进行了描述。

运行terraform planterraform apply 命令比较.tf.tfstate 文件。然后,任何存在于 .tfstate 而不是 .tf 的资源都会被销毁

在您的情况下,它在上次运行的 .tfstate 中有 keyspace 1000,但在 .tf 中没有,因为您对其进行了编辑到1001

解决方法

  1. 使用 AWS CLI,正如 Winston 评论中所建议的那样
  2. 为键空间 1001 添加另一个资源,而不是编辑您拥有的资源

另外,我注意到你有

source = "deploy"
etag = "${md5(file("keys"))}"

通常,它们应该引用同一个文件:

source = "readme.md"
etag = "${filemd5("readme.md")}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2021-12-06
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2018-02-07
    相关资源
    最近更新 更多