Listen to this Post
When working with Terraform for Infrastructure as Code (IaC), you may need to upload archive files containing multiple items rather than individual files. A common use case is AWS Lambda deployments, where bundling code into a ZIP file is more efficient. Terraform’s `archive_file` data source helps automate this process while ensuring updates only occur when file contents change.
You Should Know: How to Use Terraform’s `archive_file`
1. Basic `archive_file` Example
The `archive_file` data source creates a ZIP file from specified sources. Below is a basic configuration:
[hcl]
data “archive_file” “lambda_zip” {
type = “zip”
source_dir = “${path.module}/lambda_code”
output_path = “${path.module}/lambda_function.zip”
}
[/hcl]
2. Hashing for Change Detection
Terraform uses file hashing to determine if the archive needs rebuilding. If no files change, Terraform skips recreating the ZIP.
[hcl]
output “archive_hash” {
value = data.archive_file.lambda_zip.output_base64sha256
}
[/hcl]
3. Using with AWS Lambda
Deploy the ZIP file to AWS Lambda efficiently:
[hcl]
resource “aws_lambda_function” “example” {
filename = data.archive_file.lambda_zip.output_path
function_name = “example_lambda”
role = aws_iam_role.lambda_role.arn
handler = “index.handler”
runtime = “nodejs14.x”
}
[/hcl]
4. Filtering Files in the Archive
Exclude unnecessary files (e.g., `node_modules`) using `excludes`:
[hcl]
data “archive_file” “lambda_zip” {
type = “zip”
source_dir = “${path.module}/lambda_code”
output_path = “${path.module}/lambda_function.zip”
excludes = [“node_modules/**”]
}
[/hcl]
5. Dynamic File Inclusion
Use `dynamic` blocks to conditionally include files:
[hcl]
dynamic “source” {
for_each = fileset(“${path.module}/lambda_code”, “*.js”)
content {
source_file = source.value
output_path = “js/${source.value}”
}
}
[/hcl]
6. Linux & Windows Commands for File Handling
- Linux (Bash)
</li> </ul> <h1>Check ZIP file hash (SHA-256)</h1> openssl dgst -sha256 lambda_function.zip <h1>List ZIP contents</h1> unzip -l lambda_function.zip
- Windows (PowerShell)
</li> </ul> <h1>Get file hash</h1> Get-FileHash -Algorithm SHA256 lambda_function.zip <h1>Extract ZIP</h1> Expand-Archive -Path lambda_function.zip -DestinationPath output_dir
7. Automating with Terraform `null_resource`
Trigger external scripts for pre-processing before archiving:
[hcl]
resource “null_resource” “pre_archive” {
triggers = {
always_run = timestamp()
}provisioner “local-exec” {
command = “npm install –prefix ${path.module}/lambda_code”
}
}
[/hcl]What Undercode Say
Terraform’s `archive_file` is a powerful tool for managing bundled deployments in IaC workflows. By leveraging file hashing, it optimizes performance by avoiding unnecessary uploads. Combining this with AWS Lambda, automation scripts, and proper file filtering ensures a streamlined DevOps process.
For further reading, check the original article:
Terraform Archive_File Data Source
Expected Output:
A Terraform-managed ZIP file deployed to AWS Lambda only when source files change, reducing redundant operations.
References:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅Join Our Cyber World:
- Windows (PowerShell)



