Skip to main content

On This Page

IaC with Terraform on Magalu Cloud: From Zero to Reproducible Environments

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

IaC com Terraform na Magalu Cloud: do zero a um ambiente reproduzível

Magalu Cloud allows infrastructure management through the official magalucloud/mgc provider, enabling the transition from manual console tasks to declarative code. This system ensures that development and production environments remain identical through versionable configuration files.

Why This Matters

Manual infrastructure management creates a technical gap where environments drift apart, making debugging and scaling nearly impossible. By implementing IaC, engineers move from ‘click-ops’ to a predictable model where the ‘terraform plan’ command acts as a safety net, revealing potential impacts before they occur in production.

Key Insights

  • The official provider ‘magalucloud/mgc’ allows full lifecycle management of Compute, Block Storage, and Object Storage resources.
  • Authentication is secured using environment variables such as MGC_API_KEY and MGC_REGION to prevent credential leakage in version control.
  • Resource alignment is critical; Block Storage volumes must reside in the same Availability Zone as the VM instance to enable successful attachment.
  • Magalu Cloud Object Storage serves as a remote backend for Terraform state, leveraging S3 compatibility to prevent state conflicts during team collaboration.
  • The standard IaC workflow on Magalu Cloud involves a three-step process: init (provider setup), plan (change verification), and apply (resource execution).

Working Examples

Configuration of the Magalu Cloud provider using variables for authentication.

terraform {
  required_providers {
    mgc = {
      source = "magalucloud/mgc"
    }
  }
}

provider "mgc" {
  region  = var.mgc_region
  api_key = var.mgc_api_key
}

Provisioning a virtual machine instance with a public IP address.

resource "mgc_virtual_machine_instances" "app" {
  name              = "app-1"
  availability_zone = var.az
  image_id          = var.image_id
  machine_type_id   = var.machine_type_id
  ssh_key_name      = var.ssh_key_name
  network {
    associate_public_ip = true
  }
}

Creating and attaching a persistent Block Storage volume to a VM instance.

resource "mgc_block_storage_volume" "data" {
  name = "volume-via-terraform"
  size = 30
  type = {
    name = "cloud_nvme20k"
  }
}

resource "mgc_block_storage_attachment" "data_attach" {
  volume_id   = mgc_block_storage_volume.data.id
  instance_id = mgc_virtual_machine_instances.app.id
}

Practical Applications

  • System: Multi-environment deployment using Git-versioned files to ensure dev, staging, and production are identical. Pitfall: Hardcoding sensitive credentials in the main.tf file instead of utilizing environment variables.
  • System: Database persistence management by attaching Block Storage to Compute nodes. Pitfall: Attempting to attach storage volumes to instances located in different Availability Zones, resulting in provisioning failure.

References:

Continue reading

Next article

Hive Index: Real-Time Rust-Based Explorer for Blockchain Custom JSON Operations

Related Content