gpt4 book ai didi

azure - Terraform代码如何将本地计算机下载的war文件部署到Azure应用程序服务上

转载 作者:行者123 更新时间:2023-12-03 06:09:33 26 4
gpt4 key购买 nike

我正在下载 .war 文件。我想将其部署到应用程序服务上。为此,我创建了一个容器并将 .war 文件部署到该容器上。我不确定应用程序服务和存储之间的连接如何工作。我尝试了以下代码:

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg2" {
name = "resoursegrpthree"
location = "East US"
}

resource "azurerm_virtual_network" "virtunet1" {
name = "virnetxample"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg2.location
resource_group_name = azurerm_resource_group.rg2.name
}

resource "azurerm_subnet" "snet1" {
name = "sunetxample"
resource_group_name = azurerm_resource_group.rg2.name
virtual_network_name = azurerm_virtual_network.virtunet1.name
address_prefixes = ["10.0.1.0/24"]
delegation {
name = "delegationapp2"

service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}


resource "azurerm_app_service_plan" "azurewebapp2" {
name = "example-appservice-plan"
location = azurerm_resource_group.rg2.location
resource_group_name = azurerm_resource_group.rg2.name
kind = "Linux"
reserved = true
sku {
tier = "Standard"
size = "B1"
}
}

resource "azurerm_app_service" "appservice2" {
name = "terraformappsvc"
location = azurerm_resource_group.rg2.location
resource_group_name = azurerm_resource_group.rg2.name
app_service_plan_id = azurerm_app_service_plan.azurewebapp2.id

site_config {
java_version = "1.8"
java_container = "TOMCAT"
java_container_version = "9.0"
remote_debugging_enabled = true
}

app_settings = {
"WEBSITE_WEBDEPLOY_USE_SCM" = "true"
}

connection_string {
name = "ExampleDB"
type = "SQLServer"
value = "Server=myserver;User Id=myuser;Password=mypassword;Initial Catalog=mydb;"
}
}

resource "azurerm_storage_account" "storagesecond2" {
name = "accountstoragsec2"
resource_group_name = azurerm_resource_group.rg2.name
location = azurerm_resource_group.rg2.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "storageofwebapp" {
name = "webappcontainer"
storage_account_name = azurerm_storage_account.storagesecond2.name
container_access_type = "private"
}

resource "azurerm_storage_blob" "blobexample" {
name = "ram.war"
storage_account_name = azurerm_storage_account.storagesecond2.name
storage_container_name = azurerm_storage_container.storageofwebapp.name
type = "Block"
source = "C:/Users/s***/Downloads/My_App.war"
}

resource "azurerm_app_service_virtual_network_swift_connection" "netswixam" {
app_service_id = azurerm_app_service.appservice2.id
subnet_id = azurerm_subnet.snet1.id
}

上面的代码成功创建了容器,我下载的文件位于容器中。但是,当我单击应用程序服务域 URL 时,无法打开该应用程序。谁可以帮我这个事?提前致谢。

最佳答案

I try to write the Terraform Code on How to deploy a war file from local computer downloads onto the Azure app service

仅依靠 Terraform 不足以促进将 .war 文件从 Azure Blob 存储部署到 Azure Web 应用。虽然 Terraform 擅长建立和设置基础设施,但建议使用额外的部署脚本来有效地编排部署过程。

但是之前需要注意这些步骤

  1. 确保您的 .war 文件位于 Azure Blob 存储中。
  2. 为 blob 生成共享访问签名 (SAS) token 以进行安全下载。
  3. 使用通过 Kudu REST API 部署 .war 文件的脚本。
  4. 使用 azure devops 或任何其他 CI/CD 工具中的部署管道来运行脚本并进行部署。

这是一个使用curl的基本示例:

#!/bin/bash

# Variables
WEBAPP_NAME="your_webapp_name"
RESOURCE_GROUP="your_resource_group"
WAR_FILE_URL="https://your_storage_account.blob.core.windows.net/your_container/your_file.war?${SAS_TOKEN}"

# Get publishing profile for the Web App
PUBLISH_PROFILE=$(az webapp deployment list-publishing-profiles --name $WEBAPP_NAME --resource-group $RESOURCE_GROUP --query "[?publishMethod=='MSDeploy'].{user:userName,pass:userPWD}" -o tsv)

# Extract username and password
USERNAME=$(echo $PUBLISH_PROFILE | awk '{print $1}')
PASSWORD=$(echo $PUBLISH_PROFILE | awk '{print $2}')

# Use Kudu REST API to deploy the war file
curl -X POST -u $USERNAME:$PASSWORD \
-H "Content-Type: application/json" \
-d "{\"packageUri\":\"$WAR_FILE_URL\"}" \
"https://$WEBAPP_NAME.scm.azurewebsites.net/api/wardeploy"

我的地形配置:

    data "azurerm_resource_group" "rg2"{
name = "bolliv"
}

resource "azurerm_virtual_network" "virtunet1" {
name = "virnetxample"
address_space = ["10.0.0.0/16"]
location = data.azurerm_resource_group.rg2.location
resource_group_name = data.azurerm_resource_group.rg2.name
}

resource "azurerm_subnet" "snet1" {
name = "sunetxample"
resource_group_name = data.azurerm_resource_group.rg2.name
virtual_network_name = azurerm_virtual_network.virtunet1.name
address_prefixes = ["10.0.1.0/24"]
delegation {
name = "delegationapp2"

service_delegation {
name = "Microsoft.Web/serverFarms"
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}


resource "azurerm_app_service_plan" "azurewebapp2" {
name = "example-appservice-plan"
location = data.azurerm_resource_group.rg2.location
resource_group_name = data.azurerm_resource_group.rg2.name
kind = "Linux"
reserved = true
sku {
tier = "Standard"
size = "B1"
}
}





resource "azurerm_storage_account" "storagesecond2" {
name = "accountstoragsecvk"
resource_group_name = data.azurerm_resource_group.rg2.name
location = data.azurerm_resource_group.rg2.location
account_tier = "Standard"
account_replication_type = "LRS"
}

data "azurerm_storage_account_sas" "test" {
connection_string = "${azurerm_storage_account.testsa.primary_connection_string}"
https_only = true
resource_types {
service = true
container = false
object = false
}
services {
blob = true
queue = false
table = false
file = false
}
start = "2018-03-21"
expiry = "2020-03-21"
permissions {
read = true
write = true
delete = false
list = false
add = true
create = true
update = false
process = false
}
}

output "sas_url_query_string" {
value = data.azurerm_storage_account_sas.test.sas"
}

resource "azurerm_storage_container" "storageofwebapp" {
name = "webappcontainer"
storage_account_name = azurerm_storage_account.storagesecond2.name
//container_access_type = "public"
}

resource "azurerm_storage_blob" "blobexample" {
name = "demovk.war"
storage_account_name = azurerm_storage_account.storagesecond2.name
storage_container_name = azurerm_storage_container.storageofwebapp.name
type = "Block"
source = "C:/Users/v-bolliv/Downloads/demovk.war"
}

resource "azurerm_app_service_virtual_network_swift_connection" "netswixam" {
app_service_id = azurerm_app_service.appservice2.id
subnet_id = azurerm_subnet.snet1.id
}

resource "azurerm_app_service" "appservice2" {
name = "terraformappsvk"
location = data.azurerm_resource_group.rg2.location
resource_group_name = data.azurerm_resource_group.rg2.name
app_service_plan_id = azurerm_app_service_plan.azurewebapp2.id

site_config {
java_version = "1.8"
java_container = "TOMCAT"
java_container_version = "9.0"
remote_debugging_enabled = true
}

app_settings = {
"WEBSITE_WEBDEPLOY_USE_SCM" = "true"
}

connection_string {
name = "ExampleDB"
type = "SQLServer"
value = "Server=myserver;User Id=myuser;Password=mypassword;Initial Catalog=mydb;"
}

provisioner "local-exec" {
command = "./deploy_war.sh"
environment = {
SAS_TOKEN = data.azurerm_storage_account_sas.example.sas
}
}
}

输出:

enter image description here

enter image description here

根据要求完成配置后,Azure 和 blob 之间就会建立连接,这使您可以访问所需的 Java 应用程序。

关于azure - Terraform代码如何将本地计算机下载的war文件部署到Azure应用程序服务上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76948888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com