gpt4 book ai didi

azure - Terraform with Azure - 如何创建存储帐户?

转载 作者:行者123 更新时间:2023-12-05 01:41:39 30 4
gpt4 key购买 nike

在与 Terraform 战斗了一天之后,我在这里呼求帮助。

Terraform v0.11.11
+ provider.azurerm v1.20.0

我正在尝试从头开始创建一个新的资源组和一个存储帐户。看起来可以在没有存储帐户的情况下创建资源组:

resource "azurerm_resource_group" "rg1" {
name = "myResourceGroup"
location = "West Europe"
}

资源组已创建,目前不存在存储帐户。所以此时我很高兴。我执行destroy并再次从头开始。

现在,在代码中,创建资源组后,我想创建一个存储帐户,因为稍后其他资源需要引用它。 azurerm_storage_account 需要的唯一引用是对资源组的引用。

有关 azurerm_storage_account 的信息 https://www.terraform.io/docs/providers/azurerm/d/storage_account.html

代码现在看起来像这样:

resource "azurerm_resource_group" "rg1" {
name = "myResourceGroup"
location = "West Europe"
}

data "azurerm_storage_account" "stacc1" {
name = "mystorageaccount"
resource_group_name = "${azurerm_resource_group.rg1.name}"
}

我运行plan命令并得到以下输出:

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
<= read (data resources)

Terraform will perform the following actions:

<= data.azurerm_storage_account.stacc1
id: <computed>
access_tier: <computed>
account_encryption_source: <computed>
account_kind: <computed>
account_replication_type: <computed>
account_tier: <computed>
custom_domain.#: <computed>
enable_blob_encryption: <computed>
enable_file_encryption: <computed>
enable_https_traffic_only: <computed>
location: <computed>
name: "mystorageaccount"
primary_access_key: <computed>
primary_blob_connection_string: <computed>
primary_blob_endpoint: <computed>
primary_connection_string: <computed>
primary_file_endpoint: <computed>
primary_location: <computed>
primary_queue_endpoint: <computed>
primary_table_endpoint: <computed>
resource_group_name: "myResourceGroup"
secondary_access_key: <computed>
secondary_blob_connection_string: <computed>
secondary_blob_endpoint: <computed>
secondary_connection_string: <computed>
secondary_location: <computed>
secondary_queue_endpoint: <computed>
secondary_table_endpoint: <computed>
tags.%: <computed>

+ azurerm_resource_group.rg1
id: <computed>
location: "westeurope"
name: "myResourceGroup"
tags.%: <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

它表示它将查找(而不是创建)资源data.azurerm_storage_account.stacc1,并且显然运行apply命令将失败并显示以下消息:

Error: Error applying plan:

1 error(s) occurred:

  • data.azurerm_storage_account.stacc1: data.azurerm_storage_account.stacc1: Error: Storage Account "mystorageaccount" (Resource Group "myResourceGroup") was not found

因为它没有找到提到的存储帐户。

所有这些引出了我的问题:“如何在 Azure 中使用 Terraform 创建存储帐户?”

最佳答案

您需要使用资源,而不是数据实体。对于所有资源来说都是如此。数据实体是获取资源数据,而不是创建资源数据。

resource "azurerm_resource_group" "testrg" {
name = "resourceGroupName"
location = "westus"
}

resource "azurerm_storage_account" "testsa" {
name = "storageaccountname"
resource_group_name = "${azurerm_resource_group.testrg.name}"
location = "westus"
account_tier = "Standard"
account_replication_type = "GRS"

tags {
environment = "staging"
}
}

https://www.terraform.io/docs/providers/azurerm/r/storage_account.html

关于azure - Terraform with Azure - 如何创建存储帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54099844/

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