如何从AzureRm Terraform提供程序创建应用程序/实用程序/Web应用程序?

bqujaahr  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(101)

我不知道“GO”,但我已经做了30多年的开发人员/工程师,所以我不应该有任何麻烦弄清楚…我正在寻找的是一个开始/例子/教程等。我如何使用terraform azurerm提供商存储库(https://github.com/hashicorp/terraform-provider-azurerm)的源代码,构建一个独立的应用程序/实用程序,然后运行它。
确切地说,我想知道的是,为什么他们的代码在“terraform计划”中阅读远程状态时返回特定的错误消息。我的想法是,为什么不把他们的代码“剥离”到那个特定的函数(以及任何“必需的”函数),然后运行它呢?
(我想运行resourceStorageAccountRead函数,该函数位于“internal\services\storage\storage_account_resource.go”中。)
任何建议都非常感谢!

flvtvl50

flvtvl501#

为了运行golang代码,请从here下载适用于您的操作系统的Golang。* 在VS代码中创建一个文件夹,并创建一个名为main.go的文件,右键单击该文件>使用集成终端打开,然后运行下面的命令 *:-

go mod init github.com/sid24desai/hello
OR
go mod init hello

go.mod文件将像下面这样创建:-

module hello

go 1.20

require (
    github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
    github.com/Azure/go-autorest v14.2.0+incompatible // indirect
    github.com/Azure/go-autorest/autorest v0.11.29 // indirect
    github.com/Azure/go-autorest/autorest/adal v0.9.23 // indirect
    github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
    github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
    github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
    github.com/Azure/go-autorest/logger v0.2.1 // indirect
    github.com/Azure/go-autorest/tracing v0.6.0 // indirect
    github.com/agext/levenshtein v1.2.2 // indirect
    github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
    github.com/fatih/color v1.13.0 // indirect
    github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
    github.com/golang/protobuf v1.5.2 // indirect
    github.com/google/go-cmp v0.5.9 // indirect
    github.com/hashicorp/errwrap v1.1.0 // indirect
    github.com/hashicorp/go-azure-helpers v0.57.0 // indirect
    github.com/hashicorp/go-azure-sdk v0.20230626.1153114 // indirect
    github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
    github.com/hashicorp/go-hclog v1.4.0 // indirect
    github.com/hashicorp/go-multierror v1.1.1 // indirect
    github.com/hashicorp/go-uuid v1.0.3 // indirect
    github.com/hashicorp/go-version v1.6.0 // indirect
    github.com/hashicorp/hcl/v2 v2.16.2 // indirect
    github.com/hashicorp/logutils v1.0.0 // indirect
    github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
    github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect
    github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 // indirect
    github.com/mattn/go-colorable v0.1.12 // indirect
    github.com/mattn/go-isatty v0.0.14 // indirect
    github.com/mitchellh/copystructure v1.2.0 // indirect
    github.com/mitchellh/go-testing-interface v1.14.1 // indirect
    github.com/mitchellh/go-wordwrap v1.0.0 // indirect
    github.com/mitchellh/mapstructure v1.5.0 // indirect
    github.com/mitchellh/reflectwalk v1.0.2 // indirect
    github.com/tombuildsstuff/giovanni v0.20.0 // indirect
    github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
    github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
    github.com/vmihailenco/tagparser v0.1.1 // indirect
    github.com/zclconf/go-cty v1.13.1 // indirect
    golang.org/x/crypto v0.9.0 // indirect
    golang.org/x/net v0.10.0 // indirect
    golang.org/x/sys v0.8.0 // indirect
    golang.org/x/text v0.9.0 // indirect
    google.golang.org/appengine v1.6.7 // indirect
    google.golang.org/protobuf v1.28.1 // indirect
)

现在,复制你的storage_account_resource.go代码到main.go文件中,并通过运行命令go get module导入所有github模块,参考下面:
如果你在VS Code中安装了Go扩展,你可以将鼠标悬停在github模块上,并使用快速修复功能,如下所示:

或者运行下面的代码来安装各个软件包

go get github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage

在安装完所有的软件包后,你可以用你的存储帐户改变所需的变量,然后运行下面的命令来运行golang文件:-

go run main.go

这将运行您的Go文件。
您可以参考代码通过Terraform从here部署存储帐户,参考以下步骤:-
我的main.tf:-

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

# Configure the Microsoft Azure Provider
provider "azurerm" {
  
subscription_id = "xxxxe97cb2a7"
tenant_id = "xxxed-af9038592395"
client_id = "xxxx38-6d26a31435cb"
client_secret = "xxxxx-CS0ifbLE"
features {
  resource_group {
    prevent_deletion_if_contains_resources = false
  }
}
}
resource "azurerm_resource_group" "example" {
  name     = "siliconrg65"
  location = "West Europe"
}

resource "azurerm_storage_account" "example" {
  name                     = "versiliconstrg54"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"

  tags = {
    environment = "staging"
  }
}

在VS代码终端中运行以下命令以应用terraform代码:

terraform init, 
terraform plan -out main.tfplan  
terraform apply main.tfplan

输出:-

相关问题