29.Delete Backup from S3 Using Terraform

Published: (January 31, 2026 at 12:30 AM EST)
4 min read
Source: Dev.to

Source: Dev.to

Cover image for 29.Delete Backup from S3 Using Terraform

Thu Kha Kyawe

Lab Information

The Nautilus DevOps team is currently engaged in a cleanup process, focusing on removing unnecessary data and services from their AWS account. As part of the migration process, several resources were created for one‑time use only, necessitating a cleanup effort to optimize their AWS environment.

  • An S3 bucket named nautilus-bck-29479 already exists.

Tasks

  1. Copy the contents of the nautilus-bck-29479 S3 bucket to /opt/s3-backup/ on the terraform-client host (the landing host once you load this lab).
  2. Delete the S3 bucket nautilus-bck-29479.
  3. Use the AWS CLI through Terraform to accomplish this task (e.g., by running AWS CLI commands within Terraform).
    • The Terraform working directory is /home/bob/terraform.
    • Update the main.tf file (do not create a separate .tf file) to accomplish this task.

Note: Right‑click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.

Lab Solutions

Step 1 – Create Main Terraform Configuration

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

Step 2 – Deploy the Configuration

2.1 Navigate to the Terraform directory

cd /home/bob/terraform

2.2 Initialize Terraform

terraform init

Output

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 Plan the deployment to verify the configuration

terraform plan

Output

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 Apply the configuration

terraform apply

When prompted, type yes to confirm.

Output

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=1234567890]

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

Result: The S3 bucket nautilus-bck-29479 has been backed up to /opt/s3-backup/ and then removed, all orchestrated via 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]

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

Resources & Next Steps

Credits

  • All labs are from: KodeKloud
  • I sincerely appreciate your provision of these valuable resources.
Back to Blog

Related posts

Read more »

34.Copy Data to S3 Using Terraform

Lab Information The Nautilus DevOps team is currently performing data migrations, moving data from on‑premise storage systems to AWS S3 buckets. They have rece...

30.Delete EC2 Instance Using Terraform

markdown !Cover image for 30.Delete EC2 Instance Using Terraformhttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/htt...