Saturday, December 21, 2024
HomeCloudAzureHow to create azure container instance using terraform

How to create azure container instance using terraform

In our last post we have see what is azure container instances and its features with where we can use it. Also sort demo about how to create the azure container instance from azure portal. But in real time it will be always recommended to create via some automation code. In that, lets see how to create the azure container instance via Terraform. We will be creating following flow, which will be helpful for most of the cases.

As per diagram, In this we are going to create azure files storage and then we will be proceeding to create the azure container instance.

Storage

Let’s create an Azure Storage Account, with a File share using terraform.

Below code you can find on our github repo also. Here are the details for your reference.

resource "azurerm_storage_account" "aci_storage" {
  name                     = "foxutechacistorage"
  resource_group_name      = var.resource_group_name
  location                 = var.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  min_tls_version          = "TLS1_2"
}
resource "azurerm_storage_share" "container_share" {
  name                 = "aci-data"
  storage_account_name = azurerm_storage_account.aci_storage.name
  quota                = 100
}

Please note, in this post we assumed you have existing resource group on azure, otherwise please create on before proceeding here. In this post “foxutech” is the resource group name.

Also note, in case if your organization has restriction to port 445, as per some security restriction, this will be blocked. In this your azure container may fail in private network. In this post we will be using azure, but it is always aware the possible causes. As this will be fails the container instance to mount the volume. Also please check port 443 is open, as ACI uses https to pull images, if you block that then you will soon find out that you can’t pull anything down from your registry.

Final note here is that the application in Jenkins and as all you know it uses port 8080 for application and 50000 for agent. You will see this on the terraform file.

Azure Container Instance

Okay all set now, lets see the terraform file for creating the azure container instance. Please note most of the values I have variablized and stored on variables.tf file. As this will help to avoid any changes on the main.tf file.

In this you can see the image name, instance CPU and memory, ports are described, feel free the changed as per your application need. We maynot add the environment variables and commands, as it was not needed in this use-case. If you wish to use, you can add like the port block and point it.

resource "azurerm_container_group" "containergroup" {
  name                = var.container_group_name
  location            = var.location
  resource_group_name = var.resource_group_name
  ip_address_type     = "Public"
  os_type             = "Linux"
 
  container {
    name   = var.container_name
    image  = var.image_name
    cpu    = var.cpu_core_number
    memory = var.memory_size

    ports {
        port     = var.port_number-1
        protocol = "TCP"
      }
    ports {
        port     = var.port_number-2
        protocol = "TCP"
      }

    volume {
      name                 = "jenkins-volume"
      mount_path           = "/var/jenkins_home"
      storage_account_name = azurerm_storage_account.aci_storage.name
      storage_account_key  = azurerm_storage_account.aci_storage.primary_access_key
      share_name           = azurerm_storage_share.container_share.name
    }
  }
}

Terraform Steps

Once the code has been updated as per you application requirement, you go to the directory and run the following command to initiate to download the terraform modules.

# terraform init

Once the initiate done successfully, run the next command to check what are the resource will be created by this terraform files.

# terraform plan

Verify what are the resources are getting created and make sure it is not doing anything unexpected changes, like modifying the values or recreate the resource etc.

Once all good, you can apply to create the resources on azure.

# terraform apply

Once this operation completed, you should see the resources are available on the azure portal and access it via public IP.

Check the video below to move around the azure container instance.

You can follow us on social media, to get some short knowledges regularly.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments