LoginSignup
1
0

More than 1 year has passed since last update.

【Terraform de Azure】 Storage Account を作成してみました

Last updated at Posted at 2022-01-06

概要

「Infrastructure as Code」ということで、Terraform を用いて Azure環境上に ストレージアカウント を作成し、コンテナを3つ構築してみます。

ローカル環境

  • macOS Monterey 12.0.1
  • Azure CLI 2.28.0
  • terraform v1.0.11

前提条件

  1. Azure環境がすでに用意されていること(テナント/サブスクリプション)
  2. ローカル環境に「azure cli」がインストールされていること
  3. ローカル環境に「terraform」環境が構成されていること
  4. TerraformでAzure上に環境構築するためのサービスプリンシパルが作成されており、Terraform のためのローカル環境変数が定義されていること

Terraform で ストレージアカウント を作成してみる

terraform 定義ファイルの作成

プロバイダの定義

main.tf
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
}

パラメータ定義ファイル

variables.tf
# 環境変数(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 {}                      // 利用リージョン

パラメータ値定義ファイル

terraform.tfvars
# 環境変数の定義(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"             // 利用リージョン

ストレージアカウント定義ファイル

Storage.tf
# リソースグループ
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

storage 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 Storage Account と コンテナ環境を構成できました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0