Abisola Adesegun
Posted on October 10, 2024
Table of Contents
Step 1: Set up terraform and VSCode
Step 2. Create files
Step 3: Initialize Terraform
Step 4: Plan and Apply Configuration
Step 5: Connect to the VM
Step 6: Install an Apache server
Step 7: Deploy An Application
Prerequisites:
You need an active Azure subscription.
Azure CLI installed on your local machine.
Step 1: Set up terraform and VSCode
Install Terraform
Download and install Terraform from Terraform's official website if it's not already installed on your system.Create a directory in which to test the Terraform code and make it the current directory.
Set up Azure Authentication
You can authenticate Terraform to Azure using the Azure CLI or a service principal.
Step 2. Create files
- Login via Azure CLI: Run the following command to authenticate:
This will open a browser window to log into your Azure account.
- Create a file named main.tf and insert the scripts.:
- Create .ssh file and run the command to generate a ssh key
Step 3: Initialize terraform
Once the configuration files are in place, navigate to the directory where the files are located and run the following command to
- Firstly, validate to be sure your syntax is correct by using:
- Then initialize Terraform using
Step 4: Plan and Apply Configuration
- Plan: To see the changes Terraform will make:
- Apply: Deploy the infrastructure:
Terraform will prompt you to confirm the changes. Type yes to proceed with the deployment. This will create the Azure resources, including the resource group, virtual network, subnet, security group, and Linux VM.
- Accessing the VM
Once Terraform has applied the changes, you can view the resources created on your Azure portal.
- Obtain the public IP of the VM from the terminal by running:
Step 5: Connect to the VM
Once you have the public IP address of the VM and the correct private key, you can connect to the VM using the SSH command.
Step 6: Install an Apache server
- Update your VM running:
- Install an apache server
- Start and enable apache by running the two commands below one after the other
- Access your Public IP address, this shows apache2 Ubuntu Default Page
Step 7: Deploy An Application
- Pull Code from github but first check if git is installed running:
- Then enter into the location where you want to deploy your webapp
- Clone app to the path
- Enter into the website
- Move everything to www/html
- Set permission to view images in the folder.
then run the second permission:
- Restart the webserver
- Refresh your browser and see what we have:
This setup provides a simple foundation for deploying a Linux VM in Azure using Terraform.
Posted on October 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.