Step-by-Step Guide to Creating Azure Resources with Terraform and GitHub
Welcome To Terraform!
Introduction
Terraform is a powerful and versatile infrastructure-as-code (IaC) software tool created by HashiCorp. It enables users to define and manage their IT infrastructure using simple, human-readable configuration files. By employing the declarative HashiCorp Configuration Language (HCL), Terraform allows developers to specify the desired state of their infrastructure. This open-source tool is designed to help build, change, and version infrastructure efficiently.
Terraform leverages application programming interfaces (APIs) to create and manage resources across various cloud platforms and services. Its ability to streamline and automate the deployment process makes it an essential tool for modern DevOps practices, facilitating the rapid provisioning and scaling of infrastructure while maintaining consistency and reducing manual intervention.
Terraform and Infrastructure as Code (IaC)
Here’s why IaC and Terraform are essential:
No More Clicks: With Terraform, you write down what you want to deploy, such as virtual machines (VMs), disks, and programs, in a human-readable document. This eliminates the need for manual clicks in a web interface, making deployment processes more efficient and repeatable.
Facilitates DevOps: By coding your deployments, you can track changes in version control systems like Git. This improves visibility, collaboration, and teamwork among development and operations teams.
Declarative Infrastructure: Terraform allows you to declare the desired state of your infrastructure. This declarative approach, compared to a procedural (imperative) approach, simplifies the management and understanding of infrastructure configurations.
Speed, Cost, and Reduced Risk: Automating deployments with Terraform reduces human intervention, saving time and minimizing the risk of errors, security vulnerabilities, and unnecessary resource usage.
Why Is Terraform Loved? - Easily interacts and manages communication with control layer APIs - Facilitates a wide range of private and public cloud providers - Monitors the status of every resource that is installed - Automates Software Defined Networking!
Terraform Workflow
Basic Terraform Commands
terraform init
- Initializes the working directory that contains your Terraform code. - Sets up backend: Installs modules and plugins - Download ancillary components: Configures the backend to store the Terraform statefile, which is Terraform's resource tracking method.terraform plan
- Reads the code, makes a "plan" for execution and deployment, and displays it. Note: Nothing is deployed by this command. Think of this command as read-only. - Gives the user the option to "review" the action plan before carrying it out. - If necessary, the stage uses authentication credentials to establish a connection to your infrastructure.terraform apply
- Makes use of the code's declarations and instructions - Modifies the "state file" or deployment's state tracking method file.terraform destroy
- Examines the state file that was recorded and saved during deployment and deletes all of the resources that your code generated. - Since it is an irreversible instruction, it should be utilized carefully. Make sure you want to destroy infrastructure before deleting any backups.
Prerequisites
Before we begin, ensure you have the following prerequisites:
Azure Subscription: An active Azure subscription.
Terraform: Installed on your local machine. You can download it from Terraform's official site.
Azure CLI: Installed and configured on your local machine. Follow the installation guide on Azure's official documentation.
Visual Studio Code (VSC): Installed on your local machine.
GitHub Account: A GitHub account to store your Terraform configuration.
Follow Guide To Install Terraform
- Install Terraform On Windows
A. Go to the official Terraform Website as seen in the Image - "Select" "Windows" Operating System to install Terraform
B. "Select" "Windows" Operating Systems and "Click On" "AMD64 version"
C. Open "Downloads" as highlighted in red and open File when its finished downloading
Copy The Path Of Downloaded File From Your Local System
D. Click On the Folder where the downloaded terraform file is, you will be prompted to the location of the file on your PC
E. Extract Zip files - Right click on file and select "Extract to "terraform_1.8.2_windows"
F. Copy the file path
G. On your PC search for "Edit the system environment" and "Click" "Open"
H. It will prompt you here "Select" "Advanced" and "Click On" "Environment Variables"
I. Under System variables "Click On" "Path" and "Edit"
J. "Click On" "New" and paste the file path you copied as seen in image in step "F"
K. Validate if you have successfully installed terraform, Run command terraform --version
to see the version of the terraform installed
Follow Guide To Install Terraform
- Install Terraform On Linux A. Go to the official Terraform Website as seen in the Image - "Select" "Linux" Operating System to install Terraform - "Select" "Ubuntu/Debian" package manager and copy the command
B. Open Ubuntu terminal and Run command sudo su
to enter user root - Paste the command copied from the terraform website and "Press" "Enter"
Successfully Installed
C. Validate if you have successfully installed terraform, Run command terraform --version
to see the version of the terraform installed
D. Run command terraform
to see a list of terraform commands
Follow Guide To Install Terraform
Install Terraform On Visual Studio Code(VSC) - On your PC, search for "Visual Studio Code" (VSC) and open it. - In Visual Studio Code, click on the Extensions icon located in the sidebar ( on the left side of the screen). - In the Extensions marketplace, type "HashiCorp Terraform" into the search bar.
- Find the "HashiCorp Terraform" extension in the search results and click on it.
- Click on the "Install" button to add the extension to your Visual Studio Code environment.
Step 1: Create a Repository In GitHub - Log in to your GitHub account. - On the GitHub dashboard, click on the "New" button to create a new repository.
Step 1a: Give your repository a name. - Add a description for your repository. -Tick the "Public" option to make your repository publicly accessible.
- Tick the option to "Add a README file." - Click on the "Create repository" button to finalize and create your new repository.
Step 2: Clone Repository From Visual Studio Code - "Click On" "Explorer" and "Click" "Clone Repository"
i. You will be prompted to pick repository source, "Click On" "Clone from GitHub"
ii. "Click On" "Allow"
iii. Select GitHub Account
iv. Click On" "Authorize Visual-Studio-Code"
v. Input password and confirm to authenticate user
vi. "Click on" "Open Visual Studio Code"
vii. "Select" "Repository"
Step 2: Create a Folder - "Click On" "File" and "Select" "Open Folder"
Step 2a: Click on "New folder" and name the folder "terraform git clone" or any name - Select this newly created folder. - Click on "Select as repository destination" to set it as the destination for your repository.
i. Prompt below will pop-up "Click On" "Open"
ii. Verify Authors of File - "Tick" "Trust the authors of all files in the parent folder" - "Click On" "Yes, I trust the authors"
Step 3: Create File - "Click On" the Icon(red arrow points downwards) to create new file
Step 4: - Go to Terraform official site to select a provider https://registry.terraform.io/browse/providers - The URL in the browser is highlighted, showing the link to the latest version of the AzureRM provider: https://registry.terraform.io/providers/hashicorp/azurerm/latest
. - "Click" "USE PROVIDER" button , which allows users to copy the code
Step 5: Open the "main.tf file" and paste the provider code to define the Azure Resource Manager (azurerm) provider with a specified version (3.104.2) from the HashiCorp source
Step 6: Create Resource Group - The first resource group named eunicerg
is for terraform - Create a2nd resource group named nicelady-rg
in the UK South
region for Azure
Step 6a: Create a Virtual Network - Creates a virtual network named testing-vnet
with an address space of10.0.0.0/16
. - It is located in the same region (UK South
) and resource group (nicelady-rg
) as defined by the eunicerg
resource group.
Step 6b: Create a Subnet - Creates a subnet named testing-sub
within the testing-vnet
virtual network. - It has an address prefix of 10.0.2.0/24
.
Step 7: Commit & Push Your Configuration to GitHub - Navigate to the "Source Control" on the left pane and enter the commit message as "Initial commit for Terraform" before proceeding to click on the "Commit" button.
i. "Click On" "Save All& Commit Changes"
ii. "Click On" "Sync Changes" and "Click" "OK"
Step 8: Open GitHub, navigate to the repository, and view the main.tf file and commit message
i. View the contents of main.tf file
Step 9: Log into Azure - Open Terminal in VSC and run command az login or az login --tenant <tenant Id>
to log into Azure
i. "Select Account"
ii. Successfully Logged into Azure
Step 10: Initialize Terraform - In the terminal, initialize Terraform within your project directory - Run Command terraform init
downloads the Azure provider and sets up your working directory.
Step 10: Plan and Apply Your Terraform Configuration - Run command terraform plan
to generate and review the execution plan, If everything looks good, apply the configuration
Step 10a: Apply configuration
Type yes
when prompted to confirm the application
Step 11: View Resources In Azure - Navigate to "Resource Groups" and select "nicelady-rg" Once there, click on the "Overview" tab to view your virtual network
Step 11a: While staying within the overview tab of the virtual network, scroll down the left pane and choose "Settings." From there, select "Subnets" to access and view your subnet .
Step 12: Change Subnet Configuration - To make a change to the subnet, update the address_prefixes
in main.tf
:
Step 13: Update the Subnet Configuration - Run terraform apply
to apply the updated configuration:
i. Confirm the update by typing yes
.
ii. Successfully made changes
Step 14: Destroy the Resources - If you want to clean up and remove the resources, use the destroy command
i. Confirm the destruction by typing yes
.
Conclusion
Congratulations! You have successfully created, modified, and destroyed Azure resources using Terraform. By following this guide, you have also learned how to manage your Terraform configurations using GitHub. This foundational knowledge will help you as you continue to explore the power of Infrastructure as Code with Terraform on Azure