【问题标题】:How to access azure cloud shell using C# code如何使用 C# 代码访问 azure cloud shell
【发布时间】:2021-08-19 10:34:08
【问题描述】:

无法使用 C# 程序访问 azure cloud shell

请帮助我,我无法访问 azure cloud shell,我在 azure cloud shell 中设置了一个脚本,但现在我无法使用 C# 代码打开 azure cloud shell。我想要一个蔚蓝的云壳

预期结果 invoke access azure cloud shell ==> run terraform script ==> show output

C# 代码


string extendCommand = "terraform init ";
ProcessStartInfo procStartInfo = new ProcessStartInfo("https://shell.azure.com/powershell", "/terraform_test" + extendCommand) //terraform_test folder name
            {
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            };

            Process proc = new Process
            {
                StartInfo = procStartInfo
            };

这是我在本地和云端运行的 terraform 脚本

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.46.0"
    }
  }
}

#provider "azurerm" {
# version = "~> 1.34.0"
#}

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

# Hub RG
resource "azurerm_resource_group" "rg" {
  name     = "resource-name-01"
  location = "eastus"
}

####################### Database setup ################################
resource "azurerm_storage_account" "st" {
  name                     = "irs01st"
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

#resource "azurerm_sql_server" "ss" {
#  name                         = "irs01-ss"
#  resource_group_name          = azurerm_resource_group.rg.name
#  location                     = azurerm_resource_group.rg.location
#  version                      = "12.0"
#  administrator_login          = "admin"
#  administrator_login_password = "Abc@123"
#}

####################### Start Sql server #############################
resource "azurerm_mssql_database" "sqldb" {
  name      = "db01"
  server_id = azurerm_sql_server.ss.id
  #  resource_group_name = azurerm_resource_group.rg.name
  #  storage_account_name  = azurerm_storage_account.st.name
  collation    = "SQL_Latin1_General_CP1_CI_AS"
  license_type = "LicenseIncluded"
  max_size_gb  = 1
  #read_scale   = true
  sku_name     = "Basic"
  #zone_redundant = true #this is required sku > basic
}

################################# OR ##################################
resource "azurerm_sql_database" "sqldb" {
  name                             = "sql-db"
  resource_group_name              = "${azurerm_resource_group.rg.name}"
  location                         = "${azurerm_resource_group.rg.location}"
  server_name                      = "${azurerm_sql_server.ss.name}"
  edition                          = "Basic"
  collation                        = "SQL_Latin1_General_CP1_CI_AS"
  create_mode                      = "Default"
  requested_service_objective_name = "Basic"
}

########################## End sql server ############################```
 
Thanks in advance

【问题讨论】:

  • 为什么要通过 Azure Cloud Shell 中的 C# 应用程序运行 terrafrom?
  • 我想通过我的应用程序访问 azure 并使用 terraform 创建一个单独的数据库,我知道我们可以使用浏览器直接打开一个 azure cloud shell。
  • 是的,我从代码中理解了你的地形。我的问题是关于您对运行 terraform 的偏好。为什么不使用 Azure Devops 中的管道并将状态文件放入存储帐户。
  • 因为我的客户需要。

标签: c# azure-devops terraform terraform-provider-azure azure-cloud-shell


【解决方案1】:

你不能。云外壳并非设计用于脚本。如果可能的话,您将需要编写复杂的浏览器自动化来驱动 UI。这不是一个合理的要求,您需要重新考虑您提出的解决方案。

您的意思是使用 Azure CLI? 如果是,请编辑问题以澄清这一点。云 shell 是 CLI 的多个环境之一,但 CLI 不是云 shell。

【讨论】:

  • Azure 云外壳不是 CLI
  • 我有一个使用 terraform cloud 的替代解决方案,并直接触发(使用 .net core)terraform cloud,然后使用 azure credential terraform call azure 并创建资源组、数据库等。
猜你喜欢
  • 1970-01-01
  • 2023-02-15
  • 2020-06-12
  • 1970-01-01
  • 2014-06-03
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多