@@ -274,15 +274,18 @@ jobs:
274274
275275 echo "🏗️ Creating backend infrastructure for: $environment"
276276
277- # Function to create a single bucket using Terraform
277+ # Function to create a single bucket using the terraform-backend module
278278 create_bucket() {
279279 local env="$1"
280280 local bucket_name="terraform-state-coder-$env"
281281 local temp_dir=$(mktemp -d)
282282
283283 echo "📦 Creating bucket: $bucket_name"
284284
285- # Create minimal Terraform config for bucket creation
285+ # Copy the terraform-backend module to temp directory
286+ cp -r "$GITHUB_WORKSPACE/modules/terraform-backend" "$temp_dir/"
287+
288+ # Create Terraform config using the terraform-backend module
286289 cat > "$temp_dir/main.tf" << EOF
287290 terraform {
288291 required_providers {
@@ -294,23 +297,20 @@ jobs:
294297 }
295298
296299 provider "scaleway" {
297- region = "$region"
300+ region = "$region"
301+ zone = "$region-1"
302+ organization_id = var.organization_id
303+ project_id = var.project_id
298304 }
299305
300- resource "scaleway_object_bucket" "terraform_state" {
301- name = "$bucket_name"
302- region = "$region"
306+ module "terraform_backend" {
307+ source = "./terraform-backend"
303308
304- versioning {
305- enabled = true
306- }
307-
308- cors_rule {
309- allowed_headers = ["*"]
310- allowed_methods = ["GET", "PUT", "POST", "DELETE", "HEAD"]
311- allowed_origins = ["*"]
312- max_age_seconds = 3000
313- }
309+ bucket_name = "$bucket_name"
310+ environment = "$env"
311+ region = "$region"
312+ project_id = var.project_id
313+ generate_backend_config = false
314314
315315 tags = {
316316 Environment = "$env"
@@ -321,21 +321,61 @@ jobs:
321321 }
322322
323323 output "bucket_name" {
324- value = scaleway_object_bucket.terraform_state.name
324+ value = module.terraform_backend.bucket_name
325325 }
326326
327327 output "bucket_endpoint" {
328- value = scaleway_object_bucket.terraform_state.endpoint
328+ value = module.terraform_backend.bucket_endpoint
329+ }
330+
331+ output "s3_endpoint" {
332+ value = module.terraform_backend.s3_endpoint
333+ }
334+ EOF
335+
336+ # Create variables file with required variables
337+ cat > "$temp_dir/variables.tf" << EOF
338+ variable "organization_id" {
339+ description = "Scaleway organization ID"
340+ type = string
341+ }
342+
343+ variable "project_id" {
344+ description = "Scaleway project ID"
345+ type = string
329346 }
330347 EOF
331348
332349 cd "$temp_dir"
333350
334- # Initialize and apply
335- if terraform init && terraform apply -auto-approve > /dev/null 2>&1; then
351+ # Initialize and apply with proper error handling
352+ echo " Initializing Terraform..."
353+ if ! terraform init; then
354+ echo "❌ Failed to initialize Terraform for bucket: $bucket_name"
355+ echo "🔍 Error details:"
356+ cat terraform.log 2>/dev/null || echo "No detailed log available"
357+ rm -rf "$temp_dir"
358+ return 1
359+ fi
360+
361+ echo " Planning infrastructure..."
362+ if ! terraform plan -out=plan.tfplan \
363+ -var="organization_id=${SCW_DEFAULT_ORGANIZATION_ID}" \
364+ -var="project_id=${SCW_DEFAULT_PROJECT_ID}"; then
365+ echo "❌ Failed to plan infrastructure for bucket: $bucket_name"
366+ echo "🔍 Error details:"
367+ cat terraform.log 2>/dev/null || echo "No detailed log available"
368+ rm -rf "$temp_dir"
369+ return 1
370+ fi
371+
372+ echo " Applying infrastructure..."
373+ if terraform apply -auto-approve plan.tfplan; then
336374 echo "✅ Created bucket: $bucket_name"
337375 else
338376 echo "❌ Failed to create bucket: $bucket_name"
377+ echo "🔍 Error details:"
378+ cat terraform.log 2>/dev/null || echo "No detailed log available"
339379 rm -rf "$temp_dir"
340380 return 1
341381 fi
0 commit comments