Deploy a Container Instance in Azure

This article was translated from English: Does it need improvement?
Translated
View the article in English

Use Terraform on Azure to deploy the IronSecureDoc Docker container and make it available with a public IP address and FQDN.

Prerequisites

Step 1: Clone GitHub Repository Template

We have the GitHub Repository template ready for you to clone and get started immediately here:

https://github.com/iron-software/IronSecureDoc-Terraform/

Step 2: Modify All Resource Names

Modify All Resource Names in variables.tf

  • resource_group_name: Name of the resource group.
  • resource_group_location: Location of the resource group. You can find the region from REGIONS.md and use the second column to fill in this variable.
  • container_group_name: Name of the container group.
  • container_name: Name of the container.
  • image_tag: The tag of the image to deploy; it can be latest or any specific version number.
  • Other variables do not need to be changed.

Create secret.tfvars to Contain All Sensitive Data

Populate the following variables in secret.tfvars.

Azure Container Registry

Append to main.tf after dns_name_label = var.dns_name_label:

image_registry_credential {
  server   = var.registry_server
  username = var.registry_username
  password = var.registry_password
}

Add to variables.tf:

variable "registry_server" {
  type        = string
  sensitive   = false
  description = "The server for the container registry. Required if the image is stored in a private registry."
}

variable "registry_username" {
  type        = string
  sensitive   = true
  description = "Username for the container registry. Required if the image is stored in a private registry."
}

variable "registry_password" {
  type        = string
  sensitive   = true
  description = "Password for the container registry. Required if the image is stored in a private registry."
}

Add to secret.tfvars:

registry_server   = "<registry-name>.azurecr.io"
registry_username = "YOUR-REGISTRY-USERNAME"
registry_password = "YOUR-REGISTRY-PASSWORD"
license_key       = "YOUR-LICENSE-KEY"

Docker Hub

Add to secret.tfvars:

license_key = "YOUR-LICENSE-KEY"

Step 3: Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.

terraform init -upgrade
terraform init -upgrade
SHELL

Step 4: Create a Terraform Execution Plan

Run terraform plan to create an execution plan.

terraform plan -var-file="secret.tfvars" -out main.tfplan
terraform plan -var-file="secret.tfvars" -out main.tfplan
SHELL

Step 5: Apply a Terraform Execution Plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan
terraform apply main.tfplan
SHELL

Step 6: Verify the Results

  1. When you apply the execution plan, Terraform outputs the public IP address. To display the IP address again, run terraform output.

    terraform output -raw container_ipv4_address
    terraform output -raw container_ipv4_address
    SHELL

    Or display the FQDN.

    terraform output -raw container_fqdn
    terraform output -raw container_fqdn
    SHELL
  2. Use Postman or curl to validate. The expected result should return pong.

    curl http://<container_ipv4_address>:8080/v1/document-services/ping
    curl http://<container_ipv4_address>:8080/v1/document-services/ping
    SHELL

    Or use the FQDN.

    curl http://<container_fqdn>:8080/v1/document-services/ping
    curl http://<container_fqdn>:8080/v1/document-services/ping
    SHELL

Step 7: Destroy the Resource

  • Create an execution plan for destroying the resource.

    terraform plan -destroy -var-file="secret.tfvars" -out main.tfplan
    terraform plan -destroy -var-file="secret.tfvars" -out main.tfplan
    SHELL
  • Apply an execution destroy plan.

    terraform apply main.tfplan
    terraform apply main.tfplan
    SHELL

Preguntas Frecuentes

¿Cómo despliego un contenedor Docker en Azure usando Terraform?

Para desplegar un contenedor Docker en Azure usando Terraform, necesita clonar la plantilla del repositorio GitHub para IronSecureDoc, modificar los nombres de los recursos en `variables.tf`, y crear un archivo `secret.tfvars` con datos sensibles. Luego, inicialice el despliegue con terraform init y cree y aplique un plan de ejecución usando terraform plan y terraform apply.

¿Cuáles son los pasos necesarios para configurar Terraform para el despliegue en Azure?

Primero, instale y configure Terraform, luego autentíquelo con Azure. Clone el repositorio específico de GitHub para IronSecureDoc, modifique los nombres de los recursos en el archivo `variables.tf` y gestione los datos sensibles en `secret.tfvars`. Finalmente, inicialice Terraform y cree un plan de ejecución para el despliegue.

¿Cuál es el papel del Registro de Contenedores de Azure en este despliegue?

El Registro de Contenedores de Azure se utiliza para almacenar las imágenes Docker. Necesita añadir las credenciales del registro en el archivo `main.tf` y especificar la información del registro en `variables.tf` y `secret.tfvars` para que el despliegue acceda y despliegue las imágenes Docker necesarias.

¿Cómo puedo verificar si mi despliegue de Terraform en Azure es exitoso?

Después de aplicar el plan de ejecución de Terraform, ejecute terraform output para obtener la dirección IP pública o FQDN. Use herramientas como Postman o curl para enviar una solicitud de ping al servicio. Un despliegue exitoso retornará 'pong'.

¿Qué comando se usa para inicializar Terraform para despliegues en Azure?

Para inicializar Terraform para despliegues en Azure, use el comando terraform init -upgrade. Esto descarga los plugins de proveedor necesarios para gestionar recursos de Azure.

¿Cómo manejo los datos sensibles en las configuraciones de Terraform?

Los datos sensibles como credenciales de registro y claves de licencia deben almacenarse en un archivo `secret.tfvars`. Este archivo se referencia durante los procesos de planificación y aplicación de Terraform para gestionar de manera segura la información sensible.

¿Cuál es el proceso para destruir recursos desplegados en Azure usando Terraform?

Para destruir recursos desplegados en Azure usando Terraform, cree un plan de ejecución de destrucción con terraform plan -destroy -var-file='secret.tfvars' -out main.tfplan y aplíquelo usando terraform apply main.tfplan.

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más
¿Listo para empezar?
Versión: 2024.10 recién lanzado