LoginSignup
10
3

More than 1 year has passed since last update.

Azure環境でTerraformを使ってみる(1/2)

Last updated at Posted at 2022-05-16

はじめに

初めてTerraformを使ってみたい方かつAzureで試してみたい方向けにやり方をまとめました。
この内容を実行することでIT初級者でもTerraformを使ったInfrastracture As Code(IaC)を体験することができます。
(所要時間30分くらい)
今回は利便性を体験することが目的なのでコード自体の解説は別の機会があればと思います。

概要

Terraform実行用の環境として専用環境を準備することもできるのですが、今回は試してみることが目的なので簡易的に動かしたいと思います。
AzureではCloud Shellを使えばTerraformを動かせるので、ローカル環境のノートパッドで作成したコードをAzure上のCloud Shellへコピーして動作させます。

構成図

最終的な構成図です。
二つのサブネットを含むVNETと3枚のNWインターフェースを持つVMを1台作成します

概要図.png

作業の流れ

  1. ネットワークを作成するコードを準備
  2. terraformの環境を準備
  3. terraformを実行
  4. VMを作成するコードを準備
  5. terraformを実行

手順

1.ネットワークを作成するコードを準備

  1. 最初にネットワークを作成するコードを準備します
    ローカルPCで作業してください
    以下のコードを丸ごとコピーしてローカルPCのメモ帳などで作成し保存します
    これがリソースグループ、VNET、サブネット、NSGなどを作成するコードです
network.tf
# Azureプロバイダーを構成する
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~>3.0.2"
    }
  }
}

# 機能を定義する
# 削除する時に楽なようにリソースグループにリソースが残っていても削除できるように設定しています
provider "azurerm" {
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
}

# リソースグループを作成する
resource "azurerm_resource_group" "ec_group" {
    name     = "education_env"
    location = "Japan East"
}

# VNETを作成する
resource "azurerm_virtual_network" "ec_network" {
    name                = "EducationNetwork"
    address_space       = ["10.10.0.0/16"]
    location            = "Japan East"
    resource_group_name = "${azurerm_resource_group.ec_group.name}"
}

# VNETに含むサブネット1を作成する
resource "azurerm_subnet" "ec_FRsubnet" {
    name                 = "FrontSubnet"
    resource_group_name  = "${azurerm_resource_group.ec_group.name}"
    virtual_network_name = "${azurerm_virtual_network.ec_network.name}"
    address_prefixes     = ["10.10.1.0/24"]
}

# VNETに含むサブネット2を作成する
resource "azurerm_subnet" "ec_BKsubnet" {
    name                 = "BackSubnet"
    resource_group_name  = "${azurerm_resource_group.ec_group.name}"
    virtual_network_name = "${azurerm_virtual_network.ec_network.name}"
    address_prefixes     = ["10.10.100.0/24"]
}

# Linux用のNSGを作成する
resource "azurerm_network_security_group" "eclinux_nsg" {
    name                = "SSHNetworkSecurityGroup"
    location            = "Japan East"
    resource_group_name = "${azurerm_resource_group.ec_group.name}"
    
    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }
}

2 Terraformの環境を準備

  1. Azure Portalにログインして右上のコンソールアイコンを選択しCloud Shellを起動
    00.PNG

 2. 以下の画面が出る場合はBashをクリック
(PowerShellでもIaCは体験できますが、今回はTerraformがテーマなのでBashを選択します)
cloudshell02.PNG

 3. 初めて作成する場合は以下の画面になると思います。詳細設定の表示をクリック
cloudshell03.PNG

 4. リソースグループ名、ストレージアカウント名、共有ファイル名を入力してストレージの作成をクリック
ストレージアカウント名はAzure内で一意である必要があるので誰とも重複しない名前にして下さい。
 (他の人の命名と重複してはいけないこの制限はかなり不便だと思います)
  リソースグループ名はnetwork.tfの中でeducation_envを使っているのでそれ以外の名前(以下のサンプルではeducation)にして下さい

参考URL:ストレージ アカウントの概要

