Saturday, July 16, 2022

How to import existing Azure resources into Terraform Management? | Using Terraform to import existing resources on Azure

Let's say that you have created any resources in Azure Cloud by some other means(manually or using azure CLI) before you started using Terraform. You can import them under Terraform management instead of destroying and re-creating them from scratch. The terraform import command can be used to import existing resources. 

This command currently can import only one resource at a time. This means you can't yet point Terraform import to an entire collection of resources such as azure resource group and import all the resources under that group.

To achieve this import exercise, we will do the following:

1. Create resource group, app service manually in Azure cloud (yes, by not using terraform)

2. Create terraform file and write code to create the resource. 

3. Run terraform apply to see the error complaining resource exists

4. run terraform import

5. Verify in the state file that resource is imported into Terraform state.

6. Perform terraform destroy to clean up the imported resources

Watch the steps in YouTube channel:


Pre-requisites:

1. Install Terraform on your machine

2. Azure account setup

3. VS Code or any IDE


Let's create the resource manually in Azure Cloud first

Login to Azure portal - https://portal.azure.com/#home

Create App service manually in portal 


Create terraform file 

To import any resource, create a tf file first write a resource block for it in your configuration, establishing the name by which it will be known to Terraform:


terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=2.99.0"
    }
  }
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}
# Add this block code if you want to import existing Resource group
resource "azurerm_resource_group" "rg" {
  name     = "myManualRG"
  location = "southcentralus"
  tags = {
    owner       = "ak"
    orgnization = "MyOrg"
  }
}
resource "azurerm_service_plan" "service-plan" {
  name                = "myServicePlan"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  os_type             = "Linux"
  sku_name            = "S1"
  tags = {
          environment = "dev"
  }
}
# Create JAVA app service
resource "azurerm_linux_web_app" "app-service" {
  name = "MyAwesomeSuperWebApp"
  location = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  service_plan_id = azurerm_service_plan.service-plan.id
  site_config {
    minimum_tls_version = "1.0"
    application_stack {
      java_server         = "TOMCAT"
      java_version        = "java11"
      java_server_version = 9.5
    }
  }
tags = {
    environment = "dev"
  }
}


Now run terraform apply command



terraform import can be run to import the resource under Terraform management.

Import Resource Group created manually in Azure Cloud into Terraform Management:

terraform import azurerm_resource_group.rg /subscriptions/XXXXX/resourceGroups/myManualRG


Terraform State View
Execute --> terraform state list
The above command will show the below output.
azurerm_resource_group.rg


This command will list the resources that are part of Terraform state.

Import App service plan created manually in Azure Cloud into Terraform Management:

terraform import azurerm_app_service_plan.service-plan /subscriptions/XXXXXX/resourceGroups/myManualRG/providers/Microsoft.Web/serverfarms/myServicePlan

Terraform State View
Execute --> terraform state list
The above command will show the below output.
azurerm_app_service_plan.service-plan
azurerm_resource_group.rg

Import App Service created manually in Azure Cloud into Terraform Management:
terraform import azurerm_app_service.app-service /subscriptions/XXXX/resourceGroups/myManualRG/providers/Microsoft.Web/sites/myPythonWebApp

Terraform State View
Execute --> terraform state list


The above command will show the below output.

azurerm_app_service.app-service
azurerm_app_service_plan.service-plan
azurerm_resource_group.rg

finally clean up the resources 
Execute --> terraform destroy
enter yes


This will clean up all three resources we have imported from Azure cloud.

How to create Web App in Azure Cloud | Create App service in Microsoft Azure

WebApp is an App service provide by Azure Cloud for hosting your application. It supports .NET, Java, PHP,  Python and NodeJS. It is a fully managed Platform as a Serice (PaaS) where developers can deploy mobile or web applications in Azure Cloud.


Steps for Creating WebApp in Azure Portal

1. Now to login portal.azure.com
2. Click on App services


3.Click on + Add or click on Create app service



Click on Web App.



Choose your Azure subscription

Create a new resource group(for first time if you create an app service, otherwise you can use existing group name)
Enter App service name(it should be unique)
Publish as Code
Run time stack as Java 11 --> Tomcat 8.5
Operating System as Linux
Region as South Central US or what ever you are close to.
Enter LinuxPlan name
Choose SKU and size as given below:


Choose DEV/Test and use 1 GB memory




Click on Apply
Now click on Apply & Create.
This will take bit time to create the app service. Once WebApp is created, go to resources, click on Webapp name and click on the URL.




You should see the app service home page some thing like below:



GitHub Actions CICD Pipeline to Deploy Java WebApp into Azure App Service | Integration GitHub Actions with Azure App Service

Pre-requisites: Make sure Java web app is setup in GitHub Azure subscription to create web app What are we going to do in this lab? 1. Creat...