TerraformPilot

Terraform

Terraform Archive Provider - Create ZIP and TAR Files

Use the Terraform archive provider to create ZIP files for Lambda functions, Cloud Functions, and deployments. archive_file data source with source_dir and...

LLuca Berton1 min read

Quick Answer

#
data "archive_file" "lambda" {
  type        = "zip"
  source_dir  = "${path.module}/lambda"
  output_path = "${path.module}/lambda.zip"
}
 
resource "aws_lambda_function" "main" {
  filename         = data.archive_file.lambda.output_path
  source_code_hash = data.archive_file.lambda.output_base64sha256
  # ...
}

Zip a Directory

#
data "archive_file" "app" {
  type        = "zip"
  source_dir  = "${path.module}/src"
  output_path = "${path.module}/dist/app.zip"
 
  excludes = [
    "node_modules",
    ".git",
    "*.test.js",
    "__pycache__",
  ]
}

Zip a Single File

#
data "archive_file" "handler" {
  type        = "zip"
  source_file = "${path.module}/handler.py"
  output_path = "${path.module}/handler.zip"
}

Zip from Inline Content

#
data "archive_file" "config" {
  type        = "zip"
  output_path = "${path.module}/config.zip"
 
  source {
    content  = jsonencode({ environment = var.environment })
    filename = "config.json"
  }
 
  source {
    content  = templatefile("${path.module}/templates/startup.sh", { port = var.port })
    filename = "startup.sh"
  }
}

Multiple Sources

#
data "archive_file" "deployment" {
  type        = "zip"
  output_path = "${path.module}/deployment.zip"
 
  source {
    content  = file("${path.module}/src/main.py")
    filename = "main.py"
  }
 
  source {
    content  = file("${path.module}/src/utils.py")
    filename = "utils.py"
  }
 
  source {
    content  = file("${path.module}/requirements.txt")
    filename = "requirements.txt"
  }
}

AWS Lambda with Dependencies

#
# Build step: install dependencies
resource "terraform_data" "pip_install" {
  triggers_replace = [filemd5("${path.module}/requirements.txt")]
 
  provisioner "local-exec" {
    command = "pip install -r requirements.txt -t ${path.module}/build/python/lib/python3.12/site-packages/ --upgrade"
  }
}
 
# Lambda layer (dependencies)
data "archive_file" "layer" {
  type        = "zip"
  source_dir  = "${path.module}/build"
  output_path = "${path.module}/dist/layer.zip"
  depends_on  = [terraform_data.pip_install]
}
 
resource "aws_lambda_layer_version" "deps" {
  filename            = data.archive_file.layer.output_path
  source_code_hash    = data.archive_file.layer.output_base64sha256
  layer_name          = "${var.project}-deps"
  compatible_runtimes = ["python3.12"]
}
 
# Lambda function (application code only)
data "archive_file" "function" {
  type        = "zip"
  source_dir  = "${path.module}/src"
  output_path = "${path.module}/dist/function.zip"
}
 
resource "aws_lambda_function" "main" {
  filename         = data.archive_file.function.output_path
  source_code_hash = data.archive_file.function.output_base64sha256
  function_name    = var.project
  handler          = "main.handler"
  runtime          = "python3.12"
  layers           = [aws_lambda_layer_version.deps.arn]
  role             = aws_iam_role.lambda.arn
}

GCP Cloud Functions

#
data "archive_file" "function" {
  type        = "zip"
  source_dir  = "${path.module}/function-source"
  output_path = "${path.module}/function-source.zip"
}
 
resource "google_storage_bucket_object" "source" {
  name   = "function-${data.archive_file.function.output_md5}.zip"
  bucket = google_storage_bucket.source.name
  source = data.archive_file.function.output_path
}

Useful Attributes

#
AttributeDescription
output_pathPath to the created archive
output_sizeFile size in bytes
output_base64sha256Base64-encoded SHA256 (for Lambda)
output_md5MD5 hash (for GCS object naming)
output_shaSHA1 hash
#

Conclusion

#

Use archive_file to package Lambda functions, Cloud Functions, and deployment artifacts directly in Terraform. The source_code_hash/output_md5 attributes trigger redeployment when source code changes. Use excludes to skip test files and dependencies.

#Terraform#DevOps#Infrastructure as Code

Share this article