emrgcl / DeployVMWithTerraform

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DeployVMWithTerraform

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.

Prerequisites

  1. An Azure account with an active subscription.
  2. A configured Azure CLI or Azure PowerShell on your local machine.
  3. Terraform installed on your local machine.
  4. A GitHub account and an existing GitHub repository.

Repository structure

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

Files

  • 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

Setup

  1. Clone the repository: git clone https://github.com/emrgcl/DeployVMWithTerraform.git

  2. Create the src/terraform.tfvars file and Replace the placeholder values in the src/terraform.tfvars file with your desired values. Make sure to use a strong and unique password for the admin_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"
  3. Configure your Azure CLI or Azure PowerShell by running az login or Connect-AzAccount, respectively.

  4. 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>"
  5. Initialize Terraform: terraform init

  6. Apply the Terraform configuration: terraform apply

  7. Add a GitHub Personal Access Token with the repo scope as a secret named GH_TOKEN in your GitHub repository settings.

  8. Update the <your-github-username> and <your-repo-name> placeholders in the .github/workflows/terraform.yml file with your GitHub username and repository name.

  9. 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.

  10. Push your changes to the main branch:

    git add .
    git commit -m "Initial setup"
    git push origin main
    
  11. The GitHub Actions workflow will run automatically when changes are pushed to the main branch.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About


Languages

Language:PowerShell 74.0%Language:HCL 26.0%