29. 使用 Terraform 从 S3 删除备份

发布: (2026年1月31日 GMT+8 13:30)
5 min read
原文: Dev.to

Source: Dev.to

29. 使用 Terraform 从 S3 删除备份的封面图片

Thu Kha Kyawe

实验信息

Nautilus DevOps 团队目前正在进行一次清理工作,重点是从他们的 AWS 账户中移除不必要的数据和服务。作为迁移过程的一部分,已经创建了一些仅供一次性使用的资源,需要进行清理以优化其 AWS 环境。

  • 已存在一个名为 nautilus-bck-29479 的 S3 桶。

任务

  1. nautilus-bck-29479 S3 桶的内容复制到 terraform-client 主机(即加载本实验后得到的登录主机)上的 /opt/s3-backup/ 目录。
  2. 删除 S3 桶 nautilus-bck-29479
  3. 使用 Terraform 中的 AWS CLI 来完成此任务(例如,在 Terraform 中运行 AWS CLI 命令)。
    • Terraform 工作目录为 /home/bob/terraform
    • 更新 main.tf 文件不要创建单独的 .tf 文件)以实现上述任务。

注意: 在 VS Code 的 EXPLORER 区域右键单击并选择 Open in Integrated Terminal 以打开终端。

Source:

实验解答

第 1 步 – 创建主 Terraform 配置

# main.tf

# Execute AWS CLI commands to copy and delete the S3 bucket
resource "null_resource" "s3_cleanup" {
  provisioner "local-exec" {
    command = <<EOT
      echo "Creating backup directory..."
      mkdir -p /opt/s3-backup/

      echo "Copying contents from S3 bucket to local directory..."
      aws s3 cp s3://nautilus-bck-29479 /opt/s3-backup/ --recursive

      echo "Deleting the S3 bucket..."
      aws s3 rb s3://nautilus-bck-29479 --force
    EOT
  }
}

第 2 步 – 部署配置

2.1 进入 Terraform 目录

cd /home/bob/terraform

2.2 初始化 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.

2.3 规划部署以验证配置

terraform plan

输出

bob@iac-server ~/terraform via 💠 default ➜  terraform plan

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:

  # null_resource.s3_cleanup will be created
  + resource "null_resource" "s3_cleanup" {
      + id = (known after apply)
    }

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

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

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to
take exactly these actions if you run "terraform apply" now.

2.4 应用配置

terraform apply

当出现提示时,键入 yes 进行确认。

输出

bob@iac-server ~/terraform via 💠 default ➜  terraform apply

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:

  # null_resource.s3_cleanup will be created
  + resource "null_resource" "s3_cleanup" {
      + id = (known after apply)
    }

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

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

null_resource.s3_cleanup: Creating...
null_resource.s3_cleanup: Provisioning with 'local-exec'...
null_resource.s3_cleanup (local-exec): Executing: ["/bin/sh" "-c" "      echo \"Creating backup directory...\"\n      mkdir -p /opt/s3-backup/\n\n      echo \"Copying contents from S3 bucket to local directory...\"\n      aws s3 cp s3://nautilus-bck-29479 /opt/s3-backup/ --recursive\n\n      echo \"Deleting the S3 bucket...\"\n      aws s3 rb s3://nautilus-bck-29479 --force\n"]
null_resource.s3_cleanup (local-exec): Creating backup directory...
null_resource.s3_cleanup (local-exec): Copying contents from S3 bucket to local directory...
null_resource.s3_cleanup (local-exec): Deleting the S3 bucket...
null_resource.s3_cleanup: Creation complete after 3s [id=123456]

7890]

应用完成!资源:新增 1 项,修改 0 项,销毁 0 项。

Result: S3 存储桶 nautilus-bck-29479 已备份至 /opt/s3-backup/,随后被删除,整个过程由 Terraform 编排。

_cleanup (local-exec): Completed 27 Bytes/27 Bytes (2.7 KiB/s) with 1 file(s) remaining
null_resource.s3_cleanup (local-exec): download: s3://nautilus-bck-29479/nautilus.txt to ../../../opt/s3-backup/nautilus.txt
null_resource.s3_cleanup (local-exec): Deleting the S3 bucket...
null_resource.s3_cleanup (local-exec): delete: s3://nautilus-bck-29479/nautilus.txt
null_resource.s3_cleanup (local-exec): remove_bucket: nautilus-bck-29479
null_resource.s3_cleanup: Creation complete after 1s [id=4571548781870379384]

**应用完成!资源:新增 1 项,修改 0 项,销毁 0 项。**

资源与后续步骤

致谢

  • 所有实验均来自: KodeKloud
  • 我真诚地感谢您提供这些宝贵的资源。
Back to Blog

相关文章

阅读更多 »