Hands on exercises for Terraform beginners.
Objective: Learn how to define and create a basic Azure Resource Group using Terraform.
This exercise will guide you through the process of installing Terraform, configuring the Azure CLI, writing a Terraform configuration file to define an Azure Resource Group, initializing Terraform, planning the deployment, and applying the configuration to create the resource group in Azure.
Tasks:
- Install Terraform and configure Azure CLI. 12
- Write a Terraform configuration file to define an Azure Resource Group. 3
- Initialize Terraform and plan the deployment. 4
- Apply the configuration to create the resource group in Azure. 5
Objective: Expand the configuration to include an Azure Storage Account within the created Resource Group.
We will use the Storage Account to store Terraform state remotely in the next exercise. This exercise will guide you through the process of updating the Terraform configuration to include an Azure Storage Account and Container, planning and applying the changes, and verifying the creation of the Storage Account in the Azure portal.
Tasks:
- Update the Terraform configuration to define an Azure Storage Account.
- Add an Azure Storage Container to the Storage Account configuration.
- Use Terraform to plan and apply the changes.
- Verify the creation of the Storage Account in the Azure portal.
Objective: Learn how to use git to manage Terraform configurations and version control.
This exercise will guide you through the process of initializing a git repository in the directory where you have your Terraform configuration, adding a .gitignore
file to exclude the .terraform
directory and the terraform.tfstate
file, and committing the Terraform configuration to the git repository. Please also take a look at the references for more information on which files to ignore when using git with Terraform.
- Initialize a git repository in the directory where you have your Terraform configuration.
- Add a
.gitignore
file to exclude the.terraform
directory and theterraform.tfstate
file. 6 - Commit the Terraform configuration to the git repository.
- Add a
tags
block to the Resource Group and Storage Account configurations to apply metadata. - Use the terraform
show
command to inspect the current state of the Terraform-managed infrastructure. - Use the terraform
destroy
command to remove the resources created in the exercises and recreate them. - Create a remote git repository and push the Terraform configuration to it.
Objective: Learn how to setup an Azure Storage Account for remote state management and to migrate Terraform state from local files to remote state.
Tasks:
- Modify the Terraform configuration to use the Azure backend for state storage. 78 Concider renaming and recreating the resource group and storage account to have meaningful names before migrating the state.
- Initialize Terraform with the new backend configuration and migrate the state with the appropriate
terraform init
option. - Observe the new
terraform.tfstate
file in the Azure Storage Account.
- Create a new resource group in the Azure portal, add some tags to it and create a Storage Account in it.
- Create a terraform resource object that represents the existing resource group.
- Use the
terraform import
command to import the existing resource group into the Terraform state. 9 - Make sure you do not destroy, modify or recreate the existing resource group when running
terraform apply
.
- Use the
terraform taint
on the imported resource group to mark it for recreation. - Use the
terraform plan
command to see the changes that Terraform would apply to the imported resource group. - Use the
terraform apply
command to recreate the imported resource group.
Objective: Evolve an existing Terraform code into a module to use variables and outputs, making it more flexible and reusable.
Our plan is to refactor the existing Terraform configuration into a module, define input variables for resource names and other parameters, define outputs for important information like the Resource Group ID and Storage Account connection string, and use the module in the main Terraform configuration with variable values.
We are also planning to create different environments for our infrastructure, like dev
and stg
, and use the module in both environments with different configurations.
Our final directory structure might look something like this:
.
├── environments
│ ├── dev
│ │ ├── backend_override.tf
│ │ ├── provider_override.tf
│ │ └── terraform.tfvars
│ └── stg
│ │ ├── backend_override.tf
│ │ ├── provider_override.tf
│ │ └── terraform.tfvars
├── main.tf
├── variables.tf
├── outputs.tf
├── modules
│ ├── network
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ └── storage
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
└── terraform.tfvars
Tasks:
- Refactor the existing Terraform configuration into a module.
- Define input variables for resource names and other parameters, try to have a flexible but easy to use module with sane defaults.
- Define outputs for important information like the Resource Group ID and Storage Account connection string.
- Use the module in the main Terraform configuration with variable values.
- Create a new deployment of your module that does not collide with the existing resources holding your terraform state.
Objective: Use the Terraform Random provider to avoid naming collisions and learn how to import existing Azure resources into Terraform management.
Tasks:
- Integrate the Random provider to generate unique names for resources.
- Create a new resource with a unique name and apply the configuration.
- Identify an existing resource in Azure to be managed by Terraform.
- Use Terraform's import command to bring the existing resource into Terraform state.
- Modify the Terraform configuration to match the imported resource's settings.