8.Sync Data to S3 Bucket with Terraform
Source: Dev.to

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– definesKKE_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

Simple Explanation
| Requirement | How the solution satisfies it |
|---|---|
Create a new private S3 bucket using variables.tf | Bucket name is taken from var.KKE_BUCKET; ACL is set to private. |
| Migrate all data from the existing bucket | aws s3 sync copies every object while preserving the folder structure. |
| Ensure data consistency | The sync operation can be re‑run safely; source and destination will match. |
| Use Terraform for everything | Terraform provisions the new bucket, sets its ACL, and triggers the migration via a null_resource. |
Do not create a separate .tf file for resources | All resources are added to the existing main.tf; only variables.tf and outputs.tf are separate as required. |
Resources & Next Steps
- Full Code Repository: KodeKloud Learning Labs
- More Deep Dives: Whispering Cloud Insights
- Discussion Community: DEV Community
Credits
- Lab content originally from KodeKloud.