tidying up and using s3 remote for tfstate

This commit is contained in:
tom.chivert committed 2026-01-28 17:23:47 +01:00
1 parent a5a5e77447
commit 5055e9ba92
8 files changed
+230 -116

No files matched your search

+78 -48
View File
@@ -2,67 +2,97 @@ name: Deploy Configuration
on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]
env:
TERRAFORM_VERSION: "1.10.5"
jobs:
deploy:
plan:
runs-on: ubuntu-latest
strategy:
matrix:
hosts: ["git.rznet.fr", "mirror.rznet.fr"]
environment: ["git.rznet.fr", "mirror.rznet.fr"]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.TERRAFORM_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -p ${{ vars.TERRAFORM_SSH_PORT }} ${{ vars.TERRAFORM_SSH_HOST }} >> ~/.ssh/known_hosts
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- name: SSH - Git pull
run: |
ssh -i ~/.ssh/id_ed25519 -p ${{ vars.TERRAFORM_SSH_PORT }} terraform@${{ vars.TERRAFORM_SSH_HOST }} << 'EOF'
cd terraform-bunnynet/${{ matrix.hosts }}
git pull
EOF
- name: Terraform init
working-directory: ${{ matrix.environment }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_KEY }}
run: terraform init -upgrade
- name: SSH - Terraform init
run: |
ssh -i ~/.ssh/id_ed25519 -p ${{ vars.TERRAFORM_SSH_PORT }} terraform@${{ vars.TERRAFORM_SSH_HOST }} << 'EOF'
cd terraform-bunnynet/${{ matrix.hosts }}
export BUNNYNET_API_KEY="${{ secrets.BUNNYNET_API_KEY }}"
terraform init -upgrade
EOF
- name: Terraform format check
working-directory: ${{ matrix.environment }}
run: terraform fmt -check
- name: SSH - Terraform format check
run: |
ssh -i ~/.ssh/id_ed25519 -p ${{ vars.TERRAFORM_SSH_PORT }} terraform@${{ vars.TERRAFORM_SSH_HOST }} << 'EOF'
cd terraform-bunnynet/${{ matrix.hosts }}
export BUNNYNET_API_KEY="${{ secrets.BUNNYNET_API_KEY }}"
terraform fmt -check
EOF
- name: Terraform validate
working-directory: ${{ matrix.environment }}
run: terraform validate
- name: SSH - Terraform plan
run: |
ssh -i ~/.ssh/id_ed25519 -p ${{ vars.TERRAFORM_SSH_PORT }} terraform@${{ vars.TERRAFORM_SSH_HOST }} << 'EOF'
cd terraform-bunnynet/${{ matrix.hosts }}
export BUNNYNET_API_KEY="${{ secrets.BUNNYNET_API_KEY }}"
export VAULT_ADDR="${{ secrets.VAULT_ADDR }}"
export VAULT_TOKEN="${{ secrets.VAULT_TOKEN }}"
terraform plan
EOF
- name: Terraform plan
working-directory: ${{ matrix.environment }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_KEY }}
BUNNYNET_API_KEY: ${{ secrets.BUNNYNET_API_KEY }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
VAULT_TOKEN: ${{ secrets.VAULT_TOKEN }}
run: terraform plan -out=tfplan
- name: SSH - Terraform apply
run: |
ssh -i ~/.ssh/id_ed25519 -p ${{ vars.TERRAFORM_SSH_PORT }} terraform@${{ vars.TERRAFORM_SSH_HOST }} << 'EOF'
cd terraform-bunnynet/${{ matrix.hosts }}
export BUNNYNET_API_KEY="${{ secrets.BUNNYNET_API_KEY }}"
export VAULT_ADDR="${{ secrets.VAULT_ADDR }}"
export VAULT_TOKEN="${{ secrets.VAULT_TOKEN }}"
terraform apply -auto-approve
EOF
- name: Upload plan artifact
uses: actions/upload-artifact@v4
with:
name: tfplan-${{ matrix.environment }}
path: ${{ matrix.environment }}/tfplan
retention-days: 1
apply:
needs: plan
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
strategy:
matrix:
environment: ["git.rznet.fr", "mirror.rznet.fr"]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- name: Download plan artifact
uses: actions/download-artifact@v4
with:
name: tfplan-${{ matrix.environment }}
path: ${{ matrix.environment }}
- name: Terraform init
working-directory: ${{ matrix.environment }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_KEY }}
run: terraform init -upgrade
- name: Terraform apply
working-directory: ${{ matrix.environment }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_KEY }}
BUNNYNET_API_KEY: ${{ secrets.BUNNYNET_API_KEY }}
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
VAULT_TOKEN: ${{ secrets.VAULT_TOKEN }}
run: terraform apply -auto-approve tfplan
+17
View File
@@ -0,0 +1,17 @@
terraform {
backend "s3" {
bucket = "rznet-terraform-state"
key = "bunnycdn/git.rznet.fr/terraform.tfstate"
endpoints = {
s3 = "https://s3.fr-par.scw.cloud"
}
region = "fr-par"
# Required for non-AWS S3 providers
skip_credentials_validation = true
skip_metadata_api_check = true
skip_region_validation = true
skip_requesting_account_id = true
}
}
+12 -8
View File
@@ -1,19 +1,23 @@
variable "name" {
type = string
default = "git-rznet"
type = string
default = "git-rznet"
description = "The name of the pullzone"
}
variable "domain" {
type = string
default = "bunny.git.rznet.fr"
type = string
default = "bunny.git.rznet.fr"
description = "The domain name for the pullzone"
}
variable "origin" {
type = string
default = "https://rznet.fr"
type = string
default = "https://rznet.fr"
description = "The origin URL"
}
variable "origin_host" {
type = string
default = "git.rznet.fr"
type = string
default = "git.rznet.fr"
description = "The host header to send to the origin"
}
+17
View File
@@ -0,0 +1,17 @@
terraform {
backend "s3" {
bucket = "rznet-terraform-state"
key = "bunnycdn/mirror.rznet.fr/terraform.tfstate"
endpoints = {
s3 = "https://s3.fr-par.scw.cloud"
}
region = "fr-par"
# Required for non-AWS S3 providers
skip_credentials_validation = true
skip_metadata_api_check = true
skip_region_validation = true
skip_requesting_account_id = true
}
}
+36 -25
View File
@@ -1,66 +1,77 @@
variable "name" {
type = string
default = "mirror-rznet"
type = string
default = "mirror-rznet"
description = "The name of the pullzone"
}
variable "domain" {
type = string
default = "mirror.rznet.fr"
type = string
default = "mirror.rznet.fr"
description = "The domain name for the pullzone"
}
variable "origin" {
type = string
default = "https://rznet.fr"
type = string
default = "https://rznet.fr"
description = "The origin URL"
}
variable "origin_host" {
type = string
default = "mirror.rznet.fr"
type = string
default = "mirror.rznet.fr"
description = "The host header to send to the origin"
}
variable "origin_verify_ssl" {
type = bool
default = false
type = bool
default = false
description = "Whether to verify SSL certificates for the origin"
}
variable "routing_tier" {
type = string
default = "Volume"
description = "Standard or Volume"
description = "The routing tier (Standard or Volume)"
}
variable "routing_zones" {
type = list(any)
default = []
type = list(string)
default = []
description = "List of routing zones"
}
variable "block_post_requests" {
type = bool
default = true
type = bool
default = true
description = "Whether to block POST requests"
}
variable "cache_vary" {
type = list(any)
type = list(string)
default = []
description = "avif, cookie, country, hostname, mobile, querystring, state, webp"
description = "List of cache vary options (avif, cookie, country, hostname, mobile, querystring, state, webp)"
}
variable "limit_download_speed" {
type = number
default = 62500
type = number
default = 62500
description = "Download speed limit in bytes per second"
}
variable "limit_bandwidth" {
type = number
default = 10000000000000
type = number
default = 10000000000000
description = "Bandwidth limit in bytes"
}
variable "websockets_enabled" {
type = bool
default = false
type = bool
default = false
description = "Whether WebSockets are enabled"
}
variable "certificate_vault" {
type = bool
default = true
type = bool
default = true
description = "Whether to use Vault for certificate management"
}
+25 -26
View File
@@ -1,5 +1,27 @@
locals {
certificate = sensitive(
var.certificate != "" ? file(var.certificate) : (
var.certificate_vault ? data.vault_kv_secret_v2.ssl_cert[0].data["cert"] : null
)
)
certificate_key = sensitive(
var.certificate_key != "" ? file(var.certificate_key) : (
var.certificate_vault ? data.vault_kv_secret_v2.ssl_cert[0].data["key"] : null
)
)
}
data "vault_kv_secret_v2" "ssl_cert" {
count = var.certificate_vault ? 1 : 0
mount = "secret"
name = "cdn/ssl/${var.domain}"
}
resource "bunnynet_pullzone" "service" {
name = var.name
origin {
type = var.origin_type
url = var.origin
@@ -14,16 +36,14 @@ resource "bunnynet_pullzone" "service" {
filters = var.routing_filters
}
block_post_requests = var.block_post_requests
block_post_requests = var.block_post_requests
errorpage_whitelabel = var.errorpage_whitelabel
websockets_enabled = var.websockets_enabled
cache_vary = var.cache_vary
cache_stale = var.cache_stale
cache_chunked = var.cache_chunked
websockets_enabled = var.websockets_enabled
errorpage_whitelabel = var.errorpage_whitelabel
limit_bandwidth = var.limit_bandwidth
limit_download_speed = var.limit_download_speed
}
@@ -56,24 +76,3 @@ output "hostname" {
value = bunnynet_pullzone_hostname.service.name
description = "The hostname configured for the pullzone"
}
data "vault_kv_secret_v2" "ssl_cert" {
count = var.certificate_vault ? 1 : 0
mount = "secret"
name = "cdn/ssl/${var.domain}"
}
locals {
certificate = sensitive(
var.certificate != "" ? file(var.certificate) : (
var.certificate_vault ? data.vault_kv_secret_v2.ssl_cert[0].data["cert"] : null
)
)
certificate_key = sensitive(
var.certificate_key != "" ? file(var.certificate_key) : (
var.certificate_vault ? data.vault_kv_secret_v2.ssl_cert[0].data["key"] : null
)
)
}
+5 -3
View File
@@ -1,12 +1,14 @@
terraform {
required_version = ">= 1.0"
required_providers {
bunnynet = {
source = "BunnyWay/bunnynet"
version = ">=0.11.6"
source = "BunnyWay/bunnynet"
version = ">= 0.11.6"
}
vault = {
source = "hashicorp/vault"
version = ">=3.0"
version = ">= 3.0"
}
}
}
+40 -6
View File
@@ -1,31 +1,52 @@
variable "name" {
type = string
default = ""
description = "The name of the pullzone"
validation {
condition = length(var.name) > 0
error_message = "The pullzone name cannot be empty."
}
}
variable "domain" {
type = string
default = ""
description = "The domain name for the pullzone"
validation {
condition = length(var.domain) > 0
error_message = "The domain cannot be empty."
}
}
variable "origin" {
type = string
default = ""
description = "The origin URL for the pullzone"
validation {
condition = can(regex("^https?://", var.origin))
error_message = "The origin must be a valid HTTP(S) URL."
}
}
variable "origin_host" {
type = string
default = ""
description = "The host header to send to the origin"
validation {
condition = length(var.origin_host) > 0
error_message = "The origin host cannot be empty."
}
}
variable "origin_type" {
type = string
default = "OriginUrl"
description = "The type of origin (OriginUrl, StorageZone, etc.)"
validation {
condition = contains(["OriginUrl", "StorageZone"], var.origin_type)
error_message = "The origin_type must be either 'OriginUrl' or 'StorageZone'."
}
}
variable "origin_verify_ssl" {
@@ -50,6 +71,11 @@ variable "routing_tier" {
type = string
default = "Standard"
description = "The routing tier (Standard or Volume)"
validation {
condition = contains(["Standard", "Volume"], var.routing_tier)
error_message = "The routing_tier must be either 'Standard' or 'Volume'."
}
}
variable "routing_zones" {
@@ -98,26 +124,34 @@ variable "limit_bandwidth" {
type = number
default = 1000000000000
description = "Bandwidth limit in bytes"
validation {
condition = var.limit_bandwidth > 0
error_message = "The bandwidth limit must be a positive number."
}
}
variable "limit_download_speed" {
type = number
default = 12500
description = "Download speed limit in bytes per second"
validation {
condition = var.limit_download_speed > 0
error_message = "The download speed limit must be a positive number."
}
}
variable "certificate" {
type = string
default = ""
description = "Path to the certificate file"
sensitive = true
}
variable "certificate_key" {
type = string
default = ""
description = "Path to the certificate key file"
sensitive = true
}
variable "certificate_vault" {