Professional Documents
Culture Documents
DevOps - Cloud Infrastructures With Terraform
DevOps - Cloud Infrastructures With Terraform
CLOUD INFRASTRUCTURES
WITH TERRAFORM
Yassine BOUFENNECHE
Enseignant en Informatique
yassine.boufenneche@esme.fr
CLOUD INFRASTRUCTURES WITH TERRAFORM
In this course
❑ What is Terraform ?
❑ Install Terraform
❑ Configuring Terraform for Azure
❑ Provisioning Azure cloud infrastructure with Terraform
2
CLOUD INFRASTRUCTURES WITH TERRAFORM
3
CLOUD INFRASTRUCTURES WITH TERRAFORM
What is Terraform ?
4
CLOUD INFRASTRUCTURES WITH TERRAFORM
❑ Download Terraform
➢ https://releases.hashicorp.com/terraform/1.3.9/terraform_1.3.9_linux_amd64.zip
❑ Unzip the archive
➢ unzip terraform_1.3.9_linux_amd64.zip
❑ Move the resulting folder to /usr/local/bin
➢ mv terraform /usr/local/bin
❑ Check the installation
➢ terraform version
6
CLOUD INFRASTRUCTURES WITH TERRAFORM
➢ We need to create a new Azure Service Principal (SP) in Azure Active Directory (AD),
▪ An application user who has permission to manage Azure resources.
➢ For this Azure SP, we have to assign to it the contributing permissions on the
subscription in which we will create resources
7
CLOUD INFRASTRUCTURES WITH TERRAFORM
8
CLOUD INFRASTRUCTURES WITH TERRAFORM
Configuring Terraform for Azure
➢ Creating the Azure SP (2 / 3)
❑ Exemple:
9
CLOUD INFRASTRUCTURES WITH TERRAFORM
10
CLOUD INFRASTRUCTURES WITH TERRAFORM
There are three main parts in Terraform project:
➢ main.tf
▪ File that has all the code to build up infrastructure (simple project);
▪ For bigger projects: use several files based on the functionalities.
➢ variables.tf
▪ Stores the declarations for the variables referenced in main.tf.
➢ terraform.tfvars
▪ Define the default values of the variables.
11
CLOUD INFRASTRUCTURES WITH TERRAFORM
Configuring the Terraform provider 1/2
To set Terraform configuration to connect to Azure using our SP:
1. Create a provider.tf which contains the following code:
12
CLOUD INFRASTRUCTURES WITH TERRAFORM
Configuring the Terraform provider 2/2
Note: instead of putting azure credentials in files variables.tf and terraform.tfvars, we can put them
into specific Terraform environment variables ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID,
ARM_CLIENT_SECRET and ARM_TENANT_ID as follows:
Example on Linux:
export ARM_CLIENT_ID="00000000-0000-0000-0000-000000000000"
export ARM_CLIENT_SECRET="12345678-0000-0000-0000-000000000000"
export ARM_TENANT_ID="10000000-0000-0000-0000-000000000000"
export ARM_SUBSCRIPTION_ID="20000000-0000-0000-0000-000000000000"
Example on PowerShell:
$env:ARM_CLIENT_ID = "00000000-0000-0000-0000-000000000000"
$env:ARM_CLIENT_SECRET = "12345678-0000-0000-0000-000000000000"
$env:ARM_TENANT_ID = "10000000-0000-0000-0000-000000000000"
$env:ARM_SUBSCRIPTION_ID = "20000000-0000-0000-0000-000000000000"
13
CLOUD INFRASTRUCTURES WITH TERRAFORM
14
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
To do this, we will create a rg.tf, network.tf and compute.tf files (in the
same folder that contains provider.tf) with the code of the followings:
https://developer.hashicorp.com/terraform/language/v1.1.x/configuration-0-11/resources
15
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
This code uses the azurerm_resource_group Terraform resource and will provision a resource
group, named devopsRg, that will be stored in the West Europe location.
16
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
2. The code for the network part: [ file network.tf]
➢ https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network
17
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
3. Writing the code of the virtual machine, which is composed of the following:
a. A network interface b. A public IP address
c. An Azure Storage object for the diagnostic boot (boot information logs) d. A virtual machine
3.a Code for the network interface: [ file compute.tf]
➢ We use an azurerm_network_interface
block. For it:
▪ We configure the the name, region, resource
group, and IP configuration with the
dynamic IP address of the network interface.
https://www.terraform.io/docs/providers/azurerm/r/network_interface
18
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
3. Writing the code of the virtual machine, which is composed of the following:
➢ https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip
19
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
3. Writing the code of the virtual machine, which is composed of the following:
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_blob
20
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
3. Writing the code of the virtual machine, which is composed of the following:
3.c code for the Ubuntu virtual machine : [ file compute.tf]
▪ contains the ID of the network interface created earlier
https://registry.terraform.io/providers/hashic
orp/azurerm/latest/docs/resources/virtual_m
achine
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine 22
CLOUD INFRASTRUCTURES WITH TERRAFORM
23
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
❑ Before execution, first, we need to provide authentication (if it’s not done yet) with the Azure SP
to ensure that Terraform can manage the Azure resources.
➢ We can either set the environment variables specific to Terraform to contain the information of the SP
created earlier in the Configuring Terraform for Azure section
➢ Or we can use an az cli script.
❑ The following script exports the four Terraform environment variables in the Linux OS:
24
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
❑ Initialization
➢ terraform init
25
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
26
CLOUD INFRASTRUCTURES WITH TERRAFORM
Terraform scripts to deploy an Azure infrastructure
27
CLOUD INFRASTRUCTURES WITH TERRAFORM
Exercise
28