Mastering archive_file in Terraform Like a Pro
By Tatiana Mikhaleva · Developer Advocate · Docker Captain · IBM Champion
Hey friends! 💅 So if you’re as obsessed with Terraform as I am (because who doesn’t love automating infrastructure the chic way?!), you NEED to meet the archive_file data source. Think of it as your bestie for packaging files into ZIP or TAR. Deployments get effortless. No more manual zipping, sis—Terraform handles it. 😍
Below: what archive_file actually is, how to build archives from one file or a whole pile of them, and the troubleshooting bits. Because tech gets moody sometimes. 🙄
What is archive_file in Terraform?
Picture archive_file as the glam squad for your code. It grabs your files and bundles them into tidy little ZIP or TAR packages. Super handy for:
✨ Deploying AWS Lambda functions (which require zipped code!) ✨ Packaging Kubernetes manifests or Helm charts ✨ Bundling files before uploading to S3, Azure Storage, or Google Cloud Storage
Setup looks like this:
data "archive_file" "example" { type = "zip" # Options: zip, tar, tgz source_dir = "path/to/source" # Directory to compress output_path = "path/to/output.zip" # Destination of the archive}Working with just one file? Then:
data "archive_file" "example_file" { type = "zip" source_file = "path/to/file.txt" output_path = "path/to/output.zip"}💡 Pro Tip: Use source_dir for entire folders and source_file for single files—but NOT both in the same block. Terraform’s picky like that. 😉
Creating an Archive from a Single File
Say you need to ZIP up one file. Hello, AWS Lambda deployments! Honestly, it could not be easier:
data "archive_file" "example" { type = "zip" source_file = "example.txt" output_path = "example.zip"}
output "archive_checksum" { value = data.archive_file.example.output_base64sha256}That output_base64sha256 is a SHA-256 checksum, so you know your file stayed intact. You don’t need it. But it’s a cute little sanity check. ✅
Example: Zipping a Python Script for AWS Lambda
Picture this: you’ve got lambda_function.py and it needs to land on AWS Lambda.
data "archive_file" "lambda_zip" { type = "zip" source_file = "lambda_function.py" output_path = "lambda_function.zip"}Then wire it into your Lambda function:
resource "aws_lambda_function" "my_lambda" { function_name = "my_lambda_function" role = aws_iam_role.lambda_role.arn runtime = "python3.8" handler = "lambda_function.lambda_handler"
filename = data.archive_file.lambda_zip.output_path source_code_hash = data.archive_file.lambda_zip.output_base64sha256}✨ Heads up! Got dependencies? Then zip the whole folder, not just the script.
Creating Archives from Multiple Files
Okay so, I really wish Terraform let us list multiple files outright. It won’t. Total diva. 👀 The workaround is to gather everything into a temp folder first:
resource "terraform_data" "prepare_files" { provisioner "local-exec" { command = <<EOTmkdir -p temp_foldercp ${path.module}/file1.txt temp_folder/cp ${path.module}/file2.txt temp_folder/cp ${path.module}/file3.txt temp_folder/EOT }}
data "archive_file" "multiple_files" { type = "zip" source_dir = "${path.module}/temp_folder" output_path = "${path.module}/multiple_files.zip" depends_on = [terraform_data.prepare_files]}Just want a whole directory archived? Easy:
data "archive_file" "example" { type = "zip" source_dir = "${path.module}/my_folder" output_path = "${path.module}/example.zip"}Uploading an Archive to Azure Storage
For my Azure girls 💙, here’s how to squish a folder and drop it into an Azure Storage Blob:
data "archive_file" "app_package" { type = "zip" source_dir = "${path.module}/my_app_folder" output_path = "${path.module}/app_package.zip"}Then spin up a storage account and push it:
resource "azurerm_storage_blob" "example" { name = "app_package.zip" storage_account_name = azurerm_storage_account.example.name storage_container_name = azurerm_storage_container.example.name type = "Block" source = data.archive_file.app_package.output_path
depends_on = [data.archive_file.app_package]}Troubleshooting archive_file Issues
Terraform acting up? Queen, take a breath. Here’s how to fix the usual archive_file headaches:
1️⃣ Incorrect Source Path → Check your file paths! Use ${path.module} to avoid mistakes.
2️⃣ Missing zip Utility → Terraform needs zip installed. Fix it with:
- Linux:
sudo apt install zip - macOS:
brew install zip - Windows: Ensure
zip.exeis in your PATH.
3️⃣ Files Not Updating? → Terraform won’t detect changes inside source_dir. Use filemd5() like this:
output "archive_hash" { value = filemd5(data.archive_file.example.output_path)}4️⃣ Permission Issues → If AWS Lambda or Docker throws permission errors, chmod your files before zipping:
chmod +x my_script.sh5️⃣ Wrong Paths in Modules → Always use ${path.module}, not ${path.root}!
Key Takeaways
💖 archive_file is a Terraform essential for packaging files before deployment.
💖 Use source_file for one file and source_dir for entire folders.
💖 Troubleshoot common issues with paths, permissions, and missing utilities.
Terraform is fun (and powerful), so go forth and automate like the IT queen you are! 👩💻✨