Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.7 kB
3
Indexable
Never
#main.tf file
provider "azurerm" {
  features {}
}

# Reference the existing resource group
data "azurerm_resource_group" "wf_cd_2_rg" {
  name = var.resource_group_name
}
output "id" {
  value = data.azurerm_resource_group.wf_cd_2_rg.id
}
output "location" {
  value = data.azurerm_resource_group.wf_cd_2_rg.location
}

data "azurerm_virtual_network" "wf-cd-2-vnet" {
  name                = "wf-cd-2-vnet"
  resource_group_name = var.resource_group_name
}
resource "azurerm_subnet" "sql_subnet" {
  name                 = "sql-subnet"
  resource_group_name  = "wf-cd-2"
  virtual_network_name = data.azurerm_virtual_network.wf-cd-2-vnet.name
  address_prefixes     = ["10.0.1.0/24"]  # Adjust the CIDR block as needed
}

resource "azurerm_subnet" "app_service_subnet" {
  name                 = "app-service-subnet"
  resource_group_name  = "wf-cd-2"
  virtual_network_name = data.azurerm_virtual_network.wf-cd-2-vnet.name
  address_prefixes     = ["10.0.2.0/24"]  # Adjust the CIDR block as needed
}



resource "azurerm_mysql_server" "wf-cd-2-sql" {
  name                = var.mysql_server_name
  location            = data.azurerm_resource_group.wf_cd_2_rg.location
  resource_group_name = data.azurerm_resource_group.wf_cd_2_rg.name

  administrator_login          = var.mysql_server_admin_login
  administrator_login_password = var.mysql_server_admin_password

  sku_name   = "GP_Gen5_2"
  storage_mb = 5120
  version    = "5.7"

  auto_grow_enabled                 = true
  backup_retention_days             = 7
  geo_redundant_backup_enabled      = true
  infrastructure_encryption_enabled = true
  public_network_access_enabled     = false
  ssl_enforcement_enabled           = true
  ssl_minimal_tls_version_enforced  = "TLS1_2"
}

resource "azurerm_mysql_firewall_rule" "wf-cd-2-fwrule" {
  name                = "office"
  resource_group_name = data.azurerm_resource_group.wf_cd_2_rg.name
  server_name         = azurerm_mysql_server.wf-cd-2-sql.name
  start_ip_address    = "40.112.0.0"
  end_ip_address      = "40.112.255.255"
}

resource "azurerm_mysql_database" "wf-cd-2-sqldb" {
  name                = var.mysql_database_name
  resource_group_name = data.azurerm_resource_group.wf_cd_2_rg.name
  server_name         = azurerm_mysql_server.wf-cd-2-sql.name
  charset             = "utf8"
  collation           = "utf8_unicode_ci"

  # prevent the possibility of accidental data loss
  lifecycle {
    prevent_destroy = true
  }
}

resource "azurerm_service_plan" "wf-cd-2-svplan" {
  name                = var.app_service_plan_name
  location            = data.azurerm_resource_group.wf_cd_2_rg.location
  resource_group_name = data.azurerm_resource_group.wf_cd_2_rg.name
  os_type             = "Linux"
  sku_name            = "B1"
}

resource "azurerm_linux_web_app" "wf-cd-2-apps" {
  name                = var.app_service_name
  location            = data.azurerm_resource_group.wf_cd_2_rg.location
  resource_group_name = data.azurerm_resource_group.wf_cd_2_rg.name
  service_plan_id     = azurerm_service_plan.wf-cd-2-svplan.id

  site_config {
    application_stack {
      dotnet_version = "7.0"
      }
    }
#  app_settings = {
#    "Env" = "UAT"
#  }

  connection_string {
    name  = "Database"
    type  = "SQLServer"
    value = "Server=${azurerm_mysql_server.wf-cd-2-sql.fqdn};Integrated Security=SSPI"
  }
}

#backend.tf

terraform {
  backend "azurerm" {
    resource_group_name   =  "wf-cd-2"
    storage_account_name   = "wfcd2storage"
    container_name         = "wf-cd-2-stgcont"
    key                    = "testpipeline.terraform.tfstate"
  }
}

#terraform.tfvars
resource_group_name          = "wf-cd-2"
location                     = "East US 2"
mysql_server_admin_login     = "mysqladminun"
mysql_server_admin_password  = "H@Sh1CoR3!"
mysql_server_name            = "wf-cd-2-sql"
mysql_database_name          = "wf-cd-2-sqldb"
app_service_plan_name        = "wf-cd-2-svplan"
app_service_name             = "wf-cd-2-apps"

#variables.tf
variable "resource_group_name" {
  description = "The name of the existing resource group"
}

variable "location" {
  description = "The location of the existing resource group"
}

variable "mysql_server_admin_login" {
  description = "The administrator login for the MySQL server"
}

variable "mysql_server_admin_password" {
  description = "The administrator login password for the MySQL server"
}

variable "mysql_server_name" {
  description = "The name of the MySQL server"
}

variable "mysql_database_name" {
  description = "The name of the MySQL database"
}

variable "app_service_plan_name" {
  description = "The name of the App Service Plan"
}

variable "app_service_name" {
  description = "The name of the App Service"
}



Leave a Comment