cloudshell05.PNG

 5. これでAzure Cloud Shellが利用できるようになりました。以下の画面が出ていると思います。

Requesting a Cloud Shell.Succeeded. 
Connecting terminal...

Welcome to Azure Cloud Shell

Type "az" to use Azure CLI
Type "help" to learn about Cloud Shell

nosecape@Azure:~$ 

  

3.terraformを実行

  1. lsを実行してデフォルトのファイル状況を確認する
nosecape@Azure:~$ ls
clouddrive

 2. ローカルに作成したnetwork.tfファイルをコンソールにドラック&ドロップする
 lsを実行してファイルがアップロードされたことを確認する

nosecape@Azure:~$ ls
clouddrive  network.tf

 3. 今回はIaCの体験なのでterraformのコマンドはおまじないだと思い以下のコマンドを実行する

 terraform initを実行する(初期化しています)

nosecape@Azure:~$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "~> 3.0.2"...

                    ・
                    ・
               <省略>
                    ・
                    ・
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
nosecape@Azure:~$ 

 4. terraform planを実行します(リソースの確認を行っています)

nosecape@Azure:~$ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_network_security_group.eclinux_nsg will be created
  + resource "azurerm_network_security_group" "eclinux_nsg" {
      + id                  = (known after apply)
      + location            = "japaneast"
      + name                = "SSHNetworkSecurityGroup"
      + resource_group_name = "education_env"
      + security_rule       = [
          + {
              + access                                     = "Allow"
              + description                                = ""
              + destination_address_prefix                 = "*"
    ![09.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2516565/db1164ea-378d-d9a7-8011-fb984e529989.png)
                ・
                    ・
               <省略>
                    ・
                    ・
  # azurerm_virtual_network.ec_network will be created
  + resource "azurerm_virtual_network" "ec_network" {
      + address_space       = [
          + "10.10.0.0/16",
        ]
      + dns_servers         = (known after apply)
      + guid                = (known after apply)
      + id                  = (known after apply)
      + location            = "japaneast"
      + name                = "EducationNetwork"
      + resource_group_name = "education_env"
      + subnet              = (known after apply)
    }

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

 5. terraform applyを実行します(リソースの作成を行います)

nosecape@Azure:~$ terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_network_security_group.eclinux_nsg will be created
  + resource "azurerm_network_security_group" "eclinux_nsg" {
      + id                  = (known after apply)
      + location            = "japaneast"
      + name                = "SSHNetworkSecurityGroup"
      + resource_group_name = "education_env"

                    ・
                    ・
               <省略>
                    ・
                    ・
  # azurerm_virtual_network.ec_network will be created
  + resource "azurerm_virtual_network" "ec_network" {
      + address_space       = [
          + "10.10.0.0/16",
        ]
      + dns_servers         = (known after apply)
      + guid                = (known after apply)
      + id                  = (known after apply)
      + location            = "japaneast"
      + name                = "EducationNetwork"
      + resource_group_name = "education_env"
      + subnet              = (known after apply)
    }

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

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

 6. Enter a value と出てくるのでyesと入力します

  Enter a value: yes

azurerm_resource_group.ec_group: Creating...
azurerm_resource_group.ec_group: Creation complete after 1s [id=/subscriptions/xxxxxxx]
azurerm_network_security_group.eclinux_nsg: Creating...
                    ・
                    ・
               <省略>
                    ・
                    ・
azurerm_subnet.ec_FRsubnet: Creation complete after 9s [id=/subscriptions/xxxxxx/FrontSubnet]

Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
nosecape@Azure:~$ 

 これでリソースが出来上がりました。
 数分で出来ると思いますがAzure状態によっては時間がかかるようです。

 7. 確認していきましょう。
 リソースグループ「education_env」を作成しているので確認してみます
 VNETとNSGが作成されています
09.PNG

 8. VNETの中にサブネットもできています
10.PNG

ここまでの手順で以下の構成までできました。続きは こちら
概要図NW.drawio.png

10
3
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
10
3