Thursday, February 9, 2023

How to create Azure Resources using Terraform in Azure Cloud | Automate Infrastructure setup using Terraform in Azure Cloud | Create Azure WebApp using Terraform

Azure Web App service lets us quickly build, deploy, and scale enterprise-grade web, mobile, and API apps running on any platform. It supports .NET, .NET core, Java, PHP,  Python, Ruby and NodeJS. It is a fully managed Platform as a Serice (PaaS) where developers can deploy mobile, API apps or web applications in Azure Cloud in matter of seconds.

We will provision WebApp in Azure cloud using Terraform.

Terraform is an open-source tool for provisioning and managing cloud infrastructure. Terraform can provision resources on any cloud platform. 

Terraform allows you to create infrastructure in configuration files(tf files) that describe the topology of cloud resources. These resources include virtual machines, storage accounts, and networking interfaces. The Terraform CLI provides a simple mechanism to deploy and version the configuration files to Azure.


Azure CLI needs to be installed.

Terraform needs to be installed.

How to Authenticate with Azure?

Terraform can authenticate with Azure in many ways, in this example we will use Azure CLI to authenticate with Azure and then we will create resources using Terraform.

Logging into the Azure CLI

Login to the Azure CLI using:

az login

The above command will open the browser and will ask your Microsoft account details. Once you logged in, you can see the account info by executing below command:

az account list

Now create a directory to store Terraform files.

mkdir azure-terraform

cd azure-terraform

Let's create a terraform file to use azure provider. To configure Terraform to use the Default Subscription defined in the Azure CLI, use the below cod.

sudo vi

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.42.0"

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}

Now initialize the working directory

Perform the below command:

terraform init

Once directory is initialized, you can start writing code for setting up the infrastructure. 

Create WebApp(App Service) using Terraform script

sudo vi

# Create a resource group
resource "azurerm_resource_group" "dev-rg" {
  name     = "dev-environment-rg"
  location = "South Central US"

# Create app service plan
resource "azurerm_service_plan" "service-plan" {
  name = "simple-service-plan"
  location =
  resource_group_name =
  os_type = "Linux"
  sku_name = "S1"
  tags = {
    environment = "dev"

# Create Web App service for hosting Java Web App in Tomcat server
resource "azurerm_linux_web_app" "app-service" {
  name = "mysuperjavawebapp2"
  location =
  resource_group_name =
  service_plan_id =

        site_config {
        always_on          = true
          application_stack {
             java_server         = "TOMCAT"
             java_server_version = "8.5"
             java_version        = "11"
tags = {
    environment = "dev"

Now Perform the below command to validate terraform files.

terraform validate

perform plan command to see how many resources will be created.

terraform plan

terraform apply --auto-approve

Now Login into Azure Cloud to see all the resources created.

To clean up the resources created from Terraform

terraform destroy --auto-approve

No comments:

Post a Comment