├── .gitattributes ├── 9781484273272.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── TerraformwithAzure-code ├── Chapter-2 │ ├── .DS_Store │ └── TerraformScript │ │ └── main.tf ├── Chapter-3 │ ├── .DS_Store │ ├── Object │ │ └── main.tf │ ├── listofhybriditems │ │ └── main.tf │ ├── listofstrings │ │ └── main.tf │ ├── map │ │ └── main.tf │ ├── multi-provider │ │ └── main.tf │ ├── providers │ │ └── main.tf │ ├── remote-storage-client │ │ └── main.tf │ ├── remote-storage │ │ └── main.tf │ ├── sets │ │ └── main.tf │ ├── terraformfiles │ │ ├── locals.tf │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── providers.tf │ │ ├── values.tfvars │ │ ├── variables.tf │ │ └── versions.tf │ ├── tuple │ │ └── main.tf │ └── variables │ │ └── main.tf ├── Chapter-4 │ ├── .DS_Store │ ├── baseconfiguration │ │ └── main.tf │ ├── terraform-conditional │ │ └── main.tf │ ├── terraform-count-with-list-variables │ │ └── main.tf │ ├── terraform-count │ │ └── main.tf │ ├── terraform-for-each │ │ └── main.tf │ ├── terraform-for-expressions │ │ └── main.tf │ ├── terraform-foreach-property │ │ └── main.tf │ ├── terraform-local-exec-when │ │ └── main.tf │ ├── terraform-local-exec │ │ └── main.tf │ ├── terraform-string-directive-conditional │ │ └── main.tf │ ├── terraform-string-directive-multiline │ │ └── main.tf │ └── terraform-string-directives │ │ └── main.tf ├── Chapter-5 │ ├── .DS_Store │ └── modules │ │ ├── .DS_Store │ │ └── iac │ │ ├── .DS_Store │ │ ├── environments │ │ ├── .DS_Store │ │ ├── dev │ │ │ ├── dev.tfvars │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ └── variables.tf │ │ ├── prod │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── prod.tfvars │ │ │ └── variables.tf │ │ └── stage │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── stage.tfvars │ │ │ └── variables.tf │ │ └── modules │ │ ├── .DS_Store │ │ ├── fixtures │ │ ├── .DS_Store │ │ ├── groups │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ └── variables.tf │ │ └── sqlserver │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ └── variables.tf │ │ ├── resources │ │ ├── .DS_Store │ │ ├── groups │ │ │ ├── dependencies.tf │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── variables.tf │ │ │ └── versions.tf │ │ └── sqlserver │ │ │ ├── .DS_Store │ │ │ ├── dependencies.tf │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── variables.tf │ │ │ └── versions.tf │ │ └── tests │ │ ├── .DS_Store │ │ ├── integration-tests │ │ ├── booksappsql_test.go │ │ ├── go.mod │ │ └── go.sum │ │ └── unit-tests │ │ ├── booksappsql_test.go │ │ ├── go.mod │ │ └── go.sum ├── Chapter-6 │ ├── .DS_Store │ ├── Using Secrets │ │ ├── .DS_Store │ │ ├── data.tf │ │ ├── locals.tf │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── terraform.tfvars │ │ ├── variables.tf │ │ └── versions.tf │ └── admin │ │ ├── .DS_Store │ │ └── admin │ │ ├── .DS_Store │ │ ├── locals.tf │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── variables.tf │ │ └── versions.tf ├── Chapter-7 │ ├── .DS_Store │ └── TerraformEngineering │ │ ├── .DS_Store │ │ ├── Terraform │ │ ├── .DS_Store │ │ ├── environments │ │ │ ├── .DS_Store │ │ │ ├── .tflint.hcl │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── variables.tf │ │ │ └── varvalues │ │ │ │ ├── dev.tfvars │ │ │ │ ├── prod.tfvars │ │ │ │ └── test.tfvars │ │ ├── modules │ │ │ ├── .DS_Store │ │ │ └── resourcegroup │ │ │ │ ├── main.tf │ │ │ │ ├── outputs.tf │ │ │ │ └── variables.tf │ │ └── tests │ │ │ ├── .DS_Store │ │ │ ├── fixtures │ │ │ ├── .DS_Store │ │ │ └── resourcegroup │ │ │ │ ├── main.tf │ │ │ │ ├── outputs.tf │ │ │ │ └── variables.tf │ │ │ ├── integrationtests │ │ │ ├── app_test.go │ │ │ ├── go.mod │ │ │ └── go.sum │ │ │ └── unittests │ │ │ ├── go.mod │ │ │ ├── go.sum │ │ │ └── resourcegroup_test.go │ │ └── azure-pipelines.yml └── Chapter-8 │ ├── .DS_Store │ ├── Integration-tests │ ├── .DS_Store │ └── modules │ │ ├── .DS_Store │ │ ├── fixtures │ │ ├── .DS_Store │ │ ├── groups │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ └── variables.tf │ │ └── sqlserver │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ └── variables.tf │ │ ├── resources │ │ ├── .DS_Store │ │ ├── groups │ │ │ ├── dependencies.tf │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── variables.tf │ │ │ └── versions.tf │ │ └── sqlserver │ │ │ ├── .DS_Store │ │ │ ├── dependencies.tf │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── variables.tf │ │ │ └── versions.tf │ │ └── tests │ │ ├── .DS_Store │ │ └── integration-tests │ │ ├── booksappsql_test.go │ │ ├── go.mod │ │ └── go.sum │ └── unit-tests │ ├── .DS_Store │ ├── first_test │ ├── .DS_Store │ ├── fixtures │ │ └── main.tf │ └── tests │ │ ├── first_test.go │ │ ├── go.mod │ │ └── go.sum │ ├── remote-state │ ├── .DS_Store │ ├── fixtures │ │ └── main.tf │ └── tests │ │ ├── basic_test.go │ │ ├── go.mod │ │ └── go.sum │ └── sub-tests │ ├── .DS_Store │ ├── fixtures │ └── main.tf │ └── tests │ ├── basic_test.go │ ├── go.mod │ └── go.sum └── errata.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484273272.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/deep-dive-terraform-azure/43f2197521f44dbc1dcf46fa3292e11789f1914d/9781484273272.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2021 Ritesh Modi 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Deep-Dive Terraform on Azure*](https://www.apress.com/9781484273272) by Ritesh Modi (Apress, 2021). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484273272.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/deep-dive-terraform-azure/43f2197521f44dbc1dcf46fa3292e11789f1914d/TerraformwithAzure-code/Chapter-2/.DS_Store -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-2/TerraformScript/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | 3 | required_providers { 4 | azurerm = { 5 | version = "~> 2.51.0" 6 | source = "hashicorp/azurerm" 7 | } 8 | } 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { 16 | type = string 17 | description = "the name of resource group for containing resources" 18 | } 19 | 20 | variable resource_location { 21 | type = string 22 | description = "azure location for hosting resources" 23 | } 24 | 25 | variable storage_account_name { 26 | type = string 27 | description = "the name of Azure storage account" 28 | } 29 | 30 | resource "azurerm_resource_group" "myrg" { 31 | name = var.resource_group_name 32 | location = var.resource_location 33 | } 34 | 35 | resource "azurerm_storage_account" "mystorage" { 36 | name = var.storage_account_name 37 | resource_group_name = azurerm_resource_group.myrg.name 38 | location = azurerm_resource_group.myrg.location 39 | account_tier = "Standard" 40 | account_replication_type = "LRS" 41 | 42 | tags = { 43 | environment = "test" 44 | } 45 | } 46 | 47 | output resource_group_details { 48 | value = azurerm_resource_group.myrg 49 | } 50 | 51 | output storage_account_details { 52 | value = azurerm_storage_account.mystorage 53 | } 54 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/deep-dive-terraform-azure/43f2197521f44dbc1dcf46fa3292e11789f1914d/TerraformwithAzure-code/Chapter-3/.DS_Store -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/Object/main.tf: -------------------------------------------------------------------------------- 1 | variable myEmployeeObject { 2 | type = object({ 3 | name = string 4 | age = number 5 | is_married = bool 6 | companies = map(string) 7 | schools = list(string) 8 | }) 9 | default = { 10 | name = "ritesh" 11 | age = 25 12 | is_married = true 13 | companies = { 14 | company1 = "abcd" 15 | "2company" = "xyz" 16 | } 17 | schools = ["school1", "school2"] 18 | } 19 | 20 | } 21 | 22 | 23 | variable myEmployeeAny { 24 | type = object({ 25 | name = any 26 | age = any 27 | is_married = any 28 | companies = any 29 | schools = any 30 | }) 31 | default = { 32 | name = "ritesh" 33 | age = 25 34 | is_married = true 35 | companies = { 36 | company1 = "abcd" 37 | "2company" = "xyz" 38 | } 39 | schools = ["school1", "school2"] 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/listofhybriditems/main.tf: -------------------------------------------------------------------------------- 1 | variable listofhybriditems { 2 | type = list(any) 3 | default = [10, "ritesh", true, "modi"] 4 | } 5 | 6 | Output listofhybriditems { 7 | Value = var.listofhybriditems 8 | } 9 | 10 | Output singlevaluefromlistofhybriditems { 11 | Value = var.listofhybriditems[1] 12 | } 13 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/listofstrings/main.tf: -------------------------------------------------------------------------------- 1 | variable listofstrings { 2 | type = list(string) 3 | default = ["one", "two", "three", "four"] 4 | } 5 | 6 | Output listofstrings { 7 | Value = var.listofstring 8 | } 9 | 10 | Output singlevaluefromlistofstrings { 11 | Value = var.listofstrings[1] 12 | } 13 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/map/main.tf: -------------------------------------------------------------------------------- 1 | variable mymapstring { 2 | type = map(string) 3 | default = { 4 | "key1" = "value1" 5 | "key2" = "value2" 6 | "key3" = "value 3" 7 | } 8 | } 9 | 10 | 11 | output mymapoutputwhole { 12 | value = var.mymapstring 13 | } 14 | 15 | output mymapoutputone { 16 | value = var.mymapstring["key2"] 17 | } 18 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/multi-provider/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "registry.terraform.io/hashicorp/azurerm" 5 | version = "~>2.36, ~>2.40" 6 | 7 | } 8 | someotherprovider = { 9 | source = "registry.terraform.io/hashicorp/azurerm" 10 | version = "~>2.36, ~>2.40" 11 | 12 | } 13 | } 14 | } 15 | 16 | provider azurerm { 17 | features {} 18 | } 19 | 20 | provider someotherprovider { 21 | features {} 22 | } 23 | 24 | 25 | resource azurerm_resource_group rg { 26 | name= "providertestrg" 27 | location = "west europe" 28 | provider = someotherprovider 29 | } 30 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/providers/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "~> 2.41" 6 | } 7 | } 8 | } 9 | 10 | provider azurerm { 11 | features {} 12 | } 13 | 14 | resource azurerm_resource_group rg { 15 | name= "providertestrg" 16 | location = "west europe" 17 | } 18 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/remote-storage-client/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | backend "azurerm" { 3 | resource_group_name = "remotestoragerg" 4 | storage_account_name = "remotestatestorage" 5 | container_name = "statefiles" 6 | key = "prod.terraform.tfstate" 7 | 8 | } 9 | } 10 | 11 | provider "azurerm" { 12 | version = "~>2.0" 13 | 14 | features {} 15 | } 16 | 17 | resource azurerm_resource_group rgname { 18 | name = "testrgforstate" 19 | location = "west europe" 20 | } 21 | 22 | output rgoutput { 23 | value = azurerm_resource_group.rgname 24 | } 25 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/remote-storage/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable rgname { type= string} 3 | variable location { type= string} 4 | 5 | 6 | provider "azurerm" { 7 | version = "~>2.0" 8 | features {} 9 | } 10 | 11 | 12 | resource "azurerm_resource_group" "remotestaterg" { 13 | name = var.rgname 14 | location = var.location 15 | } 16 | 17 | 18 | resource "azurerm_storage_account" "remotestatestorage" { 19 | name = "remotestatestorage" 20 | resource_group_name = azurerm_resource_group.remotestaterg.name 21 | location = azurerm_resource_group.remotestaterg.location 22 | account_tier = "Standard" 23 | account_replication_type = "GRS" 24 | 25 | tags = { 26 | environment = "staging" 27 | } 28 | } 29 | 30 | 31 | resource "azurerm_storage_container" "remotestate-container" { 32 | name = "statefiles" 33 | storage_account_name = azurerm_storage_account.remotestatestorage.name 34 | container_access_type = "private" 35 | } 36 | 37 | data "azurerm_storage_account_sas" "storage-sas" { 38 | connection_string = azurerm_storage_account.remotestatestorage.primary_connection_string 39 | https_only = true 40 | 41 | start = "2020-12-25" 42 | expiry = "2021-10-20" 43 | 44 | services { 45 | blob = true 46 | queue = false 47 | table = false 48 | file = false 49 | } 50 | 51 | resource_types { 52 | service = true 53 | container = true 54 | object = true 55 | } 56 | 57 | permissions { 58 | read = true 59 | write = true 60 | delete = true 61 | list = true 62 | add = true 63 | create = true 64 | update = true 65 | process = true 66 | } 67 | 68 | } 69 | 70 | 71 | output "sas_container_query_string" { 72 | value = data.azurerm_storage_account_blob_container_sas.container-sas.sas 73 | } 74 | 75 | output "sas_storage_query_string" { 76 | value = data.azurerm_storage_account_sas.storage-sas.sas 77 | } 78 | 79 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/sets/main.tf: -------------------------------------------------------------------------------- 1 | variable myset { 2 | type = set(string) 3 | default = ["one", "two", "three", "four", "two"] 4 | } 5 | 6 | variable mysetany { 7 | type = set(any) 8 | default = [10,20,30,40, 30] 9 | } 10 | 11 | //converts every element to string 12 | variable mysethybrid { 13 | type = set(any) 14 | default = [10, "ritesh", true, "modi"] 15 | } 16 | 17 | 18 | variable setofmaps { 19 | type = set(map(string)) 20 | default = [ 21 | { 22 | name = "ritesh" 23 | age = "20" 24 | }, 25 | { 26 | name = "avni" 27 | age = "10" 28 | }, 29 | { 30 | name = "avni" 31 | age = "10" 32 | } 33 | ] 34 | } 35 | 36 | 37 | variable setofobjects { 38 | type = set(object({ 39 | location = string 40 | age = number 41 | } 42 | )) 43 | default = [ 44 | { 45 | location = "hyderabad" 46 | age = 20 47 | }, 48 | { 49 | location = "kolkata" 50 | age = 10 51 | }, 52 | { 53 | location = "bngalore" 54 | age = 30 55 | name = "ritesh" 56 | } 57 | ] 58 | } 59 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/locals.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | resourcegroupname = "${var.rgname}-${var.environment}" 3 | } 4 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/main.tf: -------------------------------------------------------------------------------- 1 | resource azurerm_resource_group rg { 2 | name= local.resourcegroupname 3 | location = var.location 4 | } 5 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/outputs.tf: -------------------------------------------------------------------------------- 1 | output resourcegroupid { 2 | value = azurerm_resource_group.rg.id 3 | } 4 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/providers.tf: -------------------------------------------------------------------------------- 1 | provider azurerm { 2 | features {} 3 | } 4 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/values.tfvars: -------------------------------------------------------------------------------- 1 | rgname = "filestructureexample" 2 | location = "west europe" 3 | environment = "dev" 4 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/variables.tf: -------------------------------------------------------------------------------- 1 | variable rgname { 2 | type = string 3 | } 4 | 5 | variable location { 6 | type = string 7 | } 8 | 9 | variable environment { 10 | type = string 11 | } 12 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/terraformfiles/versions.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | required_version = "0.13.3" 4 | required_providers { 5 | azurerm = { 6 | source = "registry.terraform.io/hashicorp/azurerm" 7 | version = "~>2.36, ~>2.40" 8 | 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/tuple/main.tf: -------------------------------------------------------------------------------- 1 | variable simpletuple { 2 | type = tuple([string, number, bool]) 3 | default = ["ritesh", 10, false] 4 | } 5 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-3/variables/main.tf: -------------------------------------------------------------------------------- 1 | Variable resourceGroupName { 2 | Type= string 3 | Default = "dev-ecommerce-rg" 4 | Description = "This is used for naming the resource group related to ecommerce application in development environment" 5 | } 6 | 7 | variable rgname { 8 | type = string 9 | validation { 10 | condition = (length(var.rgname) <= 90 && length(var.rgname) > 2 && can(regex("[-\\w\\._\\(\\)]+", var.rgname)) ) 11 | error_message = "Resource group name may only contain alphanumeric characters, dash, underscores, parentheses and periods." 12 | } 13 | sensitive = true 14 | } 15 | 16 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/deep-dive-terraform-azure/43f2197521f44dbc1dcf46fa3292e11789f1914d/TerraformwithAzure-code/Chapter-4/.DS_Store -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/baseconfiguration/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | } 9 | 10 | provider "azurerm" { 11 | features {} 12 | } 13 | 14 | variable resource_group_name { type= string } 15 | variable resource_group_location { type= string } 16 | variable storage_account_name { type= string } 17 | 18 | resource "azurerm_resource_group" "resource_group" { 19 | name = var.resource_group_name 20 | location = var.resource_group_location 21 | } 22 | 23 | resource "azurerm_storage_account" "storage_account" { 24 | name = var.storage_account_name 25 | location = azurerm_resource_group.resource_group.location 26 | resource_group_name = azurerm_resource_group.resource_group.name 27 | account_replication_type = "LRS" 28 | account_tier = "Standard" 29 | account_kind = "StorageV2" 30 | min_tls_version = "TLS1_2" 31 | 32 | enable_https_traffic_only = true 33 | } 34 | 35 | output storage_account_ids { 36 | value = azurerm_storage_account.storage_account.id 37 | } 38 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-conditional/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type=string } 16 | variable resource_group_location { type=string } 17 | variable storage_account_name { type=string } 18 | variable storage_account_eastus { type=bool } 19 | 20 | resource "azurerm_resource_group" "resource_group" { 21 | name = var.resource_group_name 22 | location = var.resource_group_location 23 | } 24 | 25 | resource "azurerm_storage_account" "storage_account_eastus" { 26 | count = var.storage_account_eastus == true ? 1 : 0 27 | name = var.storage_account_name 28 | location = "eastus" 29 | resource_group_name = azurerm_resource_group.resource_group.name 30 | account_replication_type = "LRS" 31 | account_tier = "Standard" 32 | account_kind = "StorageV2" 33 | min_tls_version = "TLS1_2" 34 | 35 | enable_https_traffic_only = true 36 | } 37 | 38 | resource "azurerm_storage_account" "storage_account_westus" { 39 | count = var.storage_account_eastus == false ? 1 : 0 40 | name = var.storage_account_name 41 | location = "westus" 42 | resource_group_name = azurerm_resource_group.resource_group.name 43 | account_replication_type = "LRS" 44 | account_tier = "Standard" 45 | account_kind = "StorageV2" 46 | min_tls_version = "TLS1_2" 47 | 48 | enable_https_traffic_only = true 49 | } 50 | 51 | output storage_account_id_westus { 52 | value = azurerm_storage_account.storage_account_westus[*].id 53 | } 54 | 55 | output storage_account_location_westus { 56 | value = azurerm_storage_account.storage_account_westus[*].location 57 | } 58 | 59 | output storage_account_id_eastus { 60 | value = azurerm_storage_account.storage_account_eastus[*].id 61 | } 62 | 63 | output storage_account_location_eastus { 64 | value = azurerm_storage_account.storage_account_eastus[*].location 65 | } 66 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-count-with-list-variables/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type=string } 16 | variable resource_group_location { type=string } 17 | variable storage_account_names { type=list } 18 | 19 | 20 | resource "azurerm_resource_group" "resource_group" { 21 | name = var.resource_group_name 22 | location = var.resource_group_location 23 | } 24 | 25 | resource "azurerm_storage_account" "storage_account" { 26 | count = length(var.storage_account_names) 27 | name = "${var.storage_account_names[count.index]}" 28 | location = azurerm_resource_group.resource_group.location 29 | resource_group_name = azurerm_resource_group.resource_group.name 30 | account_replication_type = "LRS" 31 | account_tier = "Standard" 32 | account_kind = "StorageV2" 33 | min_tls_version = "TLS1_2" 34 | 35 | enable_https_traffic_only = true 36 | } 37 | 38 | output storage_account_ids { 39 | value = azurerm_storage_account.storage_account[*].id 40 | } 41 | 42 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-count/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type=string } 16 | variable resource_group_location { type=string } 17 | variable storage_account_name { type=string } 18 | variable nos_of_storage_accounts { type=number } 19 | 20 | resource "azurerm_resource_group" "resource_group" { 21 | name = var.resource_group_name 22 | location = var.resource_group_location 23 | } 24 | 25 | resource "azurerm_storage_account" "storage_account" { 26 | count = var.nos_of_storage_accounts 27 | name = "${var.storage_account_name}${count.index}" 28 | location = azurerm_resource_group.resource_group.location 29 | resource_group_name = azurerm_resource_group.resource_group.name 30 | account_replication_type = "LRS" 31 | account_tier = "Standard" 32 | account_kind = "StorageV2" 33 | min_tls_version = "TLS1_2" 34 | 35 | enable_https_traffic_only = true 36 | } 37 | 38 | output storage_account_ids { 39 | value = azurerm_storage_account.storage_account[*].id 40 | } 41 | 42 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-for-each/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type=string } 16 | variable resource_group_location { type=string } 17 | variable storage_account_info { type=map } 18 | 19 | resource "azurerm_resource_group" "resource_group" { 20 | name = var.resource_group_name 21 | location = var.resource_group_location 22 | } 23 | 24 | resource "azurerm_storage_account" "storage_account" { 25 | for_each = var.storage_account_info 26 | name = each.key 27 | location = each.value 28 | resource_group_name = azurerm_resource_group.resource_group.name 29 | account_replication_type = "LRS" 30 | account_tier = "Standard" 31 | account_kind = "StorageV2" 32 | min_tls_version = "TLS1_2" 33 | 34 | enable_https_traffic_only = true 35 | } 36 | 37 | output storage_account_location_eastus { 38 | value = azurerm_storage_account.storage_account["mapstoragename1"].name 39 | } 40 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-for-expressions/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable storage_account_info { type=map } 16 | 17 | 18 | 19 | output storage_account_location_eastus { 20 | value = {for name, location in var.storage_account_info: lower(name) => upper(location)} 21 | } 22 | 23 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-foreach-property/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | 10 | } 11 | 12 | provider "azurerm" { 13 | features {} 14 | } 15 | 16 | variable resource_group_name { type=string } 17 | variable resource_group_location { type=string } 18 | variable custom_tags { type=map } 19 | variable app_service_plan_name { type=string } 20 | variable app_service_plan_kind{ type=string } 21 | variable app_service_plan_sku { type = map } 22 | variable app_service_name{ type=string } 23 | variable connection_strings { type = list(object( 24 | { 25 | name = string, 26 | type = string, 27 | value = string 28 | } 29 | ))} 30 | 31 | locals { 32 | default_tags = { 33 | "department" : "finance", 34 | "owner" : "riteshmodi" 35 | } 36 | 37 | app_size = "B2" 38 | } 39 | 40 | resource "azurerm_resource_group" "resource_group" { 41 | name = var.resource_group_name 42 | location = var.resource_group_location 43 | } 44 | 45 | resource "azurerm_app_service_plan" "app_service_plan" { 46 | name = var.app_service_plan_name 47 | 48 | location = azurerm_resource_group.resource_group.location 49 | resource_group_name = azurerm_resource_group.resource_group.name 50 | kind = var.app_service_plan_kind 51 | reserved = var.app_service_plan_kind == "Linux" ? true : false 52 | 53 | sku { 54 | capacity = lookup(var.app_service_plan_sku, "capacity", null) 55 | size = lookup(var.app_service_plan_sku, "size", null) 56 | tier = lookup(var.app_service_plan_sku, "tier", null) 57 | } 58 | 59 | tags = merge(local.default_tags, var.custom_tags) 60 | } 61 | 62 | resource "azurerm_app_service" "app_service" { 63 | name = var.app_service_name 64 | location = azurerm_resource_group.resource_group.location 65 | resource_group_name = azurerm_resource_group.resource_group.name 66 | app_service_plan_id = azurerm_app_service_plan.app_service_plan.id 67 | 68 | dynamic "connection_string" { 69 | for_each = var.connection_strings 70 | content { 71 | name = lookup(connection_string.value, "name", null) 72 | type = lookup(connection_string.value, "type", null) 73 | value = lookup(connection_string.value, "value", null) 74 | } 75 | } 76 | 77 | https_only = true 78 | } 79 | 80 | output app_service_plan_identifier { 81 | value = azurerm_app_service_plan.app_service_plan.id 82 | } 83 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-local-exec-when/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type= string } 16 | variable resource_group_location { type= string } 17 | 18 | resource "azurerm_resource_group" "resource_group" { 19 | name = var.resource_group_name 20 | location = var.resource_group_location 21 | 22 | provisioner "local-exec" { 23 | command = "echo ${azurerm_resource_group.resource_group.id}" 24 | } 25 | } 26 | 27 | resource "null_resource" mynullresource { 28 | provisioner "local-exec" { 29 | when = "destroy" 30 | command = "date" 31 | interpreter = ["bash", "-c"] 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-local-exec/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type= string } 16 | variable resource_group_location { type= string } 17 | 18 | resource "azurerm_resource_group" "resource_group" { 19 | name = var.resource_group_name 20 | location = var.resource_group_location 21 | 22 | provisioner "local-exec" { 23 | command = "echo ${azurerm_resource_group.resource_group.id}" 24 | } 25 | } 26 | 27 | resource "null_resource" mynullresource { 28 | provisioner "local-exec" { 29 | command = "date" 30 | interpreter = ["bash", "-c"] 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-string-directive-conditional/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | required_version = "~> 0.14" 10 | } 11 | 12 | provider "azurerm" { 13 | features {} 14 | } 15 | 16 | variable resource_group_name { type=list(string) } 17 | 18 | output ifstringdirective { 19 | value = "%{ if var.resource_group_name == "simpletfexample" } name of resource group is ${ var.resource_group_name } %{else} improper resource group name %{ endif }" 20 | 21 | } 22 | -------------------------------------------------------------------------------- /TerraformwithAzure-code/Chapter-4/terraform-string-directive-multiline/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | azurerm = { 4 | source = "hashicorp/azurerm" 5 | version = "=2.53.0" 6 | } 7 | } 8 | 9 | } 10 | 11 | provider "azurerm" { 12 | features {} 13 | } 14 | 15 | variable resource_group_name { type=list(string) } 16 | 17 | output stringd { 18 | value = <