669 words
3 minutes

Mastering archive_file in Terraform Like a Pro

By · Developer Advocate · Docker Captain · IBM Champion
Side view of a clean white office desk with the edge of a silver iMac, an Apple Magic Keyboard and Magic Mouse, a ceramic pen-pot, and a white Mophie wireless charger neatly arranged

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 = <<EOT
mkdir -p temp_folder
cp ${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.exe is 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:

Terminal window
chmod +x my_script.sh

5️⃣ 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! 👩‍💻✨


Tatiana Mikhaleva

Docker Captain  ·  IBM Champion  ·  AWS Community Builder

DevOps.Pink — cloud-native education for the agentic-AI era.

Related Posts

Same category
  1. 1
    How to Secure AI Agents in Production: IBM's Six-Phase Framework
    DevOps & Cloud · Teams secure AI agents like normal software, and production breaks. Here's IBM and Anthropic's six-phase framework for securing them, phase by phase.
  2. 2
    Your AI Agent Doesn't Need a Better Prompt. It Needs a Ceiling
    DevOps & Cloud · A prompt is not a security control. It's a wish. The Vault → Sentinel → MCP → ADLC → watsonx Orchestrate stack that gives AI agents a hard ceiling — and why IBM consolidating HashiCorp made the whole thing boring, in the best possible way.
  3. 3
    CNCF Q1 2026 Report — Why Feature Flagging Is the Hidden Gateway to Cloud Native Maturity
    DevOps & Cloud · CNCF Q1 2026 cloud native report analysis. Why feature flagging is the bridge from mainstream to advanced engineering practice, with exclusive commentary from the report's author.
  4. 4
    AI SRE Joined My On-Call — A Beginner-Friendly Walkthrough of Rootly
    DevOps & Cloud · What an AI SRE actually does on call. A hands-on walkthrough of Rootly — how it observes, advises, and (when you let it) acts. With a real look at the four-level trust model.

Random Posts

Random
  1. 1
    How Generative AI Actually Understands You
    AI & MLOps · Discover how generative AI understands text, images, video, and sound — explained simply with real examples of tokens, chunks, and embeddings.
  2. 2
    Kubernetes Is No Longer Number One — The REAL 2025 Cloud Native Report (CNCF x SlashData)
    DevOps & Cloud · Kubernetes is no longer number one. The 2025 CNCF x SlashData report reveals the real cloud-native trends — backend growth, DevOps adoption, AI gaps, and the technologies developers actually use.
  3. 3
    How AI Models Are Really Trained - From Idea to Reality
    AI & MLOps · Learn how AI models are trained step by step — from data prep to deployment. Simple, beginner-friendly guide with real-life examples.
  4. 4
    Amazon Q - The AI DevOps Tool That Fixes AWS Headaches
    AI & MLOps · Amazon Q is AWS's AI assistant that helps DevOps engineers fix cloud issues faster with smart, context-aware insights and automation.
Mastering archive_file in Terraform Like a Pro
https://devops.pink/mastering-archive-file-in-terraform-like-a-pro/
Author
Tatiana Mikhaleva
Published
2025-02-27
License
CC BY-NC-SA 4.0