Crear VM-Apache2-con Terraform en Azure[ES]
์งํ
์ด๊ฒ์ ํ๋ซํผ ๊ฐํ์ผ๋ก ๋์ค์๊ฒ ์ธํ๋ผ ์์ค ๊ฑด์ค ๊ณผ์ ์ ์๋ํ๋ฅผ ์คํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ์๋ค
Terraform trabaja en torno a estos archivos*.tf*.tfvars y*.tfstate
์์น๋ณดtf:
์ด๊ณณ์ ๋๋ฒ ์ฑ์ ํ ๊ณณ์ด๋ค.์ด๊ฒ์ ๋ณต์กํ ๊ณผ์ ์ด๋ค.
์์น๋ณดTFVAR:
์ด๊ฒ์ ์์ฃผ ์ข์ ์๋ค.tf
์์น๋ณดtfstate:
์ด๊ฒ์ ์งํ์ ์ผ์ข ์ด๋ค.๋ฏธ๋์ ํ ์ง ๊ด๋ฆฌ์ ๊ฐ๋ฐ ๊ณผ์ ์์ ์ฐ๋ฆฌ๋ ํ ์ง์ ๋ํด ์ฌ๊ณํ์ ํด์ผ ํ๋ค.
Ejempo introductorio crear un recurso:
์ฌ๊ท์ ์ด์ ์ฒด์ :
- ํธ๋ฅธ ์ฟคํ
Esta herramienta Esta Disposible para WindowsใMacใLinux
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 llamadocloud-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 plan
terraform 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 iniciado
az account list
Ver informacion necesaria de la cuenta de azure
az 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
์งํ์ด์ ๋น๊ตํ ์ ์๊ณ ํ์ ํ ์ ์๋ ์งํ์ด์ผ๋ก ๊ทธ ํ์๊ณผ ํ์์ ์งํ๊ณผ ๋น์ทํ๋ค.
์งํ์ด๋ณดํ์ ์ฐ์ฌ๋ ํ์ง ์งํ์ ์ํ ํ๊ฒฝ์ ๋ณดํธ๋ฅผ ์ ๊ณตํ ์ ์๋๋ฐ ๊ทธ ๊ธฐ๋ฅ์ ์ํ ํ๊ฒฝ์ ๋ณดํธํ๋ ๊ฒ์ ํฌํจํ๋ค.
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(Crear VM-Apache2-con Terraform en Azure[ES]), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://dev.to/kevingo710/crear-vm-apache2-con-terraform-en-azure-es-7e5ํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค