Provision Azure Spring Apps using Terraform
Published Dec 07 2022 05:36 AM 1,230 Views
Microsoft

This blog will demonstrate how to provision an app on Azure Spring Apps using Terraform with easy steps.

terraform.png

Terraform is one of the most popular configuration orchestration tools providing a consistent syntax and structure across cloud platforms. It allows safe and convenient design, management, and improvement of infrastructure as code. If you are working in a relatively small team and running DevOps on your own, you can embed Terraform script in your CI/CD pipeline like GitHub Actions to manage everything as code from the provision and config cloud resources to build, test and deploy the app on it. If you are working in an enterprise with a dedicated infrastructure team, you might have that infrastructure team provisioning virtual networks, databases, and compute resources with Terraform, so that the app team can focus on business logic and deploy their code with CI/CD pipeline.

 

The Spring PetClinic is a popular sample application for tutorials. It was distributed with the Spring Framework until the 2.5 version (2007).

petclinic.png

 

We will use Azure Cloud Shell to walk through the whole steps. If it is your first time using it, you can find more setup info in the official doc.

 

1.  This sample code (name it as petclinic.tf) helps you to setup the necessary configs for petclinic. You may upload this to your git repo and pull it to the azure cloud shell:

 

 

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "petclinic_terraform"
  location = "East US"
}

resource "azurerm_application_insights" "example" {
  name                = "petclinic-test-appinsights"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  application_type    = "web"
}

resource "azurerm_spring_cloud_service" "example" {
  name                = "petclinic-springcloud"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku_name            = "S0"

  config_server_git_setting {
    uri          = "https://github.com/azure-samples/spring-petclinic-microservices-config"
    label        = "config"
    search_paths = ["dir1", "dir2"]
  }

  trace {
    connection_string = azurerm_application_insights.example.connection_string
    sample_rate       = 10.0
  }

  tags = {
    Env = "staging"
  }
}

resource "azurerm_spring_cloud_app" "example" {
  name                = "petclinicapps"
  resource_group_name = azurerm_resource_group.example.name
  service_name        = azurerm_spring_cloud_service.example.name
  is_public           = true
  https_only          = true

  identity {
    type = "SystemAssigned"
  }
}

resource "azurerm_spring_cloud_java_deployment" "example" {
  name                = "default"
  spring_cloud_app_id = azurerm_spring_cloud_app.example.id
  instance_count      = 2
  jvm_options         = "-XX:+PrintGC"
  runtime_version     = "Java_11"

  environment_variables = {
    "Env" : "Staging"
  }
}

resource "azurerm_spring_cloud_active_deployment" "example" {
  spring_cloud_app_id = azurerm_spring_cloud_app.example.id
  deployment_name     = azurerm_spring_cloud_java_deployment.example.name
}

 

 

 

2. After pulling the repo with the tf file into your cloud shell, init the terraform with

 

 

terraform init

 

 

 

init.png

3. 

 

 

terraform plan -out=springapps.plan

 

 

plan.png

4. Now execute the plan with

 

 

terraform apply springapps.plan 

 

 

Once you see the complete message, the provision is done.

finish.png

You can go to the Azure portal and check:

banner.png

You can find more API on Terraform for Azure Spring Apps from the official doc.

Learn more about the new monthly free grant of 50 vCPU and 100 GB hours.

  Try it Now

Co-Authors
Version history
Last update:
‎Dec 07 2022 05:29 AM
Updated by: