DEV Community

Cover image for Create And Deploy A Linux Virtual Machine On Azure Using Terraform
Abisola Adesegun
Abisola Adesegun

Posted on • Updated on

Create And Deploy A Linux Virtual Machine On Azure Using Terraform

Table of Contents
Step 1: Set up terraform and Visual Studio Code
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 Visual Studio Code

  1. Install Terraform
    Download and install Terraform from Terraform's official website if it's not already installed on your system.

  2. Create a directory in which to test the Terraform code and make it the current directory.

  3. 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:

Image description

This will open a browser window to log into your Azure account.

  • Create a file named main.tf and insert the following code:

Image description

  • Create .ssh file and run the command to generate a ssh key

Image description

Image description

Image description

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:

Image description

Image description

  • Then initialize Terraform using

Image description

Image description

Step 4: Plan and Apply Configuration

  • Plan: To see the changes Terraform will make:

Image description

Image description

  • Apply: Deploy the infrastructure:

Image description

Image description

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.

Image description

Image description

  • Obtain the public IP of the VM from the terminal by running:

Image description

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.

Image description

Image description

Step 6: Install an Apache server

  • Update your VM running:

Image description

Image description

  • Install an apache server

Image description

Image description

  • Start and enable apache by running the two commands below one after the other

Image description

Image description

  • Access your Public IP address, this shows apache2 Ubuntu Default Page

Image description

Step 7: Deploy An Application

  • Pull Code from github but first check if git is installed running:

Image description

Image description

  • Then enter into the location where you want to deploy your webapp

Image description

Image description

  • Clone app to the path

Image description

Image description

  • Enter into the website

Image description

Image description

  • Move everything to www/html

Image description

  • Set permission to view images in the folder.

Image description

Image description

then run the second permission:

Image description

Image description

  • Restart the webserver

Image description

  • Refresh your browser and see what we have:

Image description

This setup provides a simple foundation for deploying a Linux VM in Azure using Terraform.

Top comments (0)