🐶
Terraform

Terraform Archive File Creation Guide

By Ondřej Dolanský on 01/07/2025

Learn how to use Terraform to create archive files like zip, tar, and gzip for efficient infrastructure provisioning and deployment.

Terraform Archive File Creation Guide

Table of Contents

Introduction

The archive_file resource is deprecated. You should now use the archive_file data source to create archive files in your Terraform code.

Step-by-Step Guide

The archive_file resource is deprecated. Use the archive_file data source instead.

Here's how to create an archive file using the archive_file data source:

  1. Define the data source:

    data "archive_file" "example" {
      type        = "zip"
      output_path = "example.zip"
      source {
        content  = "example content"
        filename = "example.txt"
      }
    }

    This code defines a data source named "example" that creates a zip archive named "example.zip". The archive contains a single file named "example.txt" with the content "example content".

  2. Reference the archive file:

    You can reference the created archive file using the output_path attribute:

    resource "aws_s3_object" "example" {
      bucket = "my-bucket"
      key    = "example.zip"
      source = data.archive_file.example.output_path
    }

    This code defines an AWS S3 object resource that uploads the "example.zip" file to the "my-bucket" bucket.

Key points:

  • Use source to specify individual files to include in the archive.
  • Use source_dir to include the entire contents of a directory.
  • You can use for_each and fileset to iterate over multiple files or directories.
  • The output_file_mode attribute allows you to set the file permissions for the archived files.
  • The archive_file data source is evaluated during the planning phase, so the archive file is created before any resources that depend on it.

Code Example

This Terraform code defines infrastructure that creates a zip archive containing HTML, CSS, and images from an 'images' directory. It then uploads this archive to an AWS S3 bucket. The code configures the AWS provider, sets variables for the bucket name and region, creates the S3 bucket, defines the archive content and structure, uploads the archive to the bucket, and finally outputs the URL of the uploaded archive.

This example demonstrates how to create a zip archive containing multiple files and a directory, then upload it to an AWS S3 bucket using the archive_file data source.

# Configure the AWS Provider
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure variables for S3 bucket and region
variable "bucket_name" {
  description = "Name of the S3 bucket"
  type = string
}

variable "region" {
  description = "AWS region"
  type = string
  default = "us-west-2"
}

# Create an S3 bucket
resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name
  region = var.region

  force_destroy = true # Use with caution! This will delete all objects in the bucket before deleting the bucket itself.
}

# Define the archive_file data source
data "archive_file" "website_archive" {
  type        = "zip"
  output_path = "website.zip"

  source {
    content  = "<h1>Welcome to my website!</h1>"
    filename = "index.html"
  }

  source {
    content  = "body { font-family: sans-serif; }"
    filename = "styles.css"
  }

  source_dir {
    path = "images"
  }
}

# Upload the archive to S3
resource "aws_s3_object" "example_archive" {
  bucket = aws_s3_bucket.example.id
  key    = "website.zip"
  source = data.archive_file.website_archive.output_path

  # Make the object publicly readable
  acl    = "public-read"
}

# Output the S3 object URL
output "s3_object_url" {
  value = "https://${aws_s3_bucket.example.bucket_regional_domain_name}/${aws_s3_object.example_archive.key}"
}

Explanation:

  1. Provider and Variables: This section defines the AWS provider and sets up variables for the S3 bucket name and region.
  2. S3 Bucket Creation: An S3 bucket is created using the provided variables.
  3. archive_file Data Source: This section defines the archive_file data source named "website_archive". It specifies:
    • type: The archive type, in this case, "zip".
    • output_path: The path where the archive file will be created ("website.zip").
    • source: Defines individual files to include in the archive:
      • An "index.html" file with HTML content.
      • A "styles.css" file with CSS content.
    • source_dir: Includes the entire contents of the "images" directory in the archive.
  4. S3 Object Upload: This section defines an aws_s3_object resource that uploads the created "website.zip" file to the S3 bucket.
  5. Output: The code outputs the URL of the uploaded S3 object.

To use this code:

  1. Create a directory named "images" in the same directory as your Terraform file.
  2. Place any images you want to include in the archive inside the "images" directory.
  3. Run terraform init to initialize the provider.
  4. Run terraform apply to create the resources and upload the archive.

This example demonstrates a basic use case for the archive_file data source. You can adapt it to your specific needs by modifying the file content, adding more files or directories, and changing the archive type. Remember to replace the placeholder values with your actual bucket name and region.

Additional Notes

  • Why the change? Deprecating the archive_file resource and favoring the archive_file data source promotes better Terraform practices. Data sources are designed for fetching and computing data, making them a more appropriate tool for creating archives, which are essentially derived data.
  • Idempotency: The archive_file data source helps ensure idempotency in your Terraform code. Since the archive is created during the planning phase, Terraform can determine if the archive needs to be recreated based on changes to its content or configuration.
  • Flexibility: The archive_file data source offers flexibility in defining archive content. You can:
    • Include individual files with specific content.
    • Include entire directories.
    • Use for_each and fileset for dynamic archive generation based on multiple files or directories.
  • Security: Remember to manage the permissions (using output_file_mode) of the generated archive, especially if it contains sensitive information.
  • Alternatives: While archive_file is a convenient option for simple scenarios, consider dedicated tools like zip, tar, or build systems for more complex archiving needs, especially when dealing with large files or performance is critical.
  • Real-world applications: This approach is highly useful for packaging and deploying applications, configurations, or website assets.

