34. 使用 Terraform 将数据复制到 S3

发布: (2026年2月1日 GMT+8 10:30)
6 min read
原文: Dev.to

I’m happy to translate the article for you, but I’ll need the full text you’d like translated. Could you please paste the content (excluding the source line you already provided) here? Once I have it, I’ll translate it into Simplified Chinese while preserving the original formatting, markdown, and technical terms.

实验信息

Nautilus DevOps 团队目前正在执行数据迁移,将数据从本地存储系统迁移到 AWS S3 桶。他们收到一个需要复制到已有 S3 桶的文件。

  • 桶名称: datacenter-cp-13069(已存在)
  • 要复制的文件: /tmp/datacenter.txt
  • Terraform 工作目录: /home/bob/terraform

注意: 在 VS Code 中,右键点击 EXPLORER 部分下的任意位置,选择 Open in Integrated Terminal 以打开终端。

resource "aws_s3_bucket" "my_bucket" {
  bucket = "datacenter-cp-13069"
  acl    = "private"

  tags = {
    Name = "datacenter-cp-13069"
  }
}

# Upload file to S3 bucket
resource "aws_s3_object" "upload_file" {
  bucket = aws_s3_bucket.my_bucket.bucket
  key    = "datacenter.txt"
  source = "/tmp/datacenter.txt"
  etag   = filemd5("/tmp/datacenter.txt")
}

验证源文件

ls -l /tmp/datacenter.txt

输出

bob@iac-server ~/terraform via 💠 default ➜  ls -l /tmp/datacenter.txt 
-rw-rw-r-- 1 bob bob 27 Nov  8 15:11 /tmp/datacenter.txt

初始化 Terraform

cd /home/bob/terraform
terraform init

输出

bob@iac-server ~/terraform via 💠 default ➜  terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "5.91.0"...
- Installing hashicorp/aws v5.91.0...
- Installed hashicorp/aws v5.91.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

查看执行计划

terraform plan

输出

bob@iac-server ~/terraform via 💠 default ✖ terraform plan
aws_s3_bucket.my_bucket: Refreshing state... [id=datacenter-cp-13069]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_object.upload_file will be created
  + resource "aws_s3_object" "upload_file" {
      + acl                    = (known after apply)
      + arn                    = (known after apply)
      + bucket                 = "datacenter-cp-13069"
      + bucket_key_enabled     = (known after apply)
      + checksum_crc32         = (known after apply)
      + checksum_crc32c        = (known after apply)
      + checksum_crc64nvme     = (known after apply)
      + checksum_sha1          = (known after apply)
      + checksum_sha256        = (known after apply)
      + content_type           = (known after apply)
      + etag                   = "628f77ec27c0e6eb1e0c6543cc3dd865"
      + force_destroy          = false
      + id                     = (known after apply)
      + key                    = "datacenter.txt"
      + kms_key_id             = (known after apply)
      + server_side_encryption = (known after apply)
      + source                 = "/tmp/datacenter.txt"
      + storage_class          = (known after apply)
      + tags_all               = (known after apply)
      + version_id             = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

│ Warning: Argument is deprecated

│   with aws_s3_bucket.my_bucket,
│   on main.tf line 3, in resource "aws_s3_bucket" "my_bucket":
│    3:   acl    = "private"

│ acl is deprecated. Use the aws_s3_bucket_acl resource instead.

│ (and one more similar warning elsewhere)


─────────────────────────────────────────────────────────────────────────────────────────

注意: 您没有使用 -out 选项保存此计划,因此如果现在运行 terraform apply,Terraform 不能保证会执行完全相同的操作。

应用更改

terraform apply

在提示时,键入 yes 以确认创建对象。

输出

bob@iac-server ~/terraform via 💠 default ➜  terraform apply
aws_s3_bucket.my_bucket: Refreshing state... [id=datacenter-cp-13069]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_object.upload_file will be created
  + resource "aws_s3_object" "upload_file" {
      + acl                    = (known after apply)
      + arn                    = (known after apply)
      + bucket                 = "datacenter-cp-13069"
      + bucket_key_enabled     = (known after apply)
      + checksum_crc32         = (known after apply)
      + checksum_crc32c        = (known after apply)
      + checksum_crc64nvme     = (known after apply)
      + checksum_sha1          = (known after apply)
      + checksum_sha256        = (known after apply)
      + content_type           = (known after apply)
      + etag                   = "628f77ec27c0e6eb1e0c6543cc3dd865"
      + force_destroy          = false
      + id                     = (known after apply)
      + key                    = "datacenter.txt"
      + kms_key_id             = (known after apply)
      + server_side_encryption = (known after apply)
      + source                 = "/tmp/datacenter.txt"
      + storage_class          = (known after apply)
      + tags_all               = (known after apply)
      + version_id             = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

│ Warning: Argument is deprecated

│   with aws_s3_bucket.my_bucket,
│   on main.tf line 3, in resource "aws_s3_bucket" "my_bucket":
│    3:   acl    = "private"

│ acl is deprecated. Use the aws_s3_bucket_acl resource instead.

结果: 文件 /tmp/datacenter.txt 现已使用 Terraform 上传至 S3 存储桶 datacenter-cp-13069,对象键为 datacenter.txt

line 3, in resource "aws_s3_bucket" "my_bucket":
│    3:   acl    = "private"

│ acl is deprecated. Use the aws_s3_bucket_acl resource instead.

│ (and one more similar warning elsewhere)


Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_s3_object.upload_file: Creating...
aws_s3_object.upload_file: Creation complete after 0s [id=datacenter.txt]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

资源与后续步骤

  • 📦 完整代码仓库: KodeKloud Learning Labs
  • 📖 更多深度阅读: Whispering Cloud Insights – 阅读其他技术文章
  • 💬 加入讨论: DEV Community – 分享你的想法和问题
  • 💼 保持联系: LinkedIn – 我很乐意与你联系

致谢

  • 所有实验均来自 KodeKloud
  • 我衷心感谢您提供这些宝贵资源。
Back to Blog

相关文章

阅读更多 »