PROJECT: IaC (Infrastructure as a Code) with terraform for deploying azure resource group
Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. This includes both low-level components like compute instances, storage, and networking, as well as high-level components like DNS entries and SaaS features.
Magalu Cloud Virtual Machines operate trought a virtualization infrastructure that allows multiples Operational Systems and Applications to run on a single server. Each VM is isolated and indepentent, allowing users to have full control over their computing environment.
An effective naming convention consists of resource names from important information about each resource. A good name helps you quickly identify the resource's type, associated workload, environment, and the region hosting it.
In our environment we adopt the following convention:
Business Cost Center (any characters) | Environment (3 characters and 1 number) | Azure Region (4 characters) | Resource Type (5 characters max) | Instance (3 characters) |
---|
Environments possibles:
Name | Acronym | Description |
---|---|---|
Production | pro1 | Production Environment |
Staging | sta1 | Homologation Environment |
Development | dev1 | Development Environment |
Shared | sha1 | Shared Environment |
hub | hub1 | Transit Environment to network resources |
Spoke | spk1 | Hub Environment to traffic requests to on-premisses |
Magalu Cloud Region (5 characters) according this table:
ACRONYM | REGION |
---|---|
brse1 |
br-se1 |
brne1 |
br-ne1 |
For example, a virtual machine for a business costcenter called cliente01 for a production workload in the Brasil Sudeste Region might be cliente01-pro1-brse1-prj-001.
cliente01-pro1-brse1-prj-001
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
terraform version
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
terraform version
Invoke-WebRequest -Uri https://releases.hashicorp.com/terraform/1.1.9/terraform_1.1.9_windows_amd64.zip -OutFile terraform.zip
Expand-Archive .\terraform.zip -DestinationPath C:\Windows\System32\ -Force
terraform version
We are using hashicorp's SAAS to host the service states. By default, Terraform will obtain an API token and save it in plain text in a local CLI configuration file called credentials.tfrc.json. When you run terraform login, it will explain specifically where it intends to save the API token and give you a chance to cancel if the current configuration is not as desired.
You can get more details about these features from the following links:
You can find the API Token that has already been generated in the environment in our keepass and configure your CLI as follows:
In Windows:
@"
{
"credentials": {
"app.terraform.io": {
"token": "SEE IN THE KEEPASS OR CONSULTE OURS ADMINS"
}
}
}
"@ | Set-Content ~\AppData\Roaming\terraform.d\credentials.tfrc.json
In Linux:
cat <<EOF | tee ~/.terraform.d/credentials.tfrc.json
{
"credentials": {
"app.terraform.io": {
"token": "SEE IN THE KEEPASS OR CONSULTE OURS ADMINS"
}
}
}
EOF
If workspace in Hashicorp's environment is configured to operate locally, you will need to authenticate to the API of the Magalu Cloud using an API KEY.
You can more information how to generate this API KEY in Create API Key
In our environment we use the credentials as environment variables to autenticate in API of the Magalu Cloud, for example:
Linux:
export MGC_API_KEY="00000000-0000-0000-0000-000000000000"
Windows:
$env:MGC_API_KEY="00000000-0000-0000-0000-000000000000"
To persist environment variables at user level
[System.Environment]::SetEnvironmentVariable("MGC_API_KEY","00000000-0000-0000-0000-000000000000","User")
To persist environment variables at machine level
[System.Environment]::SetEnvironmentVariable("MGC_API_KEY","00000000-0000-0000-0000-000000000000","Machine")
ATTENTION: On Linux operating systems it is not possible to persist environment variables
By declaring these environment variables, terraform will be able to authenticate through this SPN
Name | Version |
---|---|
mgc | 0.32.2 |
Name | Version |
---|---|
mgc | 0.32.2 |
No modules.
Name | Type |
---|---|
mgc_block_storage_volume_attachment.attach | resource |
mgc_block_storage_volumes.storage | resource |
mgc_network_public_ips.pip | resource |
mgc_network_public_ips_attach.pip | resource |
mgc_network_security_groups.secgroup | resource |
mgc_network_security_groups_attach.attach | resource |
mgc_network_security_groups_rules.rules | resource |
mgc_network_vpcs_interfaces.nic | resource |
mgc_ssh_keys.key | resource |
mgc_virtual_machine_instances.vm | resource |
mgc_virtual_machine_interface_attach.attach_vm | resource |
mgc_availability_zones.availability_zones | data source |
Name | Description | Type | Default | Required |
---|---|---|---|---|
availability_zone | [OPTIONAL] The availability zone of the virtual machine instance. | string |
null |
no |
image | [REQUIRED] The image name used for the virtual machine instance. | string |
"cloud-ubuntu-24.04 LTS" |
no |
machine_type | [REQUIRED] The machine type used for the virtual machine instance. | string |
"BV1-1-10" |
no |
nic | [REQUIRED] Network Interface Card (NIC) to be used on resource. | map( |
{ |
no |
project_name | [REQUIRED] Name of an existing Project Name | string |
n/a | yes |
sequence | [REQUIRED] Sequence to be used on resource naming. | number |
1 |
no |
ssh_key | [REQUIRED] SSH Key to be used on resource. | string |
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDaWZr3Ui3MJ57goKM7lWYPmjqYQCOgo2EUYW/a1Zqeo/EzaNhSOeP5r268bKKH6vkMdiTSksOpfr/XGZyf5idGQTUkgFXT0Kt79vhk5eZ/ce4BF4WoFZgr1XJCcsrtuQm3ybzkDV+qT7486LSbKHIr2e9j+t9M7EAbki+wMRV0p9I/3ACZ99YZw2kXODetfYuJxeBDplPgleoDo7aYXmoIuv8uZf/jcIUuDJ6/Pb7jvOHwtStuOCM14x5P1CAXHnLuW1KXdXHXPpSzUzhP57G8TGhzVghjnSHD9YCmc43v81BIN/HRdSr7dJ013uHrHISOWwq4z815YNZEmrVDoyl6zSnqn3DXN6zicfHJjz16s3z4knlXzuj9lXxesvYhWzsfuXN/y152XP+qFw4MfYVyllzBtzsotyFvNB9w2SxUCWKjFjLb1ckrRXEk5QwmlerkGFCWvUY2Qxpiw2f/VY4BgWZ4eT4bGoC5qlc7aTlkRiVJ8YHcRpmowKZl1dmViME= m@cloud" |
no |
storage | [OPTIONAL] One or more storage_data_disk blocks. | map(object({ |
{} |
no |
vpc_id | [REQUIRED] VPC ID to be used on resource. | string |
n/a | yes |
Name | Description |
---|---|
resources | n/a |
Some of this documentation was generated through terraform-docs using the following command:
docker run --rm --volume "$(pwd):/terraform-docs" quay.io/terraform-docs/terraform-docs:0.16.0 markdown /terraform-docs