{"id":2149,"date":"2024-12-20T17:10:31","date_gmt":"2024-12-20T17:10:31","guid":{"rendered":"https:\/\/technetsolutions.net\/?p=2149"},"modified":"2024-12-20T17:10:33","modified_gmt":"2024-12-20T17:10:33","slug":"terraform-overview-and-uses-to-provision-azure-iaas-infrastructure","status":"publish","type":"post","link":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/","title":{"rendered":"Terraform Overview and Uses to Provision Azure IaaS infrastructure:"},"content":{"rendered":"\n<p><strong>Terraform Overview and Uses to Provision Azure IaaS infrastructure:<\/strong><\/p>\n\n\n\n<p>Terraform is the Orchestration tool that means we can use same code to destroy infrastructure .Terraform uses Client-Only Architecture. Terraform Support Immutable infrastructure.<\/p>\n\n\n\n<p><strong><u>Terraform Work Flow<\/u><\/strong><\/p>\n\n\n\n<p>1) init Phase<br>$ terraform init<br>it will downlaod the Plugins and APi\u2019s from the provider and store into .terraform directory<\/p>\n\n\n\n<p>2) execution Phase<br>$ terraform validate<br>$ terraform plan<br>$ terraform apply<\/p>\n\n\n\n<p>3) destroy phase<br>$ terraform destory<\/p>\n\n\n\n<p><strong>Connecting to Azure Cloud with the terraform client<\/strong><\/p>\n\n\n\n<p>Method to connect to Azure from terraform using the keys<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>az login method ( not secure )<\/li>\n\n\n\n<li>Authenticate to Azure with service principal ( connect with azure AD now named changed as Entra ID)<br>https:\/\/learn.microsoft.com\/en-us\/azure\/developer\/terraform\/authenticate-to-azure-with-service-principle?tabs=bash<\/li>\n<\/ol>\n\n\n\n<p>(1) OS level , we can add them into env variables and save them into the profile file .bashrc under the home\/login directory of the user<br>(2 ) hardcode the keys in the tf config file<\/p>\n\n\n\n<p>Example: In vscode create main.tf<\/p>\n\n\n\n<p><strong><u>Terraform Settings Block<\/u><\/strong><\/p>\n\n\n\n<p>terraform {<br>required_providers {<br>azurerm = {<br>source = \u201chashicorp\/azurerm\u201d<br>version = \u201c= 3.0.0\u201d # Optional but recommended in production<br>}<br>}<br>}<\/p>\n\n\n\n<p>provider \u201cazurerm\u201d {<br>features {}<br>}<\/p>\n\n\n\n<p><strong>Create Resource Group<\/strong><\/p>\n\n\n\n<p>resource \u201cazurerm_resource_group\u201d \u201cmy_demo_rg1\u201d {<br>location = \u201ceast us\u201d<br>name = \u201cmy-demo-rg1\u201d<\/p>\n\n\n\n<p>( save the file )<\/p>\n\n\n\n<p>$ terraform init<\/p>\n\n\n\n<p>$ terraform validate<\/p>\n\n\n\n<p>$ terraform fmt<\/p>\n\n\n\n<p>$ terraform plan<\/p>\n\n\n\n<p>$ terraform.exe apply \u2013auto-approve<\/p>\n\n\n\n<p>( go back and check the rescource under rescource group it will be there , refresh the screen )<\/p>\n\n\n\n<p>destroy<\/p>\n\n\n\n<p>( go back and check the rescource under rescource group will be delete , refresh the screen )<\/p>\n\n\n\n<p>$ terraform.exe destroy \u2013auto-approve<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<p><strong>authenticate via service principal<\/strong><br><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/developer\/terraform\/authenticate-to-azure-with-service-principle?tabs=bash\">https:\/\/learn.microsoft.com\/en-us\/azure\/developer\/terraform\/authenticate-to-azure-with-service-principle?tabs=bash<\/a><\/p>\n\n\n\n<p>setup the service principal authentication<\/p>\n\n\n\n<p>$ az ad sp create-for-rbac &#8211;name &lt;service_principal_name&gt; &#8211;role Contributor &#8211;scopes \/subscriptions\/&lt;subscription_id&gt;<\/p>\n\n\n\n<p>( it show error , login to az login )<\/p>\n\n\n\n<p>restore the vscode window<\/p>\n\n\n\n<p>$ az login<\/p>\n\n\n\n<p>( it show the error )<\/p>\n\n\n\n<p>$ az account clear<\/p>\n\n\n\n<p>$ az config set core.enable_broker_on_windows=false<\/p>\n\n\n\n<p>login again<\/p>\n\n\n\n<p>$ az login<\/p>\n\n\n\n<p>$&nbsp; az account show<\/p>\n\n\n\n<p>( it will show the subscription id )<\/p>\n\n\n\n<p>$ export MSYS_NO_PATHCONV=1&nbsp;&nbsp;<\/p>\n\n\n\n<p>$ az ad sp create-for-rbac &#8211;name bofa-tf &#8211;role Contributor &#8211;scopes \/subscriptions\/&lt;subs id&gt;<\/p>\n\n\n\n<p>( if it shows error , then add \/\/ )<\/p>\n\n\n\n<p>$&nbsp; az ad sp create-for-rbac &#8211;name bofa-tf &#8211;role Contributor &#8211;scopes \/\/subscriptions\/\/&lt;subs id&gt;<\/p>\n\n\n\n<p>( it will show the details , copy and paste in some notepad )<\/p>\n\n\n\n<p>(1) OS level , we can add them into env variables&nbsp; and save them into the profile file .bashrc under the home\/login directory of the user<\/p>\n\n\n\n<p>check the variable from os level<\/p>\n\n\n\n<p>$ env | grep -i arm<\/p>\n\n\n\n<p>( refer the variable from the website )<\/p>\n\n\n\n<p>export ARM_SUBSCRIPTION_ID=&#8221;&lt;azure_subscription_id&gt;&#8221;<\/p>\n\n\n\n<p>export ARM_TENANT_ID=&#8221;&lt;azure_subscription_tenant_id&gt;&#8221;<\/p>\n\n\n\n<p>export ARM_CLIENT_ID=&#8221;&lt;service_principal_appid&gt;&#8221;<\/p>\n\n\n\n<p>export ARM_CLIENT_SECRET=&#8221;&lt;service_principal_password&gt;&#8221;<\/p>\n\n\n\n<p>$ env | grep -i arm<\/p>\n\n\n\n<p>copy then output and paste it into notepad<\/p>\n\n\n\n<p>logut out and delete the .azure directory<\/p>\n\n\n\n<p>$ az logout<\/p>\n\n\n\n<p>&nbsp;$ rm -rf ~\/.azure<\/p>\n\n\n\n<p>run the code<\/p>\n\n\n\n<p>$ terraform&nbsp; plan<\/p>\n\n\n\n<p>( it works )<\/p>\n\n\n\n<p>$ terraform apply &#8211;auto-approve<\/p>\n\n\n\n<p>( dash board and check )<\/p>\n\n\n\n<p>destory<\/p>\n\n\n\n<p>$ terraform destroy&nbsp; &#8211;auto-approve<\/p>\n\n\n\n<p>$ exit<\/p>\n\n\n\n<p>load the shell&nbsp; and change the directory<\/p>\n\n\n\n<p>$ env | grep -i arm<\/p>\n\n\n\n<p>( there wont be any variables )<\/p>\n\n\n\n<p>$ terraform apply &#8211;auto-approve<\/p>\n\n\n\n<p>it will fail<\/p>\n\n\n\n<p>Solution add them in .bashrc file unde the home or login directory<\/p>\n\n\n\n<p>\/c\/Users\/Administrator<\/p>\n\n\n\n<p>open the .bashrc from the vscode<\/p>\n\n\n\n<p>paste it then add export keywork before eact ARM line<\/p>\n\n\n\n<p>^s save the file<\/p>\n\n\n\n<p>$ exit<\/p>\n\n\n\n<p>and load the shell again<\/p>\n\n\n\n<p>$ env | grep -i arm<\/p>\n\n\n\n<p>$ cd&nbsp; rescoure-group<\/p>\n\n\n\n<p>$ terraform plan<\/p>\n\n\n\n<p>( it work )<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Terraform Overview and Uses to Provision Azure IaaS infrastructure: Terraform is the Orchestration tool that means we can use same code to destroy infrastructure .Terraform uses Client-Only Architecture. Terraform Support Immutable infrastructure. Terraform Work Flow 1) init Phase$ terraform initit will downlaod the Plugins and APi\u2019s from the provider and store into .terraform directory 2) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2149","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Terraform Overview and Uses to Provision Azure IaaS infrastructure: - technetsolutions.net<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Terraform Overview and Uses to Provision Azure IaaS infrastructure: - technetsolutions.net\" \/>\n<meta property=\"og:description\" content=\"Terraform Overview and Uses to Provision Azure IaaS infrastructure: Terraform is the Orchestration tool that means we can use same code to destroy infrastructure .Terraform uses Client-Only Architecture. Terraform Support Immutable infrastructure. Terraform Work Flow 1) init Phase$ terraform initit will downlaod the Plugins and APi\u2019s from the provider and store into .terraform directory 2) [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/\" \/>\n<meta property=\"og:site_name\" content=\"technetsolutions.net\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechnetSolutions.net\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-20T17:10:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-20T17:10:33+00:00\" \/>\n<meta name=\"author\" content=\"technetsolution\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"technetsolution\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/\",\"url\":\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/\",\"name\":\"Terraform Overview and Uses to Provision Azure IaaS infrastructure: - technetsolutions.net\",\"isPartOf\":{\"@id\":\"https:\/\/technetsolutions.net\/#website\"},\"datePublished\":\"2024-12-20T17:10:31+00:00\",\"dateModified\":\"2024-12-20T17:10:33+00:00\",\"author\":{\"@id\":\"https:\/\/technetsolutions.net\/#\/schema\/person\/33a2eaab7eef8be9022fc9a4c37c2136\"},\"breadcrumb\":{\"@id\":\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/technetsolutions.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Terraform Overview and Uses to Provision Azure IaaS infrastructure:\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/technetsolutions.net\/#website\",\"url\":\"https:\/\/technetsolutions.net\/\",\"name\":\"technetsolutions.net\",\"description\":\"technetsolutions.net\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/technetsolutions.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/technetsolutions.net\/#\/schema\/person\/33a2eaab7eef8be9022fc9a4c37c2136\",\"name\":\"technetsolution\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/technetsolutions.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b3e06688ecbbb11dfcb794106978d4a0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b3e06688ecbbb11dfcb794106978d4a0?s=96&d=mm&r=g\",\"caption\":\"technetsolution\"},\"url\":\"https:\/\/technetsolutions.net\/index.php\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Terraform Overview and Uses to Provision Azure IaaS infrastructure: - technetsolutions.net","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/","og_locale":"en_US","og_type":"article","og_title":"Terraform Overview and Uses to Provision Azure IaaS infrastructure: - technetsolutions.net","og_description":"Terraform Overview and Uses to Provision Azure IaaS infrastructure: Terraform is the Orchestration tool that means we can use same code to destroy infrastructure .Terraform uses Client-Only Architecture. Terraform Support Immutable infrastructure. Terraform Work Flow 1) init Phase$ terraform initit will downlaod the Plugins and APi\u2019s from the provider and store into .terraform directory 2) [&hellip;]","og_url":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/","og_site_name":"technetsolutions.net","article_publisher":"https:\/\/www.facebook.com\/TechnetSolutions.net","article_published_time":"2024-12-20T17:10:31+00:00","article_modified_time":"2024-12-20T17:10:33+00:00","author":"technetsolution","twitter_card":"summary_large_image","twitter_misc":{"Written by":"technetsolution","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/","url":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/","name":"Terraform Overview and Uses to Provision Azure IaaS infrastructure: - technetsolutions.net","isPartOf":{"@id":"https:\/\/technetsolutions.net\/#website"},"datePublished":"2024-12-20T17:10:31+00:00","dateModified":"2024-12-20T17:10:33+00:00","author":{"@id":"https:\/\/technetsolutions.net\/#\/schema\/person\/33a2eaab7eef8be9022fc9a4c37c2136"},"breadcrumb":{"@id":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/technetsolutions.net\/index.php\/2024\/12\/20\/terraform-overview-and-uses-to-provision-azure-iaas-infrastructure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/technetsolutions.net\/"},{"@type":"ListItem","position":2,"name":"Terraform Overview and Uses to Provision Azure IaaS infrastructure:"}]},{"@type":"WebSite","@id":"https:\/\/technetsolutions.net\/#website","url":"https:\/\/technetsolutions.net\/","name":"technetsolutions.net","description":"technetsolutions.net","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/technetsolutions.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/technetsolutions.net\/#\/schema\/person\/33a2eaab7eef8be9022fc9a4c37c2136","name":"technetsolution","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/technetsolutions.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b3e06688ecbbb11dfcb794106978d4a0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b3e06688ecbbb11dfcb794106978d4a0?s=96&d=mm&r=g","caption":"technetsolution"},"url":"https:\/\/technetsolutions.net\/index.php\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/posts\/2149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/comments?post=2149"}],"version-history":[{"count":4,"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/posts\/2149\/revisions"}],"predecessor-version":[{"id":2153,"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/posts\/2149\/revisions\/2153"}],"wp:attachment":[{"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/media?parent=2149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/categories?post=2149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/technetsolutions.net\/index.php\/wp-json\/wp\/v2\/tags?post=2149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}