我尝试创建了一个Azure存储账户【Terraform de Azure】
总结
我們可以使用 Terraform 在 Azure 環境中創建存儲帳戶並構建三個容器,從而將基礎設施以程式碼的方式管理。
本地环境
-
- macOS Monterey 12.0.1
-
- Azure CLI 2.28.0
- terraform v1.0.11
前提条件 (Qian ti tiao jian)
-
- 以下条件需要满足:
-
- 1. Azure环境已经准备就绪(租户/订阅)
-
- 2. 在本地环境中安装了”azure cli”
-
- 3. 在本地环境中配置了”terraform”环境
- 4. 已创建了用于在Azure上进行基础架构搭建的服务主体,并定义了用于Terraform的本地环境变量。
我尝试使用Terraform创建存储账户。
创建terraform定义文件
互联网服务提供商的定义
terraform {
required_providers {
azurerm = "~> 2.33"
}
}
provider "azurerm" {
features {}
tenant_id = var.ARM_TENANT_ID
client_id = var.ARM_CLIENT_ID
client_secret = var.ARM_CLIENT_SECRET
}
参数定义文件
# 環境変数(Azureサービスプリンシパル)
variable ARM_TENANT_ID {}
variable ARM_CLIENT_ID {}
variable ARM_CLIENT_SECRET {}
# タグ情報
variable tags_def {
default = {
owner = "ituru"
period = "2022-03-31"
CostCenter = "PSG2"
Environment = "Demo"
Project = "DUP_IaC"
}
}
# 各種パラメータ
variable resource_group_name {} // リソースグループ名
variable storage_account_name {} // ストレージアカウント名
variable storage_container_name01 {} // コンテナ名_01
variable storage_container_name02 {} // コンテナ名_02
variable storage_container_name03 {} // コンテナ名_03
variable region {} // 利用リージョン
参数值定义文件
# 環境変数の定義(Azureサービスプリンシパル)
ARM_TENANT_ID = "zzzzzzzz-cccc-4645-5757-zzzzzzzzzzzz"
ARM_CLIENT_ID = "xxxxxxxx-xxxx-4444-9922-xxxxxxxxxxxx"
ARM_CLIENT_SECRET = "hogehogehogehogehogehogehogehogege"
# パラメータ値の定義
resource_group_name = "rg_ituru_storage01" // リソースグループ名
storage_account_name = "iturutestaccount" // ストレージアカウント名
storage_container_name01 = "bronze" // コンテナ名_01
storage_container_name02 = "silver" // コンテナ名_01
storage_container_name03 = "gold" // コンテナ名_01
region = "japaneast" // 利用リージョン
存储账户定义文件
# リソースグループ
resource "azurerm_resource_group" "this" {
name = var.resource_group_name
location = var.region
tags = var.tags_def
}
# Gen2 ストレージアカウント
resource "azurerm_storage_account" "this" {
name = var.storage_account_name
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
account_tier = "Standard"
account_replication_type = "LRS"
account_kind = "StorageV2"
is_hns_enabled = "true"
tags = var.tags_def
}
# ストレージコンテナ_Bronze
resource "azurerm_storage_container" "c01" {
name = var.storage_container_name01
storage_account_name = azurerm_storage_account.this.name
container_access_type = "private"
}
# ストレージコンテナ_Silver
resource "azurerm_storage_container" "c02" {
name = var.storage_container_name02
storage_account_name = azurerm_storage_account.this.name
container_access_type = "private"
}
# ストレージコンテナ_Gold
resource "azurerm_storage_container" "c03" {
name = var.storage_container_name03
storage_account_name = azurerm_storage_account.this.name
container_access_type = "private"
}
运行 Terraform
## init
$ terraform init
:
Terraform has been successfully initialized!
## plan
$ terraform plan
:
Plan: 5 to add, 0 to change, 0 to destroy.
## apply
$ terraform apply
:
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
当地工作目录的情况
.
├── .terraform
│ └── providers
│ └── registry.terraform.io
│ └── hashicorp
│ └── azurerm
│ └── 2.89.0
│ └── darwin_amd64
│ └── terraform-provider-azurerm_v2.89.0_x5
├── .terraform.lock.hcl
├── Storage.tf
├── main.tf
├── terraform.tfstate
├── terraform.tfvars
└── variables.tf
terraform执行后的配置确认
连接到Azure环境
## 使用するテナントへのログイン
$ az login --tenant <tenant_id>
## 使用サブスクリプションの定義
$ az account set --subscription <Subscription_name>
确认资源组
$ az group show --name <ResourceGroup_name>
{
"id": "/subscriptions/<subscription_id>/resourceGroups/rg_ituru_storage01",
"location": "japaneast",
"managedBy": null,
"name": "rg_ituru_storage01",
"properties": {
"provisioningState": "Succeeded"
},
"tags": {
"CostCenter": "PSG2",
"Environment": "Demo",
"Project": "DUP_IaC",
"owner": "ituru",
"period": "2022-03-31"
},
"type": "Microsoft.Resources/resourceGroups"
}
获取存储账户信息。
## ストレージアカウントの取得
$ az storage account show -n <StorageAccount_name>
## コンテナ情報の取得
$ az storage container list --account-name <StorageAccount_name> --output table
Name Lease Status Last Modified
------ -------------- -------------------------
blonze 2022-01-05T12:46:22+00:00
gold 2022-01-05T12:46:23+00:00
silver 2022-01-05T12:46:22+00:00
存储 Blob 的操作
## ファイルを storage blob に upload
az storage blob upload \
--account-name <StorageAccount_name> \
--container-name <Container_name> \
--name sample.tf \
--file "./main.tf"
## ファイルを storage blob 上で copy
az storage blob copy start \
--account-name <StorageAccount_name> \
--destination-blob sample2.tf \
--destination-container <Container_name> \
--source-blob sample.tf \
--source-container <Container_name>
## コンテナ内の blob 一覧を取得
az storage blob list \
--account-name <StorageAccount_name> \
--container-name <Container_name> -o table
## storage blob を delete
az storage blob delete \
--account-name <StorageAccount_name> \
--container-name <Container_name> \
--name sample2.tf
## ファイルを storage blob から download
az storage blob download \
--account-name <StorageAccount_name> \
--container-name <Container_name> \
--name sample.tf \
--file "./download.tf"
使用 terraform 进行创建的资源的删除
$ terraform destroy
总结
通过这样,我们可以使用Terraform 在Azure环境中轻松创建Gen2存储账户和容器环境。