This repository contains a Terraform configuration for deploying an Ubuntu virtual machine on Azure with an existing virtual network and a GitHub Actions workflow to automate the deployment process.
- An Azure account with an active subscription.
- A configured Azure CLI or Azure PowerShell on your local machine.
- Terraform installed on your local machine.
- A GitHub account and an existing GitHub repository.
The repository is organized as follows:
├─ .github │ └─ Workflows │ └─ terraform_deploy.yml ├─ src │ ├─ main.tf │ └─ variables.tf ├─ temp ├─ util │ ├─ CreateGitHubSecrets.ps1 │ └─ tree.ps1 ├─ .gitignore ├─ DirectoryStructure.txt └─ README.md
src/main.tf
: Contains the Terraform configuration to deploy an Ubuntu VM on Azure with an existing virtual network.src/variables.tf
: Defines the input variables used in the Terraform configuration.src/outputs.tf
: Defines the output values generated by the Terraform configuration.src/terraform.tfvars
: Contains values for the input variables. you should create this file..github/workflows/terraform.yml
: The GitHub Actions workflow to automate the deployment process.util/CreateGitHubSecrets.ps1
: The PowerShell script creates an Azure Service Principal with a "Contributor" role on a specified resource group, and stores its credentials as secrets in a GitHub repository for use in GitHub Actions.util/tree.ps1
: script to create a directory structure
-
Clone the repository:
git clone https://github.com/emrgcl/DeployVMWithTerraform.git
-
Create the
src/terraform.tfvars
file and Replace the placeholder values in thesrc/terraform.tfvars
file with your desired values. Make sure to use a strong and unique password for theadmin_password
variable. Also, update the existing virtual network name, resource group name, and subnet name to match your existing resources. Below is a sample file for your reference.resource_group_name = "example-rg" location = "East US" vm_size = "Standard_B1s" admin_username = "adminuser" admin_password = "P@ssw0rd1234!" virtual_network_name = "existing-vnet" virtual_network_resource_group_name = "existing-vnet-rg" subnet_name = "existing-subnet"
-
Configure your Azure CLI or Azure PowerShell by running
az login
orConnect-AzAccount
, respectively. -
run the powershell script in the util folder to prepare azure and set github secrets.
.\CreateGitHubSecrets.ps1 -tenantId "<TENANT_ID>" -subscriptionId "<SUBSCRIPTION_ID>" -resourceGroupName "<RESOURCE_GROUP_NAME>" -githubToken "<GITHUB_TOKEN>" -githubOwner "<GITHUB_OWNER>" -githubRepo "<GITHUB_REPO>"
-
Initialize Terraform:
terraform init
-
Apply the Terraform configuration:
terraform apply
-
Add a GitHub Personal Access Token with the
repo
scope as a secret namedGH_TOKEN
in your GitHub repository settings. -
Update the
<your-github-username>
and<your-repo-name>
placeholders in the.github/workflows/terraform.yml
file with your GitHub username and repository name. -
Create a new environment in your GitHub repository with the same name specified in the
terraform.yml
file (e.g., "production"). Add required reviewers for approving deployments in the environment settings. -
Push your changes to the
main
branch:git add . git commit -m "Initial setup" git push origin main
-
The GitHub Actions workflow will run automatically when changes are pushed to the
main
branch.
This project is licensed under the MIT License. See the LICENSE file for details.