670 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 — Signal over noise in cloud-native & AI.

Pink Signal

The cloud-native stack, without the gatekeeping

Docker, Kubernetes, and the agentic-AI era — hands-on and clearly explained, for engineers who actually ship. Pink Signal lands in your inbox when there's something worth knowing.

Related Posts

Same category
  1. 1
    The Terminal Was Never the Point: Managing Parallel AI Coding Agents
    DevOps & Cloud · Claude Code, Codex, and Copilot agents now edit your repo in parallel, and a terminal shows one at a time. Why visual Git tooling became the control tower.
  2. 2
    The Runbook You Didn't Write Is Already Running in Production
    DevOps & Cloud · Your vendor's opinion of how to run their product now ships as an executable AI agent skill. Six places it breaks in production, and six questions to ask first.
  3. 3
    The Industry Is Hiring DevRel for Yesterday
    DevOps & Cloud · Developer Relations builds the AI future, then hires its advocates with 2012 HR scripts. Eight symptoms of DevRel hiring broken by fear, and the fix.
  4. 4
    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.

Random Posts

Random
  1. 1
    What Actually Runs the Internet? A No-Stress Guide to Containers & Kubernetes
    DevOps & Cloud · Discover how containers, Docker, Kubernetes & ContainerD power modern apps — explained simply, even for total beginners.
  2. 2
    Docker MCP - How GPT Agents Now Use Slack, GitHub, Stripe & More
    AI & MLOps · Learn how Docker and MCP let GPT agents use tools like Slack, GitHub, and Stripe — turning AI from smart talkers into real-world doers.
  3. 3
    Docker Compose Explained - Imagine Running Your Own Cafe
    DevOps & Cloud · Docker Compose explained with a fun cafe analogy! Learn how to run and deploy apps easily with containers—perfect for beginners and IT pros alike.
  4. 4
    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 practice, with commentary from the author.
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