This module provides a reusable way to manage file uploads with OpenAI. It supports both creating new files and importing existing ones into Terraform.
- Upload files to OpenAI with various purposes (fine-tune, assistants, vision, etc.)
- Import existing files into Terraform state
- Lifecycle management to avoid recreating imported files
- Automatic detection of import mode
module "fine_tune_upload" {
source = "../modules/upload"
purpose = "fine-tune"
file_path = "./training_data.jsonl"
}- Define the resource in your Terraform configuration (as above)
- Import the file using the Terraform CLI:
terraform import module.fine_tune_upload.openai_file.file file-abc123The module will automatically detect that this is an imported file and handle it appropriately.
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| purpose | The intended purpose of the uploaded file | string |
n/a | yes |
| file_path | Path to the file to upload | string |
"" |
yes for new files, ignored for imports |
| project_id | The ID of the OpenAI project to associate this upload with | string |
"" |
no |
| Name | Description |
|---|---|
| file_id | The unique identifier for the uploaded file |
| filename | The name of the uploaded file |
| bytes | The size of the file in bytes |
| created_at | The timestamp when the upload was created |
| purpose | The purpose of the file |
The module implements a special handling mechanism for imports:
- It uses a placeholder file path for imported files
- The lifecycle configuration ignores changes to the file path attribute
- Import detection works by checking if the file path exists or is the placeholder
This allows seamless management of both new and existing files without requiring recreation.
# Create a new file upload
module "new_file" {
source = "../modules/upload"
purpose = "assistants"
file_path = "./assistant_data.jsonl"
}
# Reference an imported file (after terraform import)
module "existing_file" {
source = "../modules/upload"
purpose = "fine-tune"
file_path = "./imported_file.jsonl" # This path will be ignored for imported files
}- The file path is required for new file creation but is ignored for imported files
- Changes to other attributes like purpose will require resource recreation
- When a file is imported, its metadata is fetched from the OpenAI API
Different upload purposes require different file types:
- fine-tune:
text/jsonl