Remember: Always consult the official Terraform documentation for the archive_file data source for the most up-to-date information and detailed usage instructions: https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file

Summary

This article explains how to create archive files in Terraform using the archive_file data source, which replaces the deprecated archive_file resource.

Here's a breakdown:

  1. Define the data source: Use data "archive_file" "name" {} to define the archive. Specify the archive type (zip, tar, etc.), output path, and the files to include using the source block. You can include individual files or entire directories.

  2. Reference the archive: Access the created archive file path using the output_path attribute of the data source. This path can be used in other resources, like uploading to cloud storage.

Key advantages of using the archive_file data source:

  • Flexibility: Include individual files or entire directories using source and source_dir.
  • Iteration: Use for_each and fileset to dynamically create archives from multiple sources.
  • Control: Set file permissions within the archive using output_file_mode.
  • Efficiency: The archive is created during the planning phase, ensuring resources depending on it have access.

Conclusion

The archive_file data source in Terraform provides a robust and flexible way to create archives, replacing the deprecated archive_file resource. By defining the archive content and structure within your Terraform code, you can ensure consistent and reproducible deployments. This approach is particularly useful for packaging and deploying applications, configurations, or website assets to various platforms. Remember to leverage the power of for_each and fileset for dynamic archive generation and manage file permissions carefully. For complex scenarios or performance-critical applications, consider using dedicated archiving tools alongside Terraform. Always refer to the official Terraform documentation for the most up-to-date information on the archive_file data source.

References

  • How do you create an archive file in Terraform? - Stack Overflow How do you create an archive file in Terraform? - Stack Overflow | Jan 12, 2018 ... In archive_file module you can't use source(specifies attributes of a single source file to include into the archive) and source_dir(Package entire contents of ...
  • archive_file | Resources | hashicorp/archive | Terraform | Terraform ... archive_file | Resources | hashicorp/archive | Terraform | Terraform ... | output_file_mode (String) String that specifies the octal file mode for all archived files. For example: "0666" . Setting this will ensure that cross platform ...
  • Avoid repeating code for archiving files in a directory terraform ... Avoid repeating code for archiving files in a directory terraform ... | Jul 2, 2022 ... You can combine for_each and fileset for this: data "archive_file" "from_s3" { for_each = fileset("${path.module}", "lambda_functions/*.py") ...
  • archive_file data source gets created during "terraform plan" vs ... archive_file data source gets created during "terraform plan" vs ... | Terraform CLI and Provider Versions Terraform v1.4.6 on linux_amd64 provider registry.terraform.io/hashicorp/archive v2.3.0 provider registry.terraform.io/hashicorp/google v4.66.0 provider registry...
  • terraform - Using archive_file as a resource is deprecated - Stack ... terraform - Using archive_file as a resource is deprecated - Stack ... | Jul 6, 2022 ... The data source creates an archive file during the plan while the resource creates ... How do you create an archive file in Terraform? 4 · How to ...
  • zip multiple folders : r/Terraform zip multiple folders : r/Terraform | Posted by u/Accomplished_Text_10 - 1 vote and 5 comments
  • How to upload a non-terraform file to Terraform cloud - HCP ... How to upload a non-terraform file to Terraform cloud - HCP ... | I have a Terraform configuration that that references some non-terraform files. When I try to apply this configuration on a workspace with remote execution, terraform fails with an error like this: Error: Invalid function argument on static_site_cdn.tf line 89, in resource "aws_cloudfront_function" "static_web_request_function": 89: code = file("${local.bin_root}/cloudfront_function/static_website_request_function.js") |---------------- | local.bin_root is "./../bin" Invalid val...
  • Lambda functions in terraform : r/Terraform Lambda functions in terraform : r/Terraform | Posted by u/r-pwned - 7 votes and 19 comments
  • Create an archive and upload it with Terraform • Jamie Phillips Create an archive and upload it with Terraform • Jamie Phillips | Terraform is such a versatile tool and supports many features that are always fun to discover. Today we are going to learn about the Archive provider and the File provisioner. The Archive provider allows you to create a zip file of a single file, a directory, or content generated withing your Terraform template. The second feature we will be levarging is the file provisioner, which allows you to upload a file or directory to a remote server using either SSH or Windows Remoting. The file provisioner will need to be used as a section within a resource. A typical example would be to upload a file to a virtual machine resource. However, you can also leverage it with a Null resource. A null resource is a last resort tool that I should probably cover more in depth in a future blog post.

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait