Infrastructure as code Part 2

🌻TruthfulΒ·2023λ…„ 9μ›” 15일

πŸ“Œ ν•™μŠ΅λͺ©ν‘œ

1. ARM ν…œν”Œλ¦Ώμ˜ μ‚¬μš© 이해

2. ARM ν…œν”Œλ¦Ώμ— λŒ€ν•œ μ‹€μ œ κ²½ν—˜

3. bicep의 μ‚¬μš© 이해

ARM ν…œν”Œλ¦Ώμ˜ μ‚¬μš© 이해

Azure의 μžμ›μ„ μ •μ˜ν•˜κ³  κ΅¬μ„±ν•˜λŠ” 선언적 방식을 μ œκ³΅ν•˜λŠ” JSON νŒŒμΌμž…λ‹ˆλ‹€.

ARM ν…œν”Œλ¦Ώμ„ μ‚¬μš©ν•˜λ©΄ μ—¬λŸ¬ Azure μžμ›μ˜ 배포와 ꡬ성을 μžλ™ν™”ν•˜κ³  일관성을 μœ μ§€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ARM ν…œν”Œλ¦Ώμ€ Azure μžμ›μ˜ 배포와 ꡬ성을 μ •ν™•ν•˜κ³  일관성 있게 μžλ™ν™”ν•˜λŠ” "맀뉴얼"둜 λ³Ό 수 있으며, 없을 경우 μˆ˜μž‘μ—…μœΌλ‘œ 반볡적인 μž‘μ—…μ„ ν•΄μ•Ό ν•˜κ³  μ‹€μˆ˜μ˜ κ°€λŠ₯성도 λ†’μ•„μ§‘λ‹ˆλ‹€.

첫번째 ν…œν”Œλ¦Ώ λ§Œλ“€κΈ°

VSCdoeμ—μ„œ azuredeploy.json νŒŒμΌμ„ λ§Œλ“­λ‹ˆλ‹€.

ν•˜λ‹¨μ˜ λ‚΄μš©μ„ json νŒŒμΌμ— λΆ™μ—¬λ„£κΈ° ν•©λ‹ˆλ‹€.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": []
}

$schema: 이 ν•„λ“œλŠ” μ‚¬μš©λ˜λŠ” μŠ€ν‚€λ§ˆμ˜ URIλ₯Ό μ •μ˜ν•©λ‹ˆλ‹€. 이것은 ARM ν…œν”Œλ¦Ώμ˜ 버전 및 μœ νš¨μ„±μ„ ν™•μΈν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. 이 경우, 2019-04-01 λ²„μ „μ˜ ARM ν…œν”Œλ¦Ώ μŠ€ν‚€λ§ˆλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

contentVersion: ν…œν”Œλ¦Ώμ˜ 버전을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 이 ν•„λ“œλŠ” μ‚¬μš©μžκ°€ μ •μ˜ν•  수 있으며, 배포 내역을 μΆ”μ ν•˜κ±°λ‚˜ λ‹€μ–‘ν•œ ν…œν”Œλ¦Ώ 버전을 κ΄€λ¦¬ν•˜λŠ” 데 μ‚¬μš©λ  수 μžˆμŠ΅λ‹ˆλ‹€.

resources: 이 배열은 ν…œν”Œλ¦Ώμ—μ„œ 배포할 Azure μžμ›λ“€μ„ μ •μ˜ν•©λ‹ˆλ‹€. 이 μ˜ˆμ œμ—μ„œλŠ” μ•„λ¬΄λŸ° μžμ›λ„ μ •μ˜λ˜μ–΄ μžˆμ§€ μ•ŠμœΌλ―€λ‘œ, 빈 λ°°μ—΄([])둜 ν‘œμ‹œλ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

Azure에 λ‘œκ·ΈμΈμ„ ν•©λ‹ˆλ‹€.

az login

λ§Œμ•½μ— ꡬ독 계정이 μ—¬λŸ¬κ°œ μžˆλ‹€λ©΄ ꡬ독 계정이름 λ˜λŠ” 아이디λ₯Ό SubscriptionName λŒ€μ‹ μ— μž…λ ₯ν•©λ‹ˆλ‹€.

az account set --subscription SubscriptionName

Resource group λ§Œλ“€κΈ°

λ¦¬μ†ŒμŠ€ 그룹을 ν•˜λ‹¨μ˜ μ½”λ“œμ²˜λŸΌ λ§Œλ“­λ‹ˆλ‹€.

az group create \
  --name myResourceGroup \
  --location 'Central US'

az group create: azλŠ” Azure CLI의 κΈ°λ³Έ λͺ…령어이며, group createλŠ” λ¦¬μ†ŒμŠ€ 그룹을 μƒμ„±ν•˜λŠ” μž‘μ—…μ„ μ§€μ‹œν•©λ‹ˆλ‹€.

--name myResourceGroup: 생성할 λ¦¬μ†ŒμŠ€ 그룹의 이름을 μ§€μ •ν•˜λŠ”λ° μ‚¬μš©λ©λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ”"myResourceGroup"μ΄λΌλŠ” μ΄λ¦„μ˜ λ¦¬μ†ŒμŠ€ 그룹을 μƒμ„±ν•˜λ„λ‘ μ§€μ •λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

--location 'Central US': λ¦¬μ†ŒμŠ€ 그룹을 ν˜ΈμŠ€νŒ…ν•  Azure 리전(데이터 μ„Όν„°μ˜ μœ„μΉ˜)을 μ§€μ •ν•©λ‹ˆλ‹€.

ν…œν”Œλ¦Ώ λ°°ν¬ν•˜κΈ°

μ•„λž˜ μ½”λ“œμ— {}λΆ€λΆ„κΉŒμ§€ μ œκ±°ν•˜κ³  파일 경둜λ₯Ό λ„£μŠ΅λ‹ˆλ‹€.

(파일 κ²½λ‘œλŠ” vscodeμ—μ„œ νŒŒμΌμ„ 우 ν΄λ¦­ν•˜λ©΄ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.)

templateFile="{provide-the-path-to-the-template-file}"
az deployment group create \
  --name blanktemplate \
  --resource-group myResourceGroup \
  --template-file $templateFile

μ•„λž˜μ˜ μ½”λ“œλ₯Ό 확인할 수 있으면 배포에 μ„±κ³΅ν•œ κ²ƒμž…λ‹ˆλ‹€.

"provisioningState": "Succeeded",

배포 성곡 이미지

1 succeededλ₯Ό ν΄λ¦­ν•©λ‹ˆλ‹€.

ARM template에 λ¦¬μ†ŒμŠ€ μΆ”κ°€ν•˜κΈ°

{provide-unique-name}에 고유의 μŠ€ν† λ¦¬μ§€ 계정 이름을 λ§Œλ“€μ–΄μ€λ‹ˆλ‹€. μ €λŠ” μ˜ˆμ‹œμ²˜λŸΌ κ·Έλƒ₯ store1abc06132022μ—μ„œ μ‚΄μ§λ§Œ λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "{provide-unique-name}",
      "location": "eastus",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

resources: λ°°ν¬ν•˜λ €λŠ” Azure λ¦¬μ†ŒμŠ€λ₯Ό μ •μ˜ν•˜λŠ” λ°°μ—΄μž…λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” 단 ν•˜λ‚˜μ˜ λ¦¬μ†ŒμŠ€, 즉 μŠ€ν† λ¦¬μ§€ 계정이 μ •μ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

type: "Microsoft.Storage/storageAccounts": λ¦¬μ†ŒμŠ€μ˜ μœ ν˜•μ„ μ§€μ •ν•©λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” μŠ€ν† λ¦¬μ§€ 계정을 μ˜λ―Έν•©λ‹ˆλ‹€.

apiVersion: "2021-09-01": λ¦¬μ†ŒμŠ€λ₯Ό μƒμ„±ν•˜λŠ” 데 μ‚¬μš©ν•  Azure API의 버전을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

name: "{provide-unique-name}": 생성될 μŠ€ν† λ¦¬μ§€ κ³„μ •μ˜ μ΄λ¦„μž…λ‹ˆλ‹€. μ‚¬μš©μžλŠ” 여기에 κ³ μœ ν•œ 이름을 μ œκ³΅ν•΄μ•Ό ν•©λ‹ˆλ‹€.

location: "eastus": μŠ€ν† λ¦¬μ§€ 계정이 생성될 Azure 리전을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” "eastus" (λ―Έκ΅­ 동뢀) 리전에 μƒμ„±λ©λ‹ˆλ‹€.

sku: { "name": "Standard_LRS" }: μŠ€ν† λ¦¬μ§€ κ³„μ •μ˜ μœ ν˜• 및 볡제 κΈ°λŠ₯을 μ§€μ •ν•©λ‹ˆλ‹€. Standard_LRSλŠ” ν‘œμ€€ μ„±λŠ₯의 둜컬 쀑볡 μŠ€ν† λ¦¬μ§€λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

kind: "StorageV2": μŠ€ν† λ¦¬μ§€ κ³„μ •μ˜ μ’…λ₯˜λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. StorageV2λŠ” 일반 μŠ€ν† λ¦¬μ§€ κ³„μ •μ˜ μ΅œμ‹  λ²„μ „μž…λ‹ˆλ‹€.

properties: { "supportsHttpsTrafficOnly": true }: μΆ”κ°€ 속성을 μ§€μ •ν•©λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” μŠ€ν† λ¦¬μ§€ κ³„μ •μ—μ„œ HTTPS νŠΈλž˜ν”½λ§Œ μ§€μ›ν•˜λ„λ‘ μ„€μ •λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

ν…œν”Œλ¦Ώ λ°°ν¬ν•˜κΈ°

az deployment group create \
  --name addstorage \
  --resource-group myResourceGroup \
  --template-file $templateFile

이제 ν¬ν„Έμ—μ„œ myresourcegroup에 ν΄λ¦­ν•˜λ©΄ μŠ€ν† λ¦¬μ§€κ°€ μΆ”κ°€λœ 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

ARM template에 parameters μΆ”κ°€ν•˜κΈ°

기쑴의 ν…œν”Œλ¦Ώμ— 문제점이 ν•œ κ°€μ§€ μžˆμŠ΅λ‹ˆλ‹€.

그것은 name이 λ„ˆλ¬΄ ν•˜λ“œμ½”λ”©μ΄λΌλŠ” μ μž…λ‹ˆλ‹€. 이 뢀뢄을 μˆ˜μ •ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

μ•„λž˜μ˜ μ½”λ“œμ²˜λŸΌ parametersλ₯Ό μΆ”κ°€ν•©μ‹œλ‹€.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[parameters('storageName')]",
      "location": "eastus",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

ν…œν”Œλ¦Ώ λ°°ν¬ν•˜κΈ°

az deployment group create \
  --name addnameparameter \
  --resource-group myResourceGroup \
  --template-file $templateFile \
  --parameters storageName={your-unique-name}

μ•„κΉŒ λ§Œλ“€μ–΄λ‘μ—ˆλ˜ storageName을 λ„£μŠ΅λ‹ˆλ‹€.
(λΈŒλΌμΌ“ 없이 ""없이 λ„£μœΌμ‹œλ©΄ λ©λ‹ˆλ‹€)

profile
"μΈμƒμ˜ λͺ©μ μ€ 타인을 λ”°λ₯΄λŠ” 것이 μ•„λ‹ˆλΌ μžμ‹ μ„ μ°½μ‘°ν•˜λŠ” 것이닀."

0개의 λŒ“κΈ€