An HTTP state backend for the OpenTofu/Terraform framework that provides serverless state management via IAM token authentication. This backend allows you to store and retrieve OpenTofu/Terraform state files using HTTP requests with IAM-based authentication.
- π IAM Token Authentication: Secure authentication using IAM tokens from headers or context
- ποΈ S3-Compatible Storage: Uses Minio client for S3-compatible object storage
- π Serverless Ready: Designed to work as a serverless function handler
- π¦ Automatic Bucket Management: Creates buckets automatically when needed
- π Optional Encryption: Supports KMS-based encryption for state files
- π Versioning Support: Optional state file versioning
The main handler function can be used with any serverless platform (AWS Lambda, Yandex Cloud Functions, etc.):
const { handler } = require('@64mb/tofu-http-state-backend');
// Use with your serverless platform
exports.myFunction = handler;terraform {
backend "http" {
address = "<lambda invoke http endpoint (TF_HTTP_ADDRESS)>"
headers = {
"Authorization" = "Bearer <IAM-token from TF_VAR_>"
"X-State-Bucket" = "<S3 state bucket>"
"X-State-Prefix" = "<S3 state file name>.tfstate.json"
"X-State-Versioning" = true
}
}
required_version = ">= 1.10.0" # OpenTofu
}Configure the backend using these environment variables:
| Variable | Description | Default |
|---|---|---|
STORAGE_ENDPOINT |
S3-compatible storage endpoint | Required |
STORAGE_PORT |
Storage port number | 443 |
STORAGE_ACCESS_KEY |
Storage access key | Required |
STORAGE_SECRET_KEY |
Storage secret key | Required |
IAM_TOKEN_HEADER |
Header name for IAM token | Authorization |
The backend provides a RESTful API for state management:
curl -X GET "https://your-function-url" \
-H "Authorization: Bearer <iam-token>" \
-H "X-State-Bucket: bucket" \
-H "X-State-Prefix: terraform.tfstate"curl -X POST "https://your-function-url" \
-H "Authorization: Bearer <iam-token>" \
-H "X-State-Bucket: bucket" \
-H "X-State-Prefix: terraform.tfstate" \
-H "X-State-Versioning: true" \
-H "X-State-Encryption-Key: kms-key-id" \
-d '{"version": 1, "serial": 1, "terraform_version": "1.0.0"}'curl -X DELETE "https://your-function-url" \
-H "Authorization: Bearer <iam-token>" \
-H "X-State-Bucket: bucket" \
-H "X-State-Prefix: terraform.tfstate"| Header | Description | Required |
|---|---|---|
Authorization |
IAM token (Bearer format) | Yes* |
X-State-Bucket |
S3 bucket name | No (default: tofu-http-state-bucket) |
X-State-Prefix |
State file key/prefix | No (default: tfstate.json) |
X-State-Versioning |
Enable versioning (true/false) |
No |
X-State-Encryption-Key |
KMS key ID for encryption | No |
*Either Authorization header or IAM token in context is required
The backend supports two methods of IAM token authentication:
- Authorization Header:
Authorization: Bearer <token> - Context Token: IAM token provided in the function context
- Node.js 18+
- npm
# Clone the repository
git clone https://github.com/64mb/tofu-http-state-backend.git
cd tofu-http-state-backend
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Lint code
npm run lint# Build the distribution package
npm run buildThis creates a zip file in the dist/ directory ready for deployment to serverless platforms.
The project includes a comprehensive test suite using tap and s3rver for mocking S3-compatible storage:
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode during development
npm run test:watchtofu-http-state-backend/
βββ index.js # Main handler function
βββ package.json # Project configuration
βββ build.js # Build script for deployment
βββ test/ # Test suite
β βββ index.test.js # Main test file
β βββ utils.js # Test utilities
βββ tfplan/ # Terraform plan visualizer (subproject)
β βββ src/ # React source code
β βββ public/ # Static assets
β βββ README.md # TFPlan documentation
βββ biome.json # Code formatting configuration
βββ LICENSE # Apache 2.0 License
- Build the package:
npm run build - Upload the
dist/tofu-http-state-backend.zipto Lambda - Configure environment variables
- Set the handler to
index.handler
- Build the package:
npm run build - Create a new function in the Yandex Cloud console
- Upload the zip file from
dist/ - Configure environment variables
- Set the entry point to
index.handler
This repository also includes tfplan, a OpenTofu/Terraform plan visualizer:
- Location:
tfplan/ - Purpose: Visualize OpenTofu/Terraform plan outputs in a user-friendly format
- Features: Color-coded changes, browser-based, single HTML file output
See the TFPlan README for more details.
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature - Make your changes and ensure tests pass
- Run the linter:
npm run lint - Commit your changes:
git commit -am 'Add new feature' - Push to the branch:
git push origin feature/new-feature - Submit a pull request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
For support and questions:
- Create an issue in the GitHub repository
- Contact: team@64mb.ru