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.
VPC is a virtual network isolated behind of the Magalu Cloud infrastructure. This resource allow you execute yours resources with more security and privacy, with full control over the network 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_network_subnetpools.subnetpool | resource |
mgc_network_vpcs.vpc | resource |
mgc_network_vpcs_subnets.subnet | resource |
mgc_availability_zones.availability_zones | data source |
Name | Description | Type | Default | Required |
---|---|---|---|---|
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 |
subnet_pools | [REQUIRED] The address space that is used the virtual network. You can supply more than one address space. CAUTION: Changing the existing address space recalculates all subnets. This action can harm the environment. | map( |
{ |
no |
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