├── .gitattributes ├── 12. Lab - Creating an Azure virtual network └── main.tf ├── 13. Lab - Creating an Azure virtual machine └── main.tf ├── 15. Lab - Create Public IP Address └── main.tf ├── 16.Lab - Adding data disks └── main.tf ├── 17. Lab - Availability Sets └── main.tf ├── 18. Lab - Custom Script extensions ├── IIS_Config.ps1 └── main.tf ├── 19. Lab - Network Security Groups └── main.tf ├── 20. Lab - Fetching from Azure Key Vault └── main.tf ├── 21. Lab - Creating a Linux VM └── main.tf ├── 22. Lab - Linux VM with SSH keys └── main.tf ├── 23. Lab - Cloud init data └── main.tf ├── 24. Lab - Azure Web App └── main.tf ├── 25. Lab - Azure Web App - Publishing from GitHub └── main.tf ├── 26. Lab - Azure SQL Database └── main.tf ├── 27. Lab - Azure Web App - Connecting to Database app ├── init.sql └── main.tf ├── 28. Lab - Azure Bastion └── main.tf ├── 29. Lab - Azure Load Balancer ├── IIS_Config.ps1 └── main.tf ├── 30. Lab - Azure Public DNS Zone └── main.tf ├── 31. Lab - Virtual Machine Scale Set ├── IIS_Config.ps1 └── main.tf ├── 32. Lab - Azure Traffic Manager └── main.tf ├── 33. Lab - Log Analytics workspace └── main.tf ├── 34. Lab - Azure Monitor alerts └── main.tf ├── 35. Lab - Budget alerts └── main.tf ├── 36. Lab - Azure Function App └── main.tf ├── 37. Lab - Role based access control └── main.tf ├── 38. Lab - Azure Application Gateway ├── IIS_Config_image.ps1 ├── IIS_Config_video.ps1 └── main.tf ├── 39. Breaking down the configuration files ├── main.tf ├── network.tf ├── variables.tf ├── versions.tf └── virtualmachines.tf ├── 40. Creating multiple resources ├── main.tf ├── network.tf ├── outputs.tf ├── variables.tf ├── versions.tf └── virtualmachines.tf ├── 5. Lab - Creating a resource group └── main.tf ├── 6. Lab - Creating a storage account └── main.tf ├── 7. Terraform state └── main.tf ├── 8. Lab - Creating a container and a blob └── main.tf └── 9. Lab - Dependencies between resources └── main.tf /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /12. Lab - Creating an Azure virtual network/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "ed5d6ac4-de15-4a24-b5d3-61812c9e9941" 13 | client_secret = "HOl7Q~23UVXp4J1SlBTsQQSR~dMk2CTsNERWH" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | 19 | locals { 20 | resource_group="app-grp" 21 | location="North Europe" 22 | } 23 | 24 | resource "azurerm_resource_group" "app_grp"{ 25 | name=local.resource_group 26 | location=local.location 27 | } 28 | 29 | resource "azurerm_virtual_network" "app_network" { 30 | name = "app-network" 31 | location = local.location 32 | resource_group_name = azurerm_resource_group.app_grp.name 33 | address_space = ["10.0.0.0/16"] 34 | 35 | subnet { 36 | name = "SubnetA" 37 | address_prefix = "10.0.1.0/24" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /13. Lab - Creating an Azure virtual machine/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "ed5d6ac4-de15-4a24-b5d3-61812c9e9941" 13 | client_secret = "HOl7Q~23UVXp4J1SlBTsQQSR~dMk2CTsNERWH" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | 19 | locals { 20 | resource_group="app-grp" 21 | location="North Europe" 22 | } 23 | 24 | data "azurerm_subnet" "SubnetA" { 25 | name = "SubnetA" 26 | virtual_network_name = "app-network" 27 | resource_group_name = local.resource_group 28 | } 29 | 30 | resource "azurerm_resource_group" "app_grp"{ 31 | name=local.resource_group 32 | location=local.location 33 | } 34 | 35 | resource "azurerm_virtual_network" "app_network" { 36 | name = "app-network" 37 | location = local.location 38 | resource_group_name = azurerm_resource_group.app_grp.name 39 | address_space = ["10.0.0.0/16"] 40 | 41 | subnet { 42 | name = "SubnetA" 43 | address_prefix = "10.0.1.0/24" 44 | } 45 | } 46 | 47 | resource "azurerm_network_interface" "app_interface" { 48 | name = "app-interface" 49 | location = local.location 50 | resource_group_name = local.resource_group 51 | 52 | ip_configuration { 53 | name = "internal" 54 | subnet_id = data.azurerm_subnet.SubnetA.id 55 | private_ip_address_allocation = "Dynamic" 56 | } 57 | 58 | depends_on = [ 59 | azurerm_virtual_network.app_network 60 | ] 61 | } 62 | 63 | resource "azurerm_windows_virtual_machine" "app_vm" { 64 | name = "appvm" 65 | resource_group_name = local.resource_group 66 | location = local.location 67 | size = "Standard_D2s_v3" 68 | admin_username = "demousr" 69 | admin_password = "Azure@123" 70 | network_interface_ids = [ 71 | azurerm_network_interface.app_interface.id, 72 | ] 73 | 74 | os_disk { 75 | caching = "ReadWrite" 76 | storage_account_type = "Standard_LRS" 77 | } 78 | 79 | source_image_reference { 80 | publisher = "MicrosoftWindowsServer" 81 | offer = "WindowsServer" 82 | sku = "2019-Datacenter" 83 | version = "latest" 84 | } 85 | 86 | depends_on = [ 87 | azurerm_network_interface.app_interface 88 | ] 89 | } 90 | 91 | -------------------------------------------------------------------------------- /15. Lab - Create Public IP Address/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "ed5d6ac4-de15-4a24-b5d3-61812c9e9941" 13 | client_secret = "HOl7Q~23UVXp4J1SlBTsQQSR~dMk2CTsNERWH" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | 19 | locals { 20 | resource_group="app-grp" 21 | location="North Europe" 22 | } 23 | 24 | data "azurerm_subnet" "SubnetA" { 25 | name = "SubnetA" 26 | virtual_network_name = "app-network" 27 | resource_group_name = local.resource_group 28 | } 29 | 30 | resource "azurerm_resource_group" "app_grp"{ 31 | name=local.resource_group 32 | location=local.location 33 | } 34 | 35 | resource "azurerm_virtual_network" "app_network" { 36 | name = "app-network" 37 | location = local.location 38 | resource_group_name = azurerm_resource_group.app_grp.name 39 | address_space = ["10.0.0.0/16"] 40 | 41 | subnet { 42 | name = "SubnetA" 43 | address_prefix = "10.0.1.0/24" 44 | } 45 | } 46 | 47 | resource "azurerm_network_interface" "app_interface" { 48 | name = "app-interface" 49 | location = local.location 50 | resource_group_name = local.resource_group 51 | 52 | ip_configuration { 53 | name = "internal" 54 | subnet_id = data.azurerm_subnet.SubnetA.id 55 | private_ip_address_allocation = "Dynamic" 56 | public_ip_address_id = azurerm_public_ip.app_public_ip.id 57 | } 58 | 59 | depends_on = [ 60 | azurerm_virtual_network.app_network, 61 | azurerm_public_ip.app_public_ip 62 | ] 63 | } 64 | 65 | resource "azurerm_windows_virtual_machine" "app_vm" { 66 | name = "appvm" 67 | resource_group_name = local.resource_group 68 | location = local.location 69 | size = "Standard_D2s_v3" 70 | admin_username = "demousr" 71 | admin_password = "Azure@123" 72 | network_interface_ids = [ 73 | azurerm_network_interface.app_interface.id, 74 | ] 75 | 76 | os_disk { 77 | caching = "ReadWrite" 78 | storage_account_type = "Standard_LRS" 79 | } 80 | 81 | source_image_reference { 82 | publisher = "MicrosoftWindowsServer" 83 | offer = "WindowsServer" 84 | sku = "2019-Datacenter" 85 | version = "latest" 86 | } 87 | 88 | depends_on = [ 89 | azurerm_network_interface.app_interface 90 | ] 91 | } 92 | 93 | resource "azurerm_public_ip" "app_public_ip" { 94 | name = "app-public-ip" 95 | resource_group_name = local.resource_group 96 | location = local.location 97 | allocation_method = "Static" 98 | } 99 | 100 | -------------------------------------------------------------------------------- /16.Lab - Adding data disks/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "ed5d6ac4-de15-4a24-b5d3-61812c9e9941" 13 | client_secret = "HOl7Q~23UVXp4J1SlBTsQQSR~dMk2CTsNERWH" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | 19 | locals { 20 | resource_group="app-grp" 21 | location="North Europe" 22 | } 23 | 24 | data "azurerm_subnet" "SubnetA" { 25 | name = "SubnetA" 26 | virtual_network_name = "app-network" 27 | resource_group_name = local.resource_group 28 | } 29 | 30 | resource "azurerm_resource_group" "app_grp"{ 31 | name=local.resource_group 32 | location=local.location 33 | } 34 | 35 | resource "azurerm_virtual_network" "app_network" { 36 | name = "app-network" 37 | location = local.location 38 | resource_group_name = azurerm_resource_group.app_grp.name 39 | address_space = ["10.0.0.0/16"] 40 | 41 | subnet { 42 | name = "SubnetA" 43 | address_prefix = "10.0.1.0/24" 44 | } 45 | } 46 | 47 | resource "azurerm_network_interface" "app_interface" { 48 | name = "app-interface" 49 | location = local.location 50 | resource_group_name = local.resource_group 51 | 52 | ip_configuration { 53 | name = "internal" 54 | subnet_id = data.azurerm_subnet.SubnetA.id 55 | private_ip_address_allocation = "Dynamic" 56 | public_ip_address_id = azurerm_public_ip.app_public_ip.id 57 | } 58 | 59 | depends_on = [ 60 | azurerm_virtual_network.app_network, 61 | azurerm_public_ip.app_public_ip 62 | ] 63 | } 64 | 65 | resource "azurerm_windows_virtual_machine" "app_vm" { 66 | name = "appvm" 67 | resource_group_name = local.resource_group 68 | location = local.location 69 | size = "Standard_D2s_v3" 70 | admin_username = "demousr" 71 | admin_password = "Azure@123" 72 | network_interface_ids = [ 73 | azurerm_network_interface.app_interface.id, 74 | ] 75 | 76 | os_disk { 77 | caching = "ReadWrite" 78 | storage_account_type = "Standard_LRS" 79 | } 80 | 81 | source_image_reference { 82 | publisher = "MicrosoftWindowsServer" 83 | offer = "WindowsServer" 84 | sku = "2019-Datacenter" 85 | version = "latest" 86 | } 87 | 88 | depends_on = [ 89 | azurerm_network_interface.app_interface 90 | ] 91 | } 92 | 93 | resource "azurerm_public_ip" "app_public_ip" { 94 | name = "app-public-ip" 95 | resource_group_name = local.resource_group 96 | location = local.location 97 | allocation_method = "Static" 98 | } 99 | 100 | resource "azurerm_managed_disk" "data_disk" { 101 | name = "data-disk" 102 | location = local.location 103 | resource_group_name = local.resource_group 104 | storage_account_type = "Standard_LRS" 105 | create_option = "Empty" 106 | disk_size_gb = 16 107 | } 108 | # Then we need to attach the data disk to the Azure virtual machine 109 | resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" { 110 | managed_disk_id = azurerm_managed_disk.data_disk.id 111 | virtual_machine_id = azurerm_windows_virtual_machine.app_vm.id 112 | lun = "0" 113 | caching = "ReadWrite" 114 | depends_on = [ 115 | azurerm_windows_virtual_machine.app_vm, 116 | azurerm_managed_disk.data_disk 117 | ] 118 | } -------------------------------------------------------------------------------- /17. Lab - Availability Sets/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "ed5d6ac4-de15-4a24-b5d3-61812c9e9941" 13 | client_secret = "HOl7Q~23UVXp4J1SlBTsQQSR~dMk2CTsNERWH" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | 19 | locals { 20 | resource_group="app-grp" 21 | location="North Europe" 22 | } 23 | 24 | 25 | resource "azurerm_resource_group" "app_grp"{ 26 | name=local.resource_group 27 | location=local.location 28 | } 29 | 30 | resource "azurerm_virtual_network" "app_network" { 31 | name = "app-network" 32 | location = local.location 33 | resource_group_name = azurerm_resource_group.app_grp.name 34 | address_space = ["10.0.0.0/16"] 35 | depends_on = [ 36 | azurerm_resource_group.app_grp 37 | ] 38 | } 39 | 40 | resource "azurerm_subnet" "SubnetA" { 41 | name = "SubnetA" 42 | resource_group_name = local.resource_group 43 | virtual_network_name = azurerm_virtual_network.app_network.name 44 | address_prefixes = ["10.0.1.0/24"] 45 | depends_on = [ 46 | azurerm_virtual_network.app_network 47 | ] 48 | } 49 | 50 | resource "azurerm_network_interface" "app_interface" { 51 | name = "app-interface" 52 | location = local.location 53 | resource_group_name = local.resource_group 54 | 55 | ip_configuration { 56 | name = "internal" 57 | subnet_id = azurerm_subnet.SubnetA.id 58 | private_ip_address_allocation = "Dynamic" 59 | public_ip_address_id = azurerm_public_ip.app_public_ip.id 60 | } 61 | 62 | depends_on = [ 63 | azurerm_virtual_network.app_network, 64 | azurerm_public_ip.app_public_ip, 65 | azurerm_subnet.SubnetA 66 | ] 67 | } 68 | 69 | resource "azurerm_windows_virtual_machine" "app_vm" { 70 | name = "appvm" 71 | resource_group_name = local.resource_group 72 | location = local.location 73 | size = "Standard_D2s_v3" 74 | admin_username = "demousr" 75 | admin_password = "Azure@123" 76 | availability_set_id = azurerm_availability_set.app_set.id 77 | network_interface_ids = [ 78 | azurerm_network_interface.app_interface.id, 79 | ] 80 | 81 | os_disk { 82 | caching = "ReadWrite" 83 | storage_account_type = "Standard_LRS" 84 | } 85 | 86 | source_image_reference { 87 | publisher = "MicrosoftWindowsServer" 88 | offer = "WindowsServer" 89 | sku = "2019-Datacenter" 90 | version = "latest" 91 | } 92 | 93 | depends_on = [ 94 | azurerm_network_interface.app_interface, 95 | azurerm_availability_set.app_set 96 | ] 97 | } 98 | 99 | resource "azurerm_public_ip" "app_public_ip" { 100 | name = "app-public-ip" 101 | resource_group_name = local.resource_group 102 | location = local.location 103 | allocation_method = "Static" 104 | depends_on = [ 105 | azurerm_resource_group.app_grp 106 | ] 107 | } 108 | 109 | resource "azurerm_managed_disk" "data_disk" { 110 | name = "data-disk" 111 | location = local.location 112 | resource_group_name = local.resource_group 113 | storage_account_type = "Standard_LRS" 114 | create_option = "Empty" 115 | disk_size_gb = 16 116 | } 117 | # Then we need to attach the data disk to the Azure virtual machine 118 | resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" { 119 | managed_disk_id = azurerm_managed_disk.data_disk.id 120 | virtual_machine_id = azurerm_windows_virtual_machine.app_vm.id 121 | lun = "0" 122 | caching = "ReadWrite" 123 | depends_on = [ 124 | azurerm_windows_virtual_machine.app_vm, 125 | azurerm_managed_disk.data_disk 126 | ] 127 | } 128 | 129 | resource "azurerm_availability_set" "app_set" { 130 | name = "app-set" 131 | location = local.location 132 | resource_group_name = local.resource_group 133 | platform_fault_domain_count = 3 134 | platform_update_domain_count = 3 135 | depends_on = [ 136 | azurerm_resource_group.app_grp 137 | ] 138 | } -------------------------------------------------------------------------------- /18. Lab - Custom Script extensions/IIS_Config.ps1: -------------------------------------------------------------------------------- 1 | import-module servermanager 2 | add-windowsfeature web-server -includeallsubfeature 3 | add-windowsfeature Web-Asp-Net45 4 | add-windowsfeature NET-Framework-Features -------------------------------------------------------------------------------- /18. Lab - Custom Script extensions/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "ed5d6ac4-de15-4a24-b5d3-61812c9e9941" 13 | client_secret = "HOl7Q~23UVXp4J1SlBTsQQSR~dMk2CTsNERWH" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | 19 | locals { 20 | resource_group="app-grp" 21 | location="North Europe" 22 | } 23 | 24 | 25 | resource "azurerm_resource_group" "app_grp"{ 26 | name=local.resource_group 27 | location=local.location 28 | } 29 | 30 | resource "azurerm_virtual_network" "app_network" { 31 | name = "app-network" 32 | location = local.location 33 | resource_group_name = azurerm_resource_group.app_grp.name 34 | address_space = ["10.0.0.0/16"] 35 | depends_on = [ 36 | azurerm_resource_group.app_grp 37 | ] 38 | } 39 | 40 | resource "azurerm_subnet" "SubnetA" { 41 | name = "SubnetA" 42 | resource_group_name = local.resource_group 43 | virtual_network_name = azurerm_virtual_network.app_network.name 44 | address_prefixes = ["10.0.1.0/24"] 45 | depends_on = [ 46 | azurerm_virtual_network.app_network 47 | ] 48 | } 49 | 50 | resource "azurerm_network_interface" "app_interface" { 51 | name = "app-interface" 52 | location = local.location 53 | resource_group_name = local.resource_group 54 | 55 | ip_configuration { 56 | name = "internal" 57 | subnet_id = azurerm_subnet.SubnetA.id 58 | private_ip_address_allocation = "Dynamic" 59 | public_ip_address_id = azurerm_public_ip.app_public_ip.id 60 | } 61 | 62 | depends_on = [ 63 | azurerm_virtual_network.app_network, 64 | azurerm_public_ip.app_public_ip, 65 | azurerm_subnet.SubnetA 66 | ] 67 | } 68 | 69 | resource "azurerm_windows_virtual_machine" "app_vm" { 70 | name = "appvm" 71 | resource_group_name = local.resource_group 72 | location = local.location 73 | size = "Standard_D2s_v3" 74 | admin_username = "demousr" 75 | admin_password = "Azure@123" 76 | availability_set_id = azurerm_availability_set.app_set.id 77 | network_interface_ids = [ 78 | azurerm_network_interface.app_interface.id, 79 | ] 80 | 81 | os_disk { 82 | caching = "ReadWrite" 83 | storage_account_type = "Standard_LRS" 84 | } 85 | 86 | source_image_reference { 87 | publisher = "MicrosoftWindowsServer" 88 | offer = "WindowsServer" 89 | sku = "2019-Datacenter" 90 | version = "latest" 91 | } 92 | 93 | depends_on = [ 94 | azurerm_network_interface.app_interface, 95 | azurerm_availability_set.app_set 96 | ] 97 | } 98 | 99 | resource "azurerm_public_ip" "app_public_ip" { 100 | name = "app-public-ip" 101 | resource_group_name = local.resource_group 102 | location = local.location 103 | allocation_method = "Static" 104 | depends_on = [ 105 | azurerm_resource_group.app_grp 106 | ] 107 | } 108 | 109 | resource "azurerm_managed_disk" "data_disk" { 110 | name = "data-disk" 111 | location = local.location 112 | resource_group_name = local.resource_group 113 | storage_account_type = "Standard_LRS" 114 | create_option = "Empty" 115 | disk_size_gb = 16 116 | } 117 | # Then we need to attach the data disk to the Azure virtual machine 118 | resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" { 119 | managed_disk_id = azurerm_managed_disk.data_disk.id 120 | virtual_machine_id = azurerm_windows_virtual_machine.app_vm.id 121 | lun = "0" 122 | caching = "ReadWrite" 123 | depends_on = [ 124 | azurerm_windows_virtual_machine.app_vm, 125 | azurerm_managed_disk.data_disk 126 | ] 127 | } 128 | 129 | resource "azurerm_availability_set" "app_set" { 130 | name = "app-set" 131 | location = local.location 132 | resource_group_name = local.resource_group 133 | platform_fault_domain_count = 3 134 | platform_update_domain_count = 3 135 | depends_on = [ 136 | azurerm_resource_group.app_grp 137 | ] 138 | } 139 | 140 | resource "azurerm_storage_account" "appstore" { 141 | name = "appstore4577687" 142 | resource_group_name = "app-grp" 143 | location = "North Europe" 144 | account_tier = "Standard" 145 | account_replication_type = "LRS" 146 | allow_blob_public_access = true 147 | } 148 | 149 | resource "azurerm_storage_container" "data" { 150 | name = "data" 151 | storage_account_name = "appstore4577687" 152 | container_access_type = "blob" 153 | depends_on=[ 154 | azurerm_storage_account.appstore 155 | ] 156 | } 157 | 158 | # Here we are uploading our IIS Configuration script as a blob 159 | # to the Azure storage account 160 | 161 | resource "azurerm_storage_blob" "IIS_config" { 162 | name = "IIS_Config.ps1" 163 | storage_account_name = "appstore4577687" 164 | storage_container_name = "data" 165 | type = "Block" 166 | source = "IIS_Config.ps1" 167 | depends_on=[azurerm_storage_container.data] 168 | } 169 | 170 | resource "azurerm_virtual_machine_extension" "vm_extension" { 171 | name = "appvm-extension" 172 | virtual_machine_id = azurerm_windows_virtual_machine.app_vm.id 173 | publisher = "Microsoft.Compute" 174 | type = "CustomScriptExtension" 175 | type_handler_version = "1.10" 176 | depends_on = [ 177 | azurerm_storage_blob.IIS_config 178 | ] 179 | settings = <ip.private_ip_address 6 | } 7 | } -------------------------------------------------------------------------------- /40. Creating multiple resources/variables.tf: -------------------------------------------------------------------------------- 1 | // These variables are defined as obejcts 2 | 3 | variable "network_details"{ 4 | description = "This contains all of the network details" 5 | default={ 6 | network_name="app-network" 7 | network_address_space="10.0.0.0/16" 8 | vm_subnet_name="SubnetA" 9 | vm_subnet_address_space="10.0.0.0/24" 10 | } 11 | } 12 | 13 | variable "vm_details"{ 14 | 15 | description = "This contains all of the virtual machine details" 16 | default={ 17 | vm_names="appvm" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /40. Creating multiple resources/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.93.0" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /40. Creating multiple resources/virtualmachines.tf: -------------------------------------------------------------------------------- 1 | // The count meta-arguement helps to create multiple resources 2 | // without the need of defining multiple resource blocks for each resource 3 | 4 | // General block to create the network interface 5 | resource "azurerm_network_interface" "app_interface" { 6 | count =2 7 | 8 | // The count object is available in expressions. Here the count starts from the number 0 9 | name = format("app-interface%s",(count.index)+1) 10 | location = azurerm_resource_group.app_grp.location 11 | resource_group_name = azurerm_resource_group.app_grp.name 12 | 13 | ip_configuration { 14 | name = "internal" 15 | subnet_id = azurerm_subnet.SubnetA.id 16 | private_ip_address_allocation = "Dynamic" 17 | } 18 | 19 | depends_on = [ 20 | azurerm_virtual_network.app_network, 21 | azurerm_subnet.SubnetA 22 | ] 23 | } 24 | 25 | 26 | 27 | // This is a general resource for the creation of virtual machines 28 | resource "azurerm_windows_virtual_machine" "appvm" { 29 | count=2 30 | name = format("%s%s",var.vm_details.vm_names,(count.index)+1) 31 | resource_group_name = azurerm_resource_group.app_grp.name 32 | location = azurerm_resource_group.app_grp.location 33 | size = "Standard_D2s_v3" 34 | admin_username = "demousr" 35 | admin_password = "Azure@123" 36 | network_interface_ids = [ 37 | azurerm_network_interface.app_interface[count.index].id, 38 | ] 39 | 40 | os_disk { 41 | caching = "ReadWrite" 42 | storage_account_type = "Standard_LRS" 43 | } 44 | 45 | source_image_reference { 46 | publisher = "MicrosoftWindowsServer" 47 | offer = "WindowsServer" 48 | sku = "2019-Datacenter" 49 | version = "latest" 50 | } 51 | 52 | depends_on = [ 53 | azurerm_network_interface.app_interface 54 | ] 55 | } 56 | 57 | -------------------------------------------------------------------------------- /5. Lab - Creating a resource group/main.tf: -------------------------------------------------------------------------------- 1 | # We first specify the terraform provider. 2 | # Terraform will use the provider to ensure that we can work with Microsoft Azure 3 | 4 | terraform { 5 | required_providers { 6 | azurerm = { 7 | source = "hashicorp/azurerm" 8 | version = "2.92.0" 9 | } 10 | } 11 | } 12 | 13 | # Here we need to mention the Azure AD Application Object credentials to allow us to work with 14 | # our Azure account 15 | 16 | provider "azurerm" { 17 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 18 | client_id = "230411ec-45e9-4650-95b2-7675131e2d1a" 19 | client_secret = "8D~7Q~y39tBTXsFXGcuVIwvGCOorRUo6dXtwX" 20 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 21 | features {} 22 | } 23 | 24 | # The resource block defines the type of resource we want to work with 25 | # The name and location are arguements for the resource block 26 | 27 | resource "azurerm_resource_group" "app_grp"{ 28 | name="app-grp" 29 | location="North Europe" 30 | } 31 | -------------------------------------------------------------------------------- /6. Lab - Creating a storage account/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.92.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "230411ec-45e9-4650-95b2-7675131e2d1a" 13 | client_secret = "8D~7Q~y39tBTXsFXGcuVIwvGCOorRUo6dXtwX" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | resource "azurerm_resource_group" "app_grp"{ 19 | name="app-grp" 20 | location="North Europe" 21 | } 22 | 23 | # Here we are creating a storage account. 24 | # The storage account service has more properties and hence there are more arguements we can specify here 25 | 26 | resource "azurerm_storage_account" "storage_account" { 27 | name = "terraformstore10090" 28 | resource_group_name = "app-grp" 29 | location = "North Europe" 30 | account_tier = "Standard" 31 | account_replication_type = "LRS" 32 | } -------------------------------------------------------------------------------- /7. Terraform state/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.92.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "230411ec-45e9-4650-95b2-7675131e2d1a" 13 | client_secret = "8D~7Q~y39tBTXsFXGcuVIwvGCOorRUo6dXtwX" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | resource "azurerm_resource_group" "app_grp"{ 19 | name="app-grp" 20 | location="North Europe" 21 | } 22 | 23 | # Here we are creating a storage account. 24 | # The storage account service has more properties and hence there are more arguements we can specify here 25 | 26 | resource "azurerm_storage_account" "storage_account" { 27 | name = "terraformstore10090" 28 | resource_group_name = "app-grp" 29 | location = "North Europe" 30 | account_tier = "Standard" 31 | account_replication_type = "LRS" 32 | allow_blob_public_access = true 33 | } -------------------------------------------------------------------------------- /8. Lab - Creating a container and a blob/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.92.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "230411ec-45e9-4650-95b2-7675131e2d1a" 13 | client_secret = "8D~7Q~y39tBTXsFXGcuVIwvGCOorRUo6dXtwX" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | resource "azurerm_resource_group" "app_grp"{ 19 | name="app-grp" 20 | location="North Europe" 21 | } 22 | 23 | # Here we are creating a storage account. 24 | # The storage account service has more properties and hence there are more arguements we can specify here 25 | 26 | resource "azurerm_storage_account" "storage_account" { 27 | name = "terraformstore10090" 28 | resource_group_name = "app-grp" 29 | location = "North Europe" 30 | account_tier = "Standard" 31 | account_replication_type = "LRS" 32 | allow_blob_public_access = true 33 | } 34 | 35 | # Here we are creating a container in the storage account 36 | resource "azurerm_storage_container" "data" { 37 | name = "data" 38 | storage_account_name = "terraformstore10090" 39 | container_access_type = "private" 40 | } 41 | 42 | # This is used to upload a local file onto the container 43 | resource "azurerm_storage_blob" "sample" { 44 | name = "sample.txt" 45 | storage_account_name = "appstore4577687" 46 | storage_container_name = "data" 47 | type = "Block" 48 | source = "sample.txt" 49 | } 50 | -------------------------------------------------------------------------------- /9. Lab - Dependencies between resources/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "2.92.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | subscription_id = "6912d7a0-bc28-459a-9407-33bbba641c07" 12 | client_id = "230411ec-45e9-4650-95b2-7675131e2d1a" 13 | client_secret = "8D~7Q~y39tBTXsFXGcuVIwvGCOorRUo6dXtwX" 14 | tenant_id = "70c0f6d9-7f3b-4425-a6b6-09b47643ec58" 15 | features {} 16 | } 17 | 18 | resource "azurerm_resource_group" "app_grp"{ 19 | name="app-grp" 20 | location="North Europe" 21 | } 22 | 23 | # Here we are creating a storage account. 24 | # The storage account service has more properties and hence there are more arguements we can specify here 25 | 26 | resource "azurerm_storage_account" "storage_account" { 27 | name = "terraformstore10090" 28 | resource_group_name = "app-grp" 29 | location = "North Europe" 30 | account_tier = "Standard" 31 | account_replication_type = "LRS" 32 | allow_blob_public_access = true 33 | } 34 | 35 | # Here we are creating a container in the storage account 36 | resource "azurerm_storage_container" "data" { 37 | name = "data" 38 | storage_account_name = "terraformstore10090" 39 | container_access_type = "private" 40 | } 41 | 42 | # This is used to upload a local file onto the container 43 | resource "azurerm_storage_blob" "sample" { 44 | name = "sample.txt" 45 | storage_account_name = "appstore4577687" 46 | storage_container_name = "data" 47 | type = "Block" 48 | source = "sample.txt" 49 | # Here we we are adding a dependency. The file can only be uploaded if the container is present 50 | # We can access the attributes of a resource in terraform via the resource_type.resource_name 51 | 52 | depends_on=[azurerm_storage_container.data] 53 | 54 | } 55 | --------------------------------------------------------------------------------