├── .gitignore ├── README.md ├── backend.tf ├── main.tf ├── provider.tf └── variables.tf /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform/ 2 | .terraform.lock.hcl 3 | terraform.tfstate* 4 | *.tfvars 5 | secrets.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Terraform-AzureDevOps-Sample 2 | 3 | ### You can use the below Azure cli commands to set the terraform remote backend, or you can do it via the portal 4 | 5 | ``` shell 6 | #!/bin/bash 7 | ## The Storage account name must be unique, and the values below should match your backend.tf 8 | RESOURCE_GROUP_NAME=demo-resources 9 | STORAGE_ACCOUNT_NAME=techtutorialswithpiyush 10 | CONTAINER_NAME=prod-tfstate 11 | 12 | # Create resource group 13 | az group create --name $RESOURCE_GROUP_NAME --location canadacentral 14 | 15 | # Create storage account 16 | az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob 17 | 18 | # Create blob container 19 | az storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME 20 | ``` 21 | 22 | ## Usage/Examples 23 | 24 | ### 1) login to the CLI 25 | ```shell 26 | az login --use-device-code 27 | ``` 28 | ### 2) set alias 29 | ```shell 30 | alias tf=terraform 31 | ``` 32 | ### 3) initialize the providers 33 | ```shell 34 | tf init 35 | ``` 36 | ### 4) Run the plan 37 | ```shell 38 | tf plan 39 | ``` 40 | ### 5) Apply the changes 41 | ```shell 42 | tf apply --auto-approve 43 | ``` 44 | -------------------------------------------------------------------------------- /backend.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | backend "azurerm" { 3 | resource_group_name = "demo-resources" 4 | storage_account_name = "techtutorialswithpiyush" 5 | container_name = "prod-tfstate" 6 | key = "prod.terraform.tfstate" 7 | } 8 | } -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | resource "azurerm_resource_group" "example" { 2 | name = "${var.prefix}-rg" 3 | location = var.location 4 | } 5 | 6 | resource "azurerm_virtual_network" "main" { 7 | name = "${var.prefix}-network" 8 | address_space = ["10.0.0.0/16"] 9 | location = azurerm_resource_group.example.location 10 | resource_group_name = azurerm_resource_group.example.name 11 | } 12 | 13 | resource "azurerm_subnet" "internal" { 14 | name = "internal" 15 | resource_group_name = azurerm_resource_group.example.name 16 | virtual_network_name = azurerm_virtual_network.main.name 17 | address_prefixes = ["10.0.2.0/24"] 18 | } 19 | 20 | resource "azurerm_network_interface" "main" { 21 | name = "${var.prefix}-nic" 22 | location = azurerm_resource_group.example.location 23 | resource_group_name = azurerm_resource_group.example.name 24 | 25 | ip_configuration { 26 | name = "testconfiguration1" 27 | subnet_id = azurerm_subnet.internal.id 28 | private_ip_address_allocation = "Dynamic" 29 | } 30 | } 31 | 32 | resource "azurerm_virtual_machine" "main" { 33 | name = "${var.prefix}-vm" 34 | location = azurerm_resource_group.example.location 35 | resource_group_name = azurerm_resource_group.example.name 36 | network_interface_ids = [azurerm_network_interface.main.id] 37 | vm_size = "Standard_DS1_v2" 38 | 39 | # Uncomment this line to delete the OS disk automatically when deleting the VM 40 | # delete_os_disk_on_termination = true 41 | 42 | # Uncomment this line to delete the data disks automatically when deleting the VM 43 | # delete_data_disks_on_termination = true 44 | 45 | storage_image_reference { 46 | publisher = "Canonical" 47 | offer = "0001-com-ubuntu-server-jammy" 48 | sku = "22_04-lts" 49 | version = "latest" 50 | } 51 | storage_os_disk { 52 | name = "myosdisk1" 53 | caching = "ReadWrite" 54 | create_option = "FromImage" 55 | managed_disk_type = "Standard_LRS" 56 | } 57 | os_profile { 58 | computer_name = "hostname" 59 | admin_username = "testadmin" 60 | admin_password = "Password1234!" 61 | } 62 | os_profile_linux_config { 63 | disable_password_authentication = false 64 | } 65 | tags = { 66 | environment = "staging" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /provider.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=3.0.0" 6 | } 7 | } 8 | } 9 | 10 | # Configure the Microsoft Azure Provider 11 | provider "azurerm" { 12 | features {} 13 | } -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "prefix" { 2 | 3 | } 4 | 5 | variable "location" { 6 | 7 | } --------------------------------------------------------------------------------