8.Sync Data to S3 Bucket with Terraform

Published: (February 7, 2026 at 12:58 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Cover image for 8.Sync Data to S3 Bucket with Terraform

Lab Information

As part of a data migration project, the team must:

  • Create a new private S3 bucket named devops-sync-31997 and store the bucket name in a variable KKE_BUCKET.
  • Migrate all data from the existing bucket devops-s3-17362 to the new bucket.
  • Verify that both buckets contain identical data after migration.

All of this should be done using Terraform, updating the existing main.tf (no separate resource files). The required files are:

  • variables.tf – defines KKE_BUCKET.
  • outputs.tf – exposes the new bucket name and ACL.

Lab Solutions

1️⃣ variables.tf

variable "KKE_BUCKET" {
  type = string
}

2️⃣ terraform.tfvars

KKE_BUCKET = "devops-sync-31997"

3️⃣ Update main.tf (append to the existing code)

# Existing bucket (source)
resource "aws_s3_bucket" "wordpress_bucket" {
  bucket = "devops-s3-17362"
}

resource "aws_s3_bucket_acl" "wordpress_bucket_acl" {
  bucket = aws_s3_bucket.wordpress_bucket.id
  acl    = "private"
}

# New private bucket (destination)
resource "aws_s3_bucket" "sync_bucket" {
  bucket = var.KKE_BUCKET
}

resource "aws_s3_bucket_acl" "sync_bucket_acl" {
  bucket = aws_s3_bucket.sync_bucket.id
  acl    = "private"
}

# Data migration using the AWS CLI
resource "null_resource" "s3_sync" {
  provisioner "local-exec" {
    command = "aws s3 sync s3://devops-s3-17362 s3://${var.KKE_BUCKET}"
  }

  depends_on = [
    aws_s3_bucket.sync_bucket
  ]
}

4️⃣ outputs.tf

output "new_kke_bucket_name" {
  value = aws_s3_bucket.sync_bucket.bucket
}

output "new_kke_bucket_acl" {
  value = aws_s3_bucket_acl.sync_bucket_acl.acl
}

5️⃣ Apply the configuration

terraform init
terraform validate
terraform apply   # type "yes" when prompted

Terraform apply output

Simple Explanation

RequirementHow the solution satisfies it
Create a new private S3 bucket using variables.tfBucket name is taken from var.KKE_BUCKET; ACL is set to private.
Migrate all data from the existing bucketaws s3 sync copies every object while preserving the folder structure.
Ensure data consistencyThe sync operation can be re‑run safely; source and destination will match.
Use Terraform for everythingTerraform provisions the new bucket, sets its ACL, and triggers the migration via a null_resource.
Do not create a separate .tf file for resourcesAll resources are added to the existing main.tf; only variables.tf and outputs.tf are separate as required.

Resources & Next Steps

Credits

0 views
Back to Blog

Related posts

Read more »

14.Provision IAM User with Terraform

Lab Information The Nautilus DevOps team is experimenting with Terraform provisioners. Your task is to create an IAM user and use a local-exec provisioner to l...