https://github.com/kamuridesu/tf-backend-go
Custom HTTP backend for Terraform/OpenTofu in Go with AWS Lambda support
https://github.com/kamuridesu/tf-backend-go
aws dynamodb gin-gonic go golang lambda postgresql sqlite terraform
Last synced: 3 months ago
JSON representation
Custom HTTP backend for Terraform/OpenTofu in Go with AWS Lambda support
- Host: GitHub
- URL: https://github.com/kamuridesu/tf-backend-go
- Owner: kamuridesu
- License: mit
- Created: 2024-02-16T21:29:41.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-15T17:46:41.000Z (over 1 year ago)
- Last Synced: 2025-04-09T23:06:52.603Z (over 1 year ago)
- Topics: aws, dynamodb, gin-gonic, go, golang, lambda, postgresql, sqlite, terraform
- Language: Go
- Homepage:
- Size: 66.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TF Backend Go
A jump to the sky turns to a Rider Kick
## Intro
A simple HTTP backend to store Terraform/OpenTofu state information using SQLite or Postgres as database.
It also works as an AWS Lambda Serverless service using API Gateway as HTTP frontend and DynamoDB as storage.
## Build
### Linux
```
CGO_ENABLED=1 go build -ldflags='-s -w -extldflags "-static"' -o tf-backend
```
### Lambda
Just run the `build.sh` script to generate the main.zip file. In case you want to run the commands yourself:
```sh
CGO_ENABLED=0 go build -ldflags='-s -w -extldflags "-static"' -o bootstrap # Build project
zip main.zip bootstrap # Stores it into a main.zip file
rm bootstrap # removes the executable file
```
In case you're using Windows, just follow this documentation: https://docs.aws.amazon.com/lambda/latest/dg/golang-package.html#golang-package-windows
## AWS Setup
### Lambda
Create a new Lambda Function, give it the name you want and as for the runtime select `Amazon Linux 2023`. Choose the arch compatible with your build and click Create Function.
Then, in Configurations -> Environment Variables, add these following variables:
```
ACCESS_KEY=
SECRET_ACCESS_KEY=
AUTH_USERS=
```
The access key and secret access key should be created on AWS. Just follow the docs. For the AUTH_USERS env it should be in this format: USER:PASSWORD;USER2:PASSWORD2
Then just upload the .zip file containing the executable.
### API Gateway
#### New API gateway
Click Create API, then HTTP API. Add Lambda integration and search for the Lambda Function you created. Select it and give it a name.
Then create a route that starts with / and another that starts with /{proxy+}, the later will be used to give a name to the Terraform State. The method for all routes should be `ANY`.
After creating the routes just integrate it with the Lambda Function you created. Get the URL clicking on Deploy -> Stages.
#### Existing API Gateway
If you already has an API Gateway, just setup it to use {proxy+} in a route and integrate it to the lambda function. Remember, the method should be ANY.
### DynamoDB
Create a new table called `tfstates` with Name as Partition Key and Locked as Sort Key. Leave the other options as default.
## Routes
- `tfstates/:name` -> route for Terraform Operations