Coder Social home page Coder Social logo

realfireau / worker-tfstate Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 137 KB

A cloudflare worker for storing terraform state using HTTP backend

License: GNU General Public License v3.0

TypeScript 100.00%
cloudflare terraform terraform-backend terraform-state

worker-tfstate's Introduction

Worker-tfstate

This project is a CloudFlare Worker that stores the state of a Terraform workspace in a KV namespace.

Usage

terraform {
  backend "http" {
    address = "https://worker.example.com/api/v1/example"
  }
}

Configuration

The following environment variables are required:

  • USERNAME: The username to authenticate with
  • PASSWORD: The password to authenticate with (wrangler secret put PASSWORD)
  • NAMESPACE_ID: The ID of the KV namespace to use (needs to be bound to the worker) wrangler.toml

Development

Prerequisites

Setup

npm install

Deployment

wrangler publish

API Reference

All endpoints require basic authentication with the configured username and password.

GET /api/v1/:project

Returns the state of the project as JSON.

curl -X GET https://worker.example.com/api/v1/example -u username:password

Example response - state not found

HTTP/1.1 404 Not Found
Content-Type: application/json;charset=UTF-8
Content-Length: 0
Cache-Control: no-store

Example response - state found

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Content-Length: 123
{"version": 4, "terraform_version": "0.12.24", "serial": 4, "lineage": "e2b0f0e0-7f1a-4c4a-8b1a-2b1a0b1a0b1a", "outputs": {}, "resources": []}

POST /api/v1/:project

Updates the state of the project with the JSON body.

curl -X POST https://worker.example.com/api/v1/example -u username:password -H "Content-Type: application/json" -d '{"version": 4, "terraform_version": "0.12.24", "serial": 4, "lineage": "e2b0f0e0-7f1a-4c4a-8b1a-2b1a0b1a0b1a", "outputs": {}, "resources": []}'

Example response - state updated

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Content-Length: 123
{"version": 4, "terraform_version": "0.12.24", "serial": 4, "lineage": "e2b0f0e0-7f1a-4c4a-8b1a-2b1a0b1a0b1a", "outputs": {}, "resources": []}

LOCK /api/v1/:project

Locks the statefile.

curl -X LOCK https://worker.example.com/api/v1/example -u username:password

Example response - state locked

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store

Example response - state already locked

HTTP/1.1 423 Locked
Content-Type: application/json
Cache-Control: no-store
{"id": "e2b0f0e0-7f1a-4c4a-8b1a-2b1a0b1a0b1a", "operation": "lock", "who": "username", "created": "2020-05-01T00:00:00Z"}

UNLOCK /api/v1/:project

Unlocks the statefile.

Example response - state unlocked

HTTP/1.1 200 OK
Cache-Control: no-store

DELETE /api/v1/:project

Deletes the statefile.

Example response - state deleted

HTTP/1.1 200 OK
Cache-Control: no-store

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

worker-tfstate's People

Contributors

dependabot[bot] avatar realfireau avatar

Watchers

 avatar

worker-tfstate's Issues

V2 API

For V2 api looking to add several enhancements.

  • Ability to generate per project username/passwords
  • Ability to generate readonly project credentials for local dev (can get state but can't lock/unlock/update/delete)
  • Removal of /:project from the api to be replaced by per-project usernames

KV is not durable

I will need to implement a solution for updates to a KV value during the TTL of the record as it can lead to the lock having no effect if the update occurs to quickly.

An example is where I wrote script to import the state of 40 objects however only half the records made it to the final state due to the TTL

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.