Crear VM-Apache2-con Terraform en Azure[ES]

15435 ๋‹จ์–ด terraformapacheazure
ํ‘ธ๋ฅธ ์šฐ์‚ฌ์ œ์„ธ์ฃผ์™€ ์œก์ง€์—์„œ ๊ตญ๊ฐ€์ „๋ ฅํšŒ์‚ฌ์˜ ์ „๋ ฅ์‹œ์Šคํ…œ๊ณผ ์ธํ”„๋ผ๋Š” ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ๊ตฌ๋ฆ„์ด๋‹ค.
์ง€ํ˜•
์ด๊ฒƒ์€ ํ”Œ๋žซํผ ๊ฐœํ˜์œผ๋กœ ๋Œ€์ค‘์—๊ฒŒ ์ธํ”„๋ผ ์‹œ์„ค ๊ฑด์„ค ๊ณผ์ •์˜ ์ž๋™ํ™”๋ฅผ ์‹คํ˜„ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์˜€๋‹ค
Terraform trabaja en torno a estos archivos*.tf*.tfvars y*.tfstate
์•„์น˜๋ณดtf:
์ด๊ณณ์€ ๋ˆ„๋ฒ ์„ฑ์˜ ํ•œ ๊ณณ์ด๋‹ค.์ด๊ฒƒ์€ ๋ณต์žกํ•œ ๊ณผ์ •์ด๋‹ค.
์•„์น˜๋ณดTFVAR:
์ด๊ฒƒ์€ ์•„์ฃผ ์ข‹์€ ์˜ˆ๋‹ค.tf
์•„์น˜๋ณดtfstate:
์ด๊ฒƒ์€ ์ง€ํ˜•์˜ ์ผ์ข…์ด๋‹ค.๋ฏธ๋ž˜์˜ ํ† ์ง€ ๊ด€๋ฆฌ์™€ ๊ฐœ๋ฐœ ๊ณผ์ •์—์„œ ์šฐ๋ฆฌ๋Š” ํ† ์ง€์— ๋Œ€ํ•ด ์žฌ๊ณ„ํš์„ ํ•ด์•ผ ํ•œ๋‹ค.
Ejempo introductorio crear un recurso:
์žฌ๊ท€์  ์šด์˜ ์ฒด์ œ:
- ํ‘ธ๋ฅธ ์ฟคํƒ€

1. Instalar Terraform


Esta herramienta Esta Disposible para Windowsใ€Macใ€Linux

2. Instalar el CLI de Azure


Microsoft ์„ค์น˜ ์†Œํ”„ํŠธ์›จ์–ด(Microsoft Installer)์˜ ๊ณต์‹ ๋ฌธ์„œ ์ ์šฉ

Autenticarse en CLI de Azure


์šฐ์‚ฌ๋ฅด ์ฝ”๋งŒ๋„az login๊ตฌ๋ฆ„์ธต์˜ ํ™œ๋™์—์„œ ์šฐ๋ฆฌ๋Š” ๋” ๋งŽ์€ ์‹ ๋ขฐ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค

Crear un directorio


"ํ‘ธ๋ฅธ ์ง€ํ˜•์„ ๋ฐฐ์šด๋‹ค"๋Š” ์—์Šคํ…Œ ์นด์†Œ์˜ ์ด์‚ฌ๋กœ HCL(Hashi Corporation Archivo estarรกel cรณdigo escrito)์—์„œ main.tf์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "myTFResourceGroup"
  location = "westus2"
}

์„ฑ๋ช…์˜ ์ค‘์š”ํ•œ ๋‚ด์šฉ์€ provider๋…„ ใ€Š๋ฏธ๊ตญ ๊ตญ๊ฐ€์ธ๊ถŒ์„ ์–ธใ€‹๊ณผ resource๋…„ ใ€Š์ ˆ๋ง์ง€์—ญ ์žฌ๋ฐœ์„ฑ ์งˆ๋ณ‘์„ ์–ธใ€‹์„ ํฌํ•จํ•œ๋‹ค.

Inicializar terraform


๊ณต์‚ฌ ์„ค๊ณ„ ๋ณธ์‚ฌ(Dentro del proyecto ejecutar el comando) terraform init ๊ฑด์ถ• ๊ตฌ์กฐ๋„

Generar el plan de ejecuciรณn


์œ ๋Ÿฝ ๊ณต๋™์ฒด terraform planํ˜ธ ๊ฒฐ์˜๋Š” ๊ฐ ํšŒ์›๊ตญ์˜ ์ด์ต์„ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•œ ๊ณ„ํš์„ ํ†ต๊ณผ์‹œ์ผฐ๋‹ค

Aplicar la configuracion


์šฐ๋ฆฌ๋Š” ๊ณ„ํš์„ ์ˆ˜์ •ํ•˜์˜€๋Š”๋ฐ, ์ˆ˜์ •๋œ ๊ณต์‚ฌ๋Š” terraform apply๋…„์— ์™„๊ณต๋˜๊ณ , ๊ณต์‚ฌ๋Š” yes๋…„์— ์™„๊ณต๋˜๋ฉฐ, ๊ณต์‚ฌ๋Š” terraform destroy๋…„์— ์™„๊ณต๋  ๊ฒƒ์ด๋‹ค

Inspeccionar el estado
Ver el grupo de recursos que se han creado mediante terraform
terraform show

Ver en la consola de la nube de Azure



Destruir el recurso


์šฐ์‚ฐ๋„ ์ฝ”๋งŒ๋„ yes๋ฒˆ ๋„๋กœ์— ์žˆ๋Š” ์ฒ ๋„์ž…๋‹ˆ๋‹ค. ์ด ๋„๋กœ๋Š” ๋„๋กœ๋กœ ๋„๋กœ๊ตํ†ต์•ˆ์ „์œ„์›ํšŒ๊ฐ€ ๊ด€๋ฆฌํ•˜๊ณ  terraform plan -out=newplan๋ฒˆ ๋„๋กœ์ž…๋‹ˆ๋‹ค.


Cambios en la infraestructura


์‹ ์‚ฌ๋ ˆ๋‹ค์™€ ๋ฃจ์—๊ณ  ์ง€์—ญ terraform apply newplan๋…„ ์‹ ์‚ฌ๋ ˆ๋‹ค์™€ ๋ฃจ์—๊ณ  ์ง€์—ญ์˜ ํ† ์ง€ ์ด์šฉ ๊ณ„ํš์—์„œ ์ด ์ง€์—ญ์˜ ํ† ์ง€ ์ด์šฉ ์ด์ฒด์  ๊ณ„ํš ์ค‘ ํ† ์ง€ ์ด์šฉ๋ฅ ์ด ๊ฐ€์žฅ ๋†’๋‹ค
์„ธ๊ณต๋‹ค ํŒŒํŠธ
*์•„ํŒŒ์น˜์˜ ์ธํ„ฐ๋„ท ์„œ๋น„์Šค์—์„œ ๊ฐ€์ƒ์˜ ์„ธ๊ณ„๊ฐ€ ํ‘ธ๋ฅธ ํ•˜๋Š˜์—์„œ ์šดํ–‰๋˜๊ณ  ์žˆ๋‹ค. ์ด๊ฒƒ์€ ๊ฐ€์ƒ์˜ ์„ธ๊ณ„์ด๋‹ค*

Crear un archivo variables.tf en que se declara el uso de variables en la creaciรณn de los recursos


variable "location" {}

variable "admin_username" {
  type        = string
  description = "Administrator user name for virtual machine"
}

variable "admin_password" {
  type        = string
  description = "Password must meet Azure complexity requirements"
}

variable "prefix" {
  type    = string
  default = "my"
}

variable "tags" {
  type = map

  default = {
    Environment = "Terraform GS"
    Dept        = "Engineering"
  }
}

variable "sku" {
  default = {
    westus2 = "16.04-LTS"
    eastus  = "18.04-LTS"
  }
}


Crear el archivo terraform.tfvars en donde se establecen los valores de las varibles


location       = "xxxxx"
prefix         = "xx"
admin_username = "xxxxx"
admin_password = "xxxxxxxx"

Crear un archivo azure-user-data.sh que tendrรก los script iniciales que se ejecutaran en la maquina virtual creada en Azure


#! /bin/bash
sudo apt-get update
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
echo "<h1>Demp Apache2 from Azure Virtual Machine</h1>" | sudo tee /var/www/html/index.html

Las siguientes configuraciones se realizaran en el archivo main.tf


Agregar una regla de red que habilite el puerto 80
    security_rule {
    name                       = "allow-http"
    description                = "allow-http"
    priority                   = 110
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefix      = "Internet"
    destination_address_prefix = "*"
  }
๋ธํŠธ๋กœ ๋ธ๋ ˆ์†Œ๋“œ ๋ ˆ์ด๋“œ
resource "azurerm_network_security_group" "nsg" {
  name                = "myTFNSG"
  location            = "westus2"
  resource_group_name = azurerm_resource_group.rg.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 = "*"
  }

    security_rule {
    name                       = "allow-http"
    description                = "allow-http"
    priority                   = 110
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefix      = "Internet"
    destination_address_prefix = "*"
  }
}

Declarar la data para leer el archivo de configuracion inicial tambien llamado cloud-init


data "template_file" "linux-vm-cloud-init" {
  template = file("azure-user-data.sh")
}

Cargar la data en sistema operativo de la maquina virtual, decodificando el archivo anteriormente declarado


  os_profile {
    computer_name  = "myTFVM-kg"
    admin_username = var.admin_username
    admin_password = var.admin_password
    custom_data = base64encode(data.template_file.linux-vm-cloud-init.rendered)
  }

Configuraciรณn completa del archivo main.tf


์ด๊ฒƒ์€ vm_size = "Standard_B1s"์ผ์˜ ๋ฌด๋ฃŒ ๊ณ„์•ฝ์ด๋‹ค

Declarar una salida por consola de la ip de la vm creada


output "public_ip_address" {
  value = data.azurerm_public_ip.ip.ip_address
}

main.tf


# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "myTFResourceGroup"
  location = "westus2"

  tags = {
      Environment = "Terraform Getting Started"
      Team = "DevOps"
  }
}

# Create a virtual network
resource "azurerm_virtual_network" "vnet" {
    name                = "myTFVnet"
    address_space       = ["10.0.0.0/16"]
    location            = "westus2"
    resource_group_name = azurerm_resource_group.rg.name
}

# Create subnet
resource "azurerm_subnet" "subnet" {
  name                 = "myTFSubnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create public IP
resource "azurerm_public_ip" "publicip" {
  name                = "myTFPublicIP"
  location            = "westus2"
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Static"
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "nsg" {
  name                = "myTFNSG"
  location            = "westus2"
  resource_group_name = azurerm_resource_group.rg.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 = "*"
  }

    security_rule {
    name                       = "allow-http"
    description                = "allow-http"
    priority                   = 110
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefix      = "Internet"
    destination_address_prefix = "*"
  }
}

# Create network interface
resource "azurerm_network_interface" "nic" {
  name                      = "myNIC"
  location                  = "westus2"
  resource_group_name       = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "myNICConfg"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = azurerm_public_ip.publicip.id
  }
}

data "template_file" "linux-vm-cloud-init" {
  template = file("azure-user-data.sh")
}

# Create a Linux virtual machine
resource "azurerm_virtual_machine" "vm" {
  name                  = "myTFVM"
  location              = "westus2"
  resource_group_name   = azurerm_resource_group.rg.name
  network_interface_ids = [azurerm_network_interface.nic.id]
  vm_size               = "Standard_B1s"


  storage_os_disk {
    name              = "myOsDisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = lookup(var.sku, var.location)
    version   = "latest"
  }

  os_profile {
    computer_name  = "myTFVM-kg"
    admin_username = var.admin_username
    admin_password = var.admin_password
    custom_data = base64encode(data.template_file.linux-vm-cloud-init.rendered)
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

data "azurerm_public_ip" "ip" {
  name                = azurerm_public_ip.publicip.name
  resource_group_name = azurerm_virtual_machine.vm.resource_group_name
  depends_on          = [azurerm_virtual_machine.vm]
}

output "public_ip_address" {
  value = data.azurerm_public_ip.ip.ip_address
}


*์•„์น˜์›Œ์Šค ํ˜ธํ…”*

์—์ œํฌํƒ€terraform planterraform apply - ๋„ค.


์ด๊ฒƒ์€ ๋„คํŠธ์›Œํฌ ์„œ๋น„์Šค ํ”Œ๋žซํผ์ด๋‹ค. ๊ทธ๊ฒƒ์€ ์†Œํ˜• ์„œ๋น„์Šค ํ”Œ๋žซํผ์ด๋‹ค๐Ÿ‘๐Ÿผ๐Ÿ‘๐Ÿผterraform destroy์ด๊ฒƒ์€ ๋งค์šฐ ์ข‹์€ ์˜ˆ์ด๋‹ค
๋ถ„๋‹น ์„œ๋น„์Šค ์‚ฌ์ดํŠธ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ, ๊ทธ๊ฒƒ์€ ์ž‘์€ ์‹๋‹น์ด๋‹ค

Almacenar el estado remoto en Terraform Cloud


๋ฐฑ์—”๋“œ์—์„œ ์ง€ํ˜• ํ—ˆ๊ฐ€์ฆ๊ณผ ์ง€ํ˜• ์ด์šฉ๋ฅ ์„ ์ œ๊ฑฐํ•˜๊ณ estado ๋ฐ์ดํ„ฐ์™€ ๋น„๊ตํ–ˆ๋‹ค.์ธํ”„๋ผ ์‹œ์„ค์„ ๊ฑด์„คํ•˜๋Š” ์นธ๋น„์˜ค์Šค์—์„œ ์šฐ๋ฆฌ๋Š” ์ง€ํ˜•์šด์— ๋Œ€ํ•ด ๋ฐฑ์—”๋“œ ๊ฐœ์กฐ๋ฅผ ํ•ด์„œ ์„ค๋น„์˜ ๋ฒ„์ „ํ™”, ๊ฐ์‚ฌ์™€ ํ˜‘๋ ฅ์„ ์‹คํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

Registrarse en Terraform Cloud

Configurar el backend remoto


terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }

+ backend "remote" {
+   organization = "<ORG_NAME>"
+   workspaces {
+     name = "Example-Workspace"
+   }
+ }
}

provider "azurerm" {
  features {}
}

Iniciar sesion en local terraform login

Ver la cuenta de azure iniciadoaz account list

Ver informacion necesaria de la cuenta de azureaz account set --subscription="SUBSCRIPTION_ID"

Copiar las ID en la cloud de terraform


az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/SUBSCRIPTION_ID"

Aplicar las configuraciones terraform apply



Destruir los recursos creados terraform destroy


์ง€ํ˜•์šด์€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ณ  ํ™•์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ํ˜•์šด์œผ๋กœ ๊ทธ ํ˜•์ƒ๊ณผ ํ˜•์ƒ์€ ์ง€ํ˜•๊ณผ ๋น„์Šทํ•˜๋‹ค.
์ง€ํ˜•์šด๋ณดํ—˜์ •์‚ฐ์‚ฌ๋Š” ํ˜„์ง€ ์ง€ํ˜•์˜ ์ƒํƒœ ํ™˜๊ฒฝ์— ๋ณดํ˜ธ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ ๊ธฐ๋Šฅ์€ ์ƒํƒœ ํ™˜๊ฒฝ์„ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์„ ํฌํ•จํ•œ